c#小程序——画一棵树(v1与v2)
来源:互联网 发布:mcmore php 编辑:程序博客网 时间:2024/05/16 14:15
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(手动嫌弃)
阅读全文
0 0
- c#小程序——画一棵树(v1与v2)
- 系统小闹钟V1.0 C#程序小例
- Inception v1 —v2 —v3 —v4
- V1接口与V2接口对应表
- Openstack组件实现原理 — Glance架构(V1/V2)
- MemorySniffer V1.0小程序
- C#小程序—字符串加密
- 学习之路——命令行菜单小程序V1.0
- 系统学习深度学习(二十一)--GoogLeNetV4与Inception-ResNet V1,V2
- 第一个golang程序 ( v1.0 v2.0 )
- Excel潜能系列——Excel游戏(2v2战斗~5v5战斗模拟器)【更新V1.5】 Einsphoton
- RIP v1与RIP v2路由协议对比分析
- YOLO v1,YOLO v2,YOLO9000算法总结与源码解析
- Android 签名时 v2 与 v1 的选择
- Android 签名时 v2 与 v1 的选择
- 命令行菜单小程序v2.0
- 命令行菜单小程序V1.0
- 命令行菜单小程序v1.0
- Apache 的 httpd 过程占用大量内存原因及其解决方案
- win10上使用putty通过ssh连接Linux
- CSS基础知识总结之基本概念
- 今天学习的时候偶然发现了一个有趣的题目,可以用编程实现。
- Linux进程全解1——程序的开始、结束
- c#小程序——画一棵树(v1与v2)
- 2. 掀起接口测试的盖头-HTTP初窥(一)
- C语言基础编程之统计字母,数字和空格
- B/S初识
- JSch
- 北京大学可视化发展前沿研究生暑期学校Day4
- Android开发工程师面试题整理(百度)
- CSS样式的学习
- C#客户端(WinForm)开机自动启动实现