unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure DrawPythagorasTree(x1, y1, x2, y2, currentDepth, maxDepth: Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// Set the form size
Width := 400;
Height := 200;
end;
procedure TForm1.DrawPythagorasTree(x1, y1, x2, y2, currentDepth, maxDepth: Integer);
var
dx, dy, x3, y3, x4, y4, x5, y5: Integer;
colorIntensity: Double;
newcolor: TColor;
begin
Canvas.Pen.Color := clBlue;
if currentDepth > 0 then
begin
// Calculate the differences in x and y coordinates between the two points
dx := x2 - x1;
dy := y2 - y1;
// Calculate the coordinates for the three additional points to create the branches
x3 := x2 - dy;
y3 := y2 + dx;
x4 := x1 - dy;
y4 := y1 + dx;
x5 := x4 + (dx - dy) div 2;
y5 := y4 + (dx + dy) div 2;
colorIntensity := 1.0 - (currentDepth / maxDepth);
newcolor := RGBToColor(Round(255 * colorIntensity), Round(255 * colorIntensity), 255);
Canvas.Polygon([Point(x1, ClientHeight - y1), Point(x4, ClientHeight - y4),
Point(x5, ClientHeight - y5), Point(x3, ClientHeight - y3),
Point(x2, ClientHeight - y2), Point(x1, ClientHeight - y1)]);
Canvas.Brush.Color := newcolor;
DrawPythagorasTree(x5, y5, x3, y3, currentDepth - 1, maxDepth);
DrawPythagorasTree(x4, y4, x5, y5, currentDepth - 1, maxDepth);
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
centerX: Integer;
begin
// Calculate the center of the form
centerX := ClientWidth div 2;
// Adjust the starting points to the center
DrawPythagorasTree(centerX - 100, 100, centerX + 100, 100, 8, 12);
end;
end.