• ASP.NET使用递归算法实现画树程序


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


    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 );
        }
    }
    }


  • 相关阅读:
    Python之路,Day2
    Dnsmasq安装与配置-搭建本地DNS服务器 更干净更快无广告DNS解析
    bind+dlz+mysql实现区域记录动态更新
    SUSE下FTP服务器搭建
    最简单粗暴的http文件列表
    shell脚本监控MySQL服务是否正常
    解决问题:Jupyter Notebook启动不会自动打开浏览器,每次都要自己打开浏览器输入网址
    让Jupyter Notebook个性化
    数据预处理小结
    多模型融合推荐算法
  • 原文地址:https://www.cnblogs.com/cache-yuan/p/9042346.html
Copyright © 2020-2023  润新知