kochView.cpp
上传用户:wangyiwen
上传日期:2021-03-13
资源大小:287k
文件大小:4k
源码类别:

分形几何

开发平台:

Visual C++

  1. // kochView.cpp : implementation of the CKochView class
  2. //
  3. #include "stdafx.h"
  4. #include "koch.h"
  5. #include "math.h"
  6. #include "kochDoc.h"
  7. #include "kochView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CKochView
  15. IMPLEMENT_DYNCREATE(CKochView, CView)
  16. BEGIN_MESSAGE_MAP(CKochView, CView)
  17. //{{AFX_MSG_MAP(CKochView)
  18. ON_COMMAND(ID_KOCH_CURVE, OnKochCurve)
  19. ON_COMMAND(ID_KOCH_SNOW, OnKochSnow)
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CKochView construction/destruction
  28. CKochView::CKochView()
  29. {
  30. // TODO: add construction code here
  31. }
  32. CKochView::~CKochView()
  33. {
  34. }
  35. BOOL CKochView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CKochView drawing
  43. void CKochView::OnDraw(CDC* pDC)
  44. {
  45. CKochDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. // TODO: add draw code for native data here
  48. /* CPOINT p,q;
  49. p.x=100;
  50. p.y=50;
  51. q.x=800;
  52. q.y=400;
  53. koch(p,q,5);*/
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CKochView printing
  57. BOOL CKochView::OnPreparePrinting(CPrintInfo* pInfo)
  58. {
  59. // default preparation
  60. return DoPreparePrinting(pInfo);
  61. }
  62. void CKochView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  63. {
  64. // TODO: add extra initialization before printing
  65. }
  66. void CKochView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  67. {
  68. // TODO: add cleanup after printing
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CKochView diagnostics
  72. #ifdef _DEBUG
  73. void CKochView::AssertValid() const
  74. {
  75. CView::AssertValid();
  76. }
  77. void CKochView::Dump(CDumpContext& dc) const
  78. {
  79. CView::Dump(dc);
  80. }
  81. CKochDoc* CKochView::GetDocument() // non-debug version is inline
  82. {
  83. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CKochDoc)));
  84. return (CKochDoc*)m_pDocument;
  85. }
  86. #endif //_DEBUG
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CKochView message handlers
  89. void CKochView::koch(CPOINT p0, CPOINT p1, int iter)
  90. {
  91. CClientDC dc(this);
  92. CPOINT r0,r1,r2;
  93. if(iter==1)
  94. {
  95.     dc.MoveTo(p0.x,p0.y);
  96.     dc.LineTo(p1.x,p1.y);
  97. return;
  98. }
  99. if(iter>1)
  100. {
  101. r0.x=p0.x+(p1.x-p0.x)/3;
  102. r0.y=p0.y+(p1.y-p0.y)/3;
  103. r1.x=p0.x+2*(p1.x-p0.x)/3;
  104. r1.y=p0.y+2*(p1.y-p0.y)/3;
  105.         r2.x=(r1.x-r0.x)/2-(r1.y-r0.y)*sqrt(3)/2+r0.x;
  106. r2.y=(r1.y-r0.y)/2+(r1.x-r0.x)*sqrt(3)/2+r0.y;
  107. koch(p0,r0,iter-1);
  108. koch(r1,p1,iter-1);
  109. koch(r0,r2,iter-1);
  110. koch(r2,r1,iter-1);
  111. }
  112. }
  113. void CKochView::OnKochCurve() 
  114. {
  115.   ClearScreen();
  116. CPOINT p,q;
  117. p.x=100;
  118. p.y=100;
  119. q.x=800;
  120. q.y=100;
  121. koch(p,q,8);
  122. }
  123. void CKochView::OnKochSnow() 
  124. {
  125. ClearScreen();
  126.     CPOINT p0,p1,p2;
  127. int iter=8;
  128. p0.x=100;
  129. p0.y=150;
  130. p2.x=500;
  131. p2.y=150;
  132. p1.x=300;p1.y=496;
  133. koch(p0,p1,iter);
  134. koch(p1,p2,iter);
  135. koch(p2,p0,iter);
  136. }
  137. void CKochView::ClearScreen()
  138. {
  139. CClientDC dc(this);
  140. CRect window; 
  141. GetClientRect(window);
  142. dc.SelectStockObject(WHITE_PEN);
  143. dc.SelectStockObject(WHITE_BRUSH);
  144. dc.Rectangle(window);
  145. }