Delphi XE5 FireMonkey移动开发示例:Tree Fractal

来源:互联网 发布:java生命周期包括 编辑:程序博客网 时间:2024/04/28 04:10

    这个例子是参照Processing中的例子写的。程序主要用到了旋转、平移变换操作,写法简洁明了,代码如下:

unit Example.TreeMain;
/** * Recursive Tree * by Daniel Shiffman.   * 2013 曹伟民
 * * Renders a simple tree-like structure via recursion.  * The branching angle is calculated as a function of  * the horizontal mouse location. Move the mouse left * and right to change the angle. */ interfaceuses  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,  FMX.Notification, FMX.StdCtrls;type  TTreeForm = class(TForm)    AngleBar: TTrackBar;    procedure FormCreate(Sender: TObject);    procedure FormPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);    procedure AngleBarChange(Sender: TObject);  private    { Private declarations }    FTheta: Single;  public    { Public declarations }    procedure Setup();    procedure Loop();    procedure Branch(H: Single);  end;var  TreeForm: TTreeForm;implementation{$R *.fmx}{ TTreeForm }procedure TTreeForm.Branch(H: Single);var  Matrix: TMatrix;begin  // Each branch will be 2/3rds the size of the previous one  H := H * 0.66;  // All recursive functions must have an exit condition!!!!  // Here, ours is when the length of the branch is 2 pixels or less  if (H > 3) then  begin    Matrix := Canvas.Matrix;  // Save the current state of transformation (i.e . where we are now)    Canvas.SetMatrix(TMatrix.CreateRotation(FTheta) * Matrix);    Canvas.DrawLine(PointF(0, 0), PointF(0, -H), 1);    Canvas.SetMatrix(TMatrix.CreateTranslation(0, -H)* Canvas.Matrix);    Branch(H);    Canvas.SetMatrix(Matrix);    // Repeat the same thing, only branch off to the "left" this time    Matrix := Canvas.Matrix;    Canvas.SetMatrix(TMatrix.CreateRotation(-FTheta) * Matrix);    Canvas.DrawLine(PointF(0, 0), PointF(0, -H), 1);    Canvas.SetMatrix(TMatrix.CreateTranslation(0, -H) * Canvas.Matrix);    Branch(H);    Canvas.SetMatrix(Matrix);  end;end;procedure TTreeForm.FormCreate(Sender: TObject);begin  Setup();end;procedure TTreeForm.FormPaint(Sender: TObject; Canvas: TCanvas;  const ARect: TRectF);begin  Loop();end;procedure TTreeForm.Loop;var  Matrix: TMatrix;  H: Integer;begin  Canvas.BeginScene();  //Canvas.Clear($FF000000);  Canvas.Stroke.Color := $FFFFFFFF;  // Let's pick an angle 0 to 90 degrees based on the mouse position  FTheta := AngleBar.Value / AngleBar.Max * Pi;  Matrix := Canvas.Matrix;  H := ClientHeight div 3;  // Start the tree from the bottom of the screen  Canvas.SetMatrix(TMatrix.CreateTranslation(ClientWidth / 2, ClientHeight));  // Draw a line 120 pixels  Canvas.DrawLine(PointF(0, 0), PointF(0, -H), 1);  // Move to the end of that line  Canvas.SetMatrix(Canvas.Matrix * TMatrix.CreateTranslation(0, -H));  // Start the recursive branching!  Branch(H);  Canvas.SetMatrix(Matrix);  Canvas.EndScene;end;procedure TTreeForm.Setup;beginend;procedure TTreeForm.AngleBarChange(Sender: TObject);begin  Invalidate();end;end.
效果图:



原创粉丝点击