drawtree.cpp
资源名称:tree1.rar [点击查看]
上传用户:yh22282098
上传日期:2021-08-30
资源大小:7k
文件大小:1k
源码类别:
分形几何
开发平台:
Visual C++
- #include "drawtree.h"
- #include<math.h>
- BOOL CMyApp::InitInstance()
- {
- this->m_pMainWnd=new CMainWnd();
- m_pMainWnd->ShowWindow(m_nCmdShow);
- m_pMainWnd->UpdateWindow();
- return true;
- }
- CMyApp theapp;
- int CMainWnd::OnCreate(LPCREATESTRUCT cs)
- {
- if(CFrameWnd::OnCreate(cs)==-1)
- return -1;
- sqt=sqrt((dt_x*dt_x+dt_y*dt_y)/2);
- return 0;
- }
- CMainWnd::CMainWnd()
- {
- Create(NULL,_T("DrawTree"));
- srand(time(NULL));//初始化随机种子
- }
- BEGIN_MESSAGE_MAP(CMainWnd,CFrameWnd)
- ON_WM_CREATE()
- ON_WM_PAINT()
- END_MESSAGE_MAP()
- void CMainWnd::OnPaint()
- {
- AfxGetMainWnd()->SetWindowText("分形树2010");
- count=0;
- CPaintDC dc(this);
- CRect rect;
- GetClientRect(&rect);
- //起点所在的屏幕位置
- int pa=rect.Width()/2;
- int pb=rect.Height();
- drawtree(&dc,pa,pb,ang,len);//,wid
- }
- void CMainWnd::drawtree(CPaintDC* pdc,int px,int py,double ang,double l)//,short width
- {
- double rn=rand()%10*(PI/180);//弧度值的偏差,若无则对称
- INT rn2=rand()%9;
- if(rn2>=5) rn=-1*rn;
- // if(width<1) width=1;
- int x=px+(int)(l*cos(ang));
- int y=py-(int)(l*sin(ang));
- pdc->MoveTo(px,py);
- pdc->LineTo(x,y);
- if(l<25)
- return;
- drawtree(pdc,x,y,ang-arg+rn,l*sqt);//,width+2
- drawtree(pdc,x,y,ang+arg+rn,l*sqt);//,width+2
- }