分形算法->跳舞的小树

这里用的是GDI来绘图,采用双缓冲.

以下是Form窗体内的所有代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.Threading;
using System.IO;

namespace 分形
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class frmAction : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        SolidBrush brush
= new SolidBrush(Color.Black);
        
private System.Windows.Forms.Button btnPlay;
        
private System.Windows.Forms.Button btnStop;
        Pen pen;
        
public frmAction()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            Init();
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new frmAction());
        }

        
private void Form1_Load(object sender, System.EventArgs e)
        
{             
        }


        
private void RePaint()
        
{
            
while (true)
            
{
                paint(
this.CreateGraphics());
            }
            
        }


        
/// <summary>
        
/// 画叶子
        
/// </summary>
        
/// <param name="g"></param>
        
/// <param name="x"></param>
        
/// <param name="y"></param>
        
/// <param name="L"></param>
        
/// <param name="a"></param>
        
/// <param name="B"></param>
        
/// <param name="C"></param>

        public void DrawLeaf(Graphics g,double x,double y,
            
double L, double a,float B,float C)
        
{    
            
double x1,x2,x1L,x2L,x2R,x1R,
                y1,y2,y1L,y2L,y2R,y1R;
       
            
float s1 = 2;
            
float s2 = 3;
            
float s3 = 1.1f;

            
if(L > s1)
            
{
                x2 
= x + L * Math.Cos(a * PI);
                y2 
= y + L * Math.Sin(a * PI);
                x2R 
= x2 + L / s2 * Math.Cos((a + B) * PI);
                y2R 
= y2 + L / s2 * Math.Sin((a + B) * PI);
                x2L 
= x2 + L / s2 * Math.Cos((a - B) * PI);
                y2L 
= y2 + L / s2 * Math.Sin((a - B) * PI);
  
                x1 
= x + L / s2 * Math.Cos(a * PI);
                y1 
= y + L / s2 * Math.Sin(a * PI);
                x1L 
= x1 + L / s2 * Math.Cos((a - B) * PI);
                y1L 
= y1 + L / s2 * Math.Sin((a - B) * PI);
                x1R 
= x1 + L / s2 * Math.Cos((a + B) * PI);
                y1R 
= y1 + L / s2 * Math.Sin((a + B) * PI);
             
                g.DrawLine(pen,(
int) x, (int) y, (int) x2, (int) y2);
                g.DrawLine(pen,(
int) x2, (int) y2, (int) x2R, (int) y2R);
                g.DrawLine(pen,(
int) x2, (int) y2, (int) x2L, (int) y2L);
                g.DrawLine(pen,(
int) x1, (int) y1, (int) x1L, (int) y1L); 
                g.DrawLine(pen,(
int) x1, (int) y1, (int) x1R, (int) y1R);
          
                DrawLeaf(g, x2, y2, L 
/ s3, a + C,B,C);
                DrawLeaf(g, x2R, y2R, L 
/ s2, a + B,B,C);
                DrawLeaf(g, x2L, y2L, L 
/ s2, a - B,B,C);
                DrawLeaf(g, x1L, y1L, L 
/ s2, a - B,B,C);
                DrawLeaf(g, x1R, y1R, L 
/ s2, a + B,B,C);   
            }

        }


        
/// <summary>
        
/// 绘制图形
        
/// </summary>
        
/// <param name="g"></param>

        public void paint(Graphics g)
        
{        
            
if (dstatus) 
            
{
                D 
+= 0.2f;
                
if (D>=10) dstatus = false;
            }

            
else 
            
{
                D 
-= 0.2f;
                
if (D<=-10) dstatus = true;
            }


            
if (K<60) K=K+0.2f;

            
//清除缓冲内的图形
            offscreenbuffer.Clear(Color.White);
     
            
//在offscreenbuffer中画树;
            DrawLeaf(offscreenbuffer,20030030,270,K,D);
            
            
//将缓冲画出来
            g.DrawImage(offscreenimage,0,0);
        }


        
public void Init()
        

//            //创建缓冲区内的图形
            offscreenimage= new Bitmap(this.Width,this.Height);

            
//得到g;
            offscreenbuffer=Graphics.FromImage(offscreenimage);
            pen 
= new Pen(brush,1);
        }


        
float D=-10;//树的弯曲角度C
        float K=40;//树杈的伸展角度B
        bool dstatus = true;
        
public static readonly double PI = Math.PI / 180;
        Bitmap offscreenimage
=null;    //缓冲区中的图形
        Graphics offscreenbuffer=null;  //缓存中的g
        MemoryStream stream = new MemoryStream();

        
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        
{
            
        }


        Thread thread;
        
private void btnPlay_Click(object sender, System.EventArgs e)
        
{
            thread 
= new Thread(new ThreadStart(RePaint));
            thread.Start();
            btnPlay.Enabled 
= false;
            btnStop.Enabled 
= true;
        }


        
private void btnStop_Click(object sender, System.EventArgs e)
        
{
            thread.Suspend();
            btnStop.Enabled 
= false;
            btnPlay.Enabled 
= true;
        }


        
private void frmAction_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        
{
            
if (thread.ThreadState==ThreadState.Running)
            
{
                thread.Abort();
            }


            Application.ExitThread();
        }

    }

}

posted @ 2005-01-30 16:28  一根神棍研古今  阅读(2886)  评论(10编辑  收藏  举报
Web Counter