c#小程序——画一棵树(v1与v2)

v1:一棵普通的数

using System;
using System.Drawing;//支持基本的 GDI+ 图形功能
using System.Collections;
using System.ComponentModel;//实现组件和控件运行时和设计时行为
using System.Windows.Forms;
using System.Data;
public class Form1 : Form//继承用户界面
{
    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;//左边上层树枝长度缩小比例

    void drawLine(double x0, double y0, double x1, double y1, double width)//画直线
    {
        graphics.DrawLine(new Pen(Color.Green, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);//绿色的直线
    }
    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 / 3);//主干
        //左右两个分支
        drawTree(n - 1, x1, y1, per1 * leng, th + th1);
        drawTree(n - 1, x1, y1, per2 * leng, th - th2);
//递归调用,一棵树实际就是一个个的有两个分支的树干组成的
    }

   private Graphics graphics;
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        graphics = e.Graphics;//窗体或控件的绘图事件
        drawTree(10, 250, 350, 100, -PI / 2);
    }
    public Form1()//定义窗口大小以及添加画图事件
    {
        this.AutoScaleBaseSize = new Size(6, 14);
        this.ClientSize = new Size(500, 400);//窗体大小
        this.Paint += new PaintEventHandler(this.Form1_Paint);
    }
    static void Main()
    {
        Application.Run(new Form1());//开启窗口
    }
}

后来加入随机值得到v2

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : Form
{
    public Form1()
    {
        this.AutoScaleBaseSize = new Size(6, 14);
        this.ClientSize = new Size(500, 400);//窗体大小
        this.Paint += new PaintEventHandler(this.Form1_Paint);
        this.Click += new EventHandler(this.Redraw);//重画
    }
    static void Main()
    {
        Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        graphics = e.Graphics;
        drawTree(10, 250, 350, 100, -PI / 2);
    }
    private void Redraw(object sender, EventArgs e)
    { this.Invalidate(); }//鼠标点击,重新画
    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 / 3);

        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, double width)
    {
     graphics.DrawLine(new Pen(Color.Green, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);
    }
}

这样就可以画出不一样的了2333。开始挺满意的,但是想让树开花,又改了一丢丢代码

 void drawLine(double x0, double y0, double x1, double y1, double width)
    {
        if ((int)width * 3 <= 1)
            graphics.DrawLine(new Pen(Color.Red, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);
        else
            graphics.DrawLine(new Pen(Color.Green, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);
    }
nnn
比刚刚的顺眼多了,然后又想着要不画个十里桃林吧

public Form1()
    {
        this.AutoScaleBaseSize = new Size(6, 14);
        this.ClientSize = new Size(1000,800);//窗体大小
        this.Paint += new PaintEventHandler(this.Form1_Paint);
        this.BackColor = Color.PaleGoldenrod;
        this.Click += new EventHandler(this.Redraw);
    }
private void Form1_Paint(object sender, PaintEventArgs e)
    {
        graphics = e.Graphics;
        for (int i = 0; i < 30; i++) {
            Random pp = new Random();
            drawTree(pp.Next(6,10), pp.Next(200,950), pp.Next(150,740), pp.Next(60,100), -PI / 2);
        }
        
    }

越看越丑。。。。。orz(手动嫌弃)



猜你喜欢

转载自blog.csdn.net/zhonghuachun/article/details/75040598