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.效果图:
- Delphi XE5 FireMonkey移动开发示例:Tree Fractal
- Delphi XE5 FireMonkey移动开发示例:粒子系统
- Delphi XE5 FireMonkey移动开发示例:Koch分形
- Delphi xe5 firemonkey关闭应用程序
- Delphi XE5 Firemonkey 创建windows 8 应用效果
- 关于Delphi xe5 firemonkey 手机屏幕自适应程序问题
- 在Delphi XE5 FireMonkey中设置TEdit中的光标位置
- Delphi XE5 的开发团队
- Delphi xe5 手机开发经验(新手级别)
- Delphi XE5的Android开发平台搭建
- 关于Delphi xe5跨平台开发
- Delphi XE5 开发android环境配置
- Delphi XE5 开发android环境配置
- delphi XE5
- 国外看到的一个DELPHI XE5 POPMENU的示例
- win8.1 64bit 小米开发 delphi xe5
- DELPHI XE5开发WEB服务器及安卓手机客户端
- Delphi XE5开发之 Android permission 访问权限大全
- VMware Player 虚拟机中音乐播放无声音 问题
- Java开发中使用Oracle数据库的一点注意事项
- 说说哪本书是对程序员最有影响、每个程序员都该阅读的书?
- zoj 1406 poj 1251 Jungle Roads
- 制作可独立发布的android模拟器
- Delphi XE5 FireMonkey移动开发示例:Tree Fractal
- 简单实现Java中的ArrayList
- JSP项目部署到本地tomcat
- wince6R3
- MP算法和OMP算法及其思想
- 开源项目管理系统:ProjectForge
- 如何通过图的方法来寻找一张地图的所有可达路径
- 简单实现Java中的LinkedList
- 股票的涨幅和跌幅求平均涨幅和跌幅