相关代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.windows.Forms;
using System.Data;
namespace windowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
this.autoScaleBaseSize = new Size(6,14);
this.ClIEntSize = new Size(400,400);
this.Paint += new PaintEventHandler(this.Form1_Load);
this.Click += new EventHandler(this.Redraw);
}
private voID Form1_Load(object sender,PaintEventArgs e)
{
graphics = e.Graphics;
drawTree(10,200,380,100,-PI / 2);
}
private voID Redraw(object sender,EventArgs e)
{
this.InvalIDate();
}
private Graphics graphics;
const double PI = Math.PI;
double th1 = 35 * Math.PI / 180;
double th2 = 25 * 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);
drawTree(n - 1,y1,per1 * leng * (0.5 + rand()),th + th1 * (0.5 + rand()));
drawTree(n - 1,per2 * leng * (0.4 + rand()),th - th2 * (0.5 + rand()));
if (rand() > 0.6)
drawTree(n - 1,th - th2 * (0.5 + rand()));
}
voID drawline(double x0,double x1,double y1)
{
graphics.Drawline(
Pens.Blue,
(int)x0,(int)y0,(int)x1,(int)y1);
}
}
}
以上是内存溢出为你收集整理的c#如何绘制树全部内容,希望文章能够帮你解决c#如何绘制树所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)