实现效果如下:(随机生成)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(600, 400);
this.Paint += new PaintEventHandler(this.Form1_Paint);//注册paint事件
this.Click+=new EventHandler(this.Redraw);//注册单击事件
} private void Redraw(object sender, EventArgs e)
{
this.Invalidate();//使窗口重绘
} private void Form1_Paint(object sender, PaintEventArgs e)
{
graphics = e.Graphics ;
drawTree( 10, 200, 310, 100, -PI/2 );
} private Graphics graphics;
const double PI = Math.PI;
double th1 = 40 * Math.PI / 180;
double th2 = 30 * Math.PI / 180;
double per1 = 0.6;
double per2 = 0.7;
Random rnd = new Random();
double rand()
{
return rnd.NextDouble();
}
void drawTree(int n,
double x0, double y0, double leng, double th)
{
if( n==0 ) return; double x1 = x0 + leng * Math.Cos(th);
double y1 = y0 + leng * Math.Sin(th); drawLine(x0, y0, x1, y1, n/2); drawTree( n - 1, x1, y1, per1 * leng*(0.5+rand()), th + th1*(0.5+rand()) );//使用递归算法反复执行drawTree方法,直到n=0,跳出
drawTree( n - 1, x1, y1, per2 * leng*(0.4+rand()), th - th2*(0.5+rand()) );
if (rand() > 0.6)
drawTree(n - 1, x1, y1, per2 * leng * (0.4 + rand()), th - th2 * (0.5 + rand()));
}
void drawLine( double x0, double y0, double x1, double y1, int width ){
graphics.DrawLine(
new Pen(Color.Blue, width ),
(int)x0, (int)y0, (int)x1, (int)y1 );
}
}
}

最新文章

  1. linux 多线程基础
  2. jquery版悬浮模块demo
  3. MySql中的tinying,smallint,int,bigint的类型介绍——转载
  4. C文件操作(全)
  5. 【IOS】3. OC 类声明和实现
  6. java web 前端页面的分页逻辑
  7. Co-prime Array&&Seating On Bus(两道水题)
  8. 国内三大PTPrivate Tracker站分析
  9. [.Net Tools] 超強大的封裝工具 Advanced Installer
  10. Servlet综述
  11. 利用xcopy命令实现本地文件复制到远程服务器的方法
  12. Linux下ftp和ssh详解
  13. php curl 跨域情趣
  14. eclipse中tomcat的add and Remove找不到项目
  15. 数据类型 varchar 和 uniqueidentifier 在 add 运算符中不兼容
  16. eclipse 无法记住svn密码
  17. Day12 (黑客成长日记) 函数
  18. java.sql.SQLException: com.mysql.jdbc.Driver
  19. 关于新版OPENWRT拔PPTP的619错或PPTPD无法连接问题笔记
  20. AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 3. Data Structures Fundamental Data Structures

热门文章

  1. ORACLE中的PL/SQL
  2. DOS命令行(6)——Windows网络状态及用户管理
  3. ASW 工作流最佳实践(二):使用 ASW 并发调用函数
  4. 19、oracle的启动和关闭过程
  5. 移动端touch、click、tap的区别
  6. hdu 1540 Tunnel Warfare 线段树 区间合并
  7. Mac nasm 汇编入门
  8. nginx反向代理tcp协议的80端口
  9. MyEclipse中,编写properties文件,输入中文显示乱码
  10. 《Linux基础知识及命令》系列分享专栏