unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, LCLIntf, LCLType;
type
{ TForm1 }
TForm1 = class(TForm)
btn_drawtree: TButton;
btn_save: TButton;
PaintBox1: TPaintBox;
Panel1: TPanel;
procedure btn_drawtreeClick(Sender: TObject);
procedure btn_saveClick(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
private
public
procedure ctreefractal(X, Y, Size : Integer; Angle : Real);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.ctreefractal(X, Y, Size : Integer; Angle : Real);
Var x2, y2 : Integer;
begin
if size < 3 then paintBox1.Canvas.Pen.color := clRed else
if size < 5 then paintBox1.Canvas.Pen.color := clYellow else
if size < 8 then paintBox1.Canvas.Pen.color := clLime else
if size < 16 then paintBox1.Canvas.Pen.color := clGreen else
paintBox1.Canvas.Pen.color := clMaroon;
if size < 3 then paintBox1.Canvas.Pen.Width := 1 else
if size < 5 then paintBox1.Canvas.Pen.Width := 2 else
if size < 8 then paintBox1.Canvas.Pen.Width := 4 else
if size < 16 then paintBox1.Canvas.Pen.Width := 6 else
paintBox1.Canvas.Pen.Width := 6;
x2 := round(x + size * sin(angle)); // round(x + size * sin(angle)+5) changer ces valeurs
y2 := round(y + size * cos(angle));// round(y + size * cos(angle)+5)
paintBox1.Canvas.MoveTo(X, Y);
paintBox1.Canvas.LineTo(x2, y2);
{ Two recursive calls. }
if Size > 0 then
begin
ctreefractal(x2, y2, 3 * size div 4, angle + Pi / (5 + random(6)));
ctreefractal(x2, y2, 3 * size div 4, angle - Pi / (5 + random(6)));
end
end;
procedure TForm1.btn_drawtreeClick(Sender: TObject);
begin
//PaintBox1Paint(PaintBox1);
PaintBox1.Invalidate;
end;
procedure TForm1.btn_saveClick(Sender: TObject);
var mypng:TPortableNetworkGraphic;
//bmp:tbitmap;
begin
mypng:=TPortableNetworkGraphic.create;
mypng.SetSize(PaintBox1.ClientWidth,PaintBox1.ClientHeight);
mypng.Canvas.Brush.Style := bsClear; //THIS ONE!!
mypng.PixelFormat:= pf24bit;
mypng.Canvas.Brush.Color := clnone;
mypng.clear;
mypng.Canvas.CopyRect(rect(0, 0, mypng.Width, mypng.Height),PaintBox1.canvas,rect(0, 0, PaintBox1.ClientWidth, PaintBox1.ClientHeight));
mypng.SaveToFile('testpngimage.png');
mypng.free;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
ctreefractal(PaintBox1.ClientWidth div 2 ,
PaintBox1.clientHeight,
PaintBox1.clientHeight div 4,
Pi)
end;
end.