c#——《c#语言程序设计》实验报告——泛型与集合——“画树”程序 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
c#——《c#语言程序设计》实验报告——泛型与集合——“画树”程序
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
原先的例子中,两棵子树的生长点都在(x1,y1),我们改进一下,将两棵子树的生长点不同,在(x1,y1)及(x2,y2)。
程序中可以加上一些控件(如滚动条、文本框等),以方便用户修改角度(例子中是35及30度)、长度(例子中是per1,per2),这里又加了两子树的位置的系数(即点0至点2的长度是点0至点1的长度的多少倍k)。
(例子中,x1=x0 leng*cos(th), 这里要加个x2=x0 leng*k*cos(th) )。
还可以加上颜色、粗细、是否随机等选项,全在于发挥你的想像力!
源代码
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 homework18 {public partial class form1 : form{public form1(){initializecomponent();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);//重画}private void form1_paint(object sender, painteventargs e){graphics = e.graphics;drawtree(10, 250, 350, 100, -pi / 2);drawtree(10, 150, 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){if ((int)width * 3 <= 1)graphics.drawline(new pen(color.red, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);elsegraphics.drawline(new pen(color.green, (int)width), (int)x0, (int)y0, (int)x1, (int)y1);}} }运行结果
https://blog.csdn.net/zhonghuachun/article/details/75040598
总结
以上是尊龙游戏旗舰厅官网为你收集整理的c#——《c#语言程序设计》实验报告——泛型与集合——“画树”程序的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c#——《c#语言程序设计》实验报告——
- 下一篇: