加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

c#如何绘制树

发布时间:2021-05-20 08:57:42 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:如图所示,点击Form控件,自动生成一棵树 相关代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsFormsApp7 { ? ? public partial

如图所示,点击Form控件,自动生成一棵树

相关代码如下:

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

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读