C#画出一棵树(主要应用递归算法)
//n为树的级别,xO与y0表示生长点,th表示角度,len表示长度
void drawTree(int n,
double x0, double y0, double leng, double th)
{
if (n == 0) return;
GetTextBox();
double x1 = x0 leng * Math.Cos(th);
double y1 = y0 leng * Math.Sin(th);
drawLine(x0, y0, x1, y1);
drawTree(n - 1, x1, y1, per1 * leng * (0.5 rand()), th th1 * (0.5 rand()));
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)
{
Pen newpen = new Pen(Color.Red, width);
graphics.DrawLine(newpen,(int)x0, (int)y0, (int)x1, (int)y1);
}
评论