drawtree.cpp
上传用户:yh22282098
上传日期:2021-08-30
资源大小:7k
文件大小:1k
源码类别:

分形几何

开发平台:

Visual C++

  1. #include "drawtree.h"
  2. #include<math.h>
  3. BOOL CMyApp::InitInstance()
  4. {
  5. this->m_pMainWnd=new CMainWnd();
  6.     m_pMainWnd->ShowWindow(m_nCmdShow);
  7. m_pMainWnd->UpdateWindow();
  8. return true;
  9. }
  10. CMyApp theapp;
  11. int CMainWnd::OnCreate(LPCREATESTRUCT cs)
  12. {
  13. if(CFrameWnd::OnCreate(cs)==-1)
  14. return -1;
  15. sqt=sqrt((dt_x*dt_x+dt_y*dt_y)/2);
  16.     return 0;
  17. }
  18. CMainWnd::CMainWnd()
  19. {
  20. Create(NULL,_T("DrawTree"));
  21. srand(time(NULL));//初始化随机种子
  22. }
  23. BEGIN_MESSAGE_MAP(CMainWnd,CFrameWnd)
  24.    ON_WM_CREATE()
  25.    ON_WM_PAINT()
  26. END_MESSAGE_MAP()
  27. void CMainWnd::OnPaint()
  28. {  
  29. AfxGetMainWnd()->SetWindowText("分形树2010");
  30. count=0;
  31.     CPaintDC dc(this);
  32. CRect rect;
  33. GetClientRect(&rect);
  34. //起点所在的屏幕位置
  35.     int pa=rect.Width()/2;
  36. int pb=rect.Height();
  37. drawtree(&dc,pa,pb,ang,len);//,wid
  38. }
  39. void CMainWnd::drawtree(CPaintDC* pdc,int px,int py,double ang,double l)//,short width
  40. {
  41. double rn=rand()%10*(PI/180);//弧度值的偏差,若无则对称
  42. INT rn2=rand()%9;
  43. if(rn2>=5)  rn=-1*rn;
  44. // if(width<1) width=1;
  45.    
  46. int x=px+(int)(l*cos(ang));
  47. int y=py-(int)(l*sin(ang));
  48.      pdc->MoveTo(px,py);
  49.      pdc->LineTo(x,y);
  50.    if(l<25)
  51.    return;
  52.     drawtree(pdc,x,y,ang-arg+rn,l*sqt);//,width+2
  53.     drawtree(pdc,x,y,ang+arg+rn,l*sqt);//,width+2
  54. }