Forum > Graphics

More Trees: Different Pythagoras Tree Styles

(1/1)

Boleeman:
A very nicely coloured Pythagoras Fractal Tree to play around with.
Perhaps would be nice to see BGRABmp version with varying opacity.

Converted from Fwend js source at https://github.com/fwend/Pythagoras-tree

Enjoy.

Boleeman:
Another type with 5 random colors.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- unit Unit1; {\$mode objfpc}{\$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);    procedure FormPaint(Sender: TObject);  private    procedure PythagorasTree(x1, y1, x2, y2: Double; depth: Integer);  public    { public declarations }  end; var  Form1: TForm1; implementation {\$R *.lfm} { TForm1 } procedure TForm1.PythagorasTree(x1, y1, x2, y2: Double; depth: Integer);var  dx, dy, x3, y3, x4, y4, x5, y5: Double;begin  if depth > 10 then    Exit;   dx := x2 - x1;  dy := y1 - y2;  x3 := x2 - dy;  y3 := y2 - dx;  x4 := x1 - dy;  y4 := y1 - dx;  x5 := x4 + (dx - dy) / 2;  y5 := y4 - (dx + dy) / 2;   case Random(5) + 1 of    1: Canvas.Pen.Color := clMaroon;    2: Canvas.Pen.Color := clRed;    3: Canvas.Pen.Color := clTeal;    4: Canvas.Pen.Color := TColor(\$00A5FF);         //Orange color    5: Canvas.Pen.Color := clGreen;  end;  Canvas.Pen.Width:=2;  Canvas.Line(Round(x1), Round(y1), Round(x2), Round(y2));  Canvas.Line(Round(x2), Round(y2), Round(x3), Round(y3));  Canvas.Line(Round(x3), Round(y3), Round(x4), Round(y4));  Canvas.Line(Round(x4), Round(y4), Round(x1), Round(y1));   PythagorasTree(x4, y4, x5, y5, depth + 1);  PythagorasTree(x5, y5, x3, y3, depth + 1);end; procedure TForm1.FormCreate(Sender: TObject);begin  Randomize;end; procedure TForm1.FormPaint(Sender: TObject);var  w, h, w2, diff: Integer;begin  w := 800;  h := w * 11 div 16;  w2 := w div 2;  diff := w div 12;   PythagorasTree(w2 - diff, h - 10, w2 + diff, h - 10, 0);end; end.

Boleeman:
And two other variations:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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.
and

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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.

lainz:
:)

Very good