kochView.cpp
上传用户:wangyiwen
上传日期:2021-03-13
资源大小:287k
文件大小:4k
- // kochView.cpp : implementation of the CKochView class
- //
- #include "stdafx.h"
- #include "koch.h"
- #include "math.h"
- #include "kochDoc.h"
- #include "kochView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CKochView
- IMPLEMENT_DYNCREATE(CKochView, CView)
- BEGIN_MESSAGE_MAP(CKochView, CView)
- //{{AFX_MSG_MAP(CKochView)
- ON_COMMAND(ID_KOCH_CURVE, OnKochCurve)
- ON_COMMAND(ID_KOCH_SNOW, OnKochSnow)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CKochView construction/destruction
- CKochView::CKochView()
- {
- // TODO: add construction code here
- }
- CKochView::~CKochView()
- {
- }
- BOOL CKochView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKochView drawing
- void CKochView::OnDraw(CDC* pDC)
- {
- CKochDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- /* CPOINT p,q;
- p.x=100;
- p.y=50;
- q.x=800;
- q.y=400;
- koch(p,q,5);*/
-
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKochView printing
- BOOL CKochView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CKochView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CKochView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKochView diagnostics
- #ifdef _DEBUG
- void CKochView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CKochView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CKochDoc* CKochView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CKochDoc)));
- return (CKochDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CKochView message handlers
- void CKochView::koch(CPOINT p0, CPOINT p1, int iter)
- {
- CClientDC dc(this);
- CPOINT r0,r1,r2;
- if(iter==1)
- {
- dc.MoveTo(p0.x,p0.y);
- dc.LineTo(p1.x,p1.y);
- return;
- }
- if(iter>1)
- {
- r0.x=p0.x+(p1.x-p0.x)/3;
- r0.y=p0.y+(p1.y-p0.y)/3;
- r1.x=p0.x+2*(p1.x-p0.x)/3;
- r1.y=p0.y+2*(p1.y-p0.y)/3;
- r2.x=(r1.x-r0.x)/2-(r1.y-r0.y)*sqrt(3)/2+r0.x;
- r2.y=(r1.y-r0.y)/2+(r1.x-r0.x)*sqrt(3)/2+r0.y;
- koch(p0,r0,iter-1);
- koch(r1,p1,iter-1);
- koch(r0,r2,iter-1);
- koch(r2,r1,iter-1);
- }
- }
- void CKochView::OnKochCurve()
- {
- ClearScreen();
- CPOINT p,q;
- p.x=100;
- p.y=100;
- q.x=800;
- q.y=100;
- koch(p,q,8);
-
- }
- void CKochView::OnKochSnow()
- {
- ClearScreen();
- CPOINT p0,p1,p2;
- int iter=8;
- p0.x=100;
- p0.y=150;
- p2.x=500;
- p2.y=150;
- p1.x=300;p1.y=496;
- koch(p0,p1,iter);
- koch(p1,p2,iter);
- koch(p2,p0,iter);
-
- }
- void CKochView::ClearScreen()
- {
- CClientDC dc(this);
- CRect window;
- GetClientRect(window);
- dc.SelectStockObject(WHITE_PEN);
- dc.SelectStockObject(WHITE_BRUSH);
- dc.Rectangle(window);
- }