www.pudn.com > PolyTry.rar > VIEW.CPP


// View.cpp : implementation of the CPolyTryView class 
// 
 
#include "stdafx.h" 
#include "PolyTry.h" 
 
#include "PolyTryDoc.h" 
#include "View.h" 
#include "triangle.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView 
 
IMPLEMENT_DYNCREATE(CPolyTryView, CView) 
 
BEGIN_MESSAGE_MAP(CPolyTryView, CView) 
	//{{AFX_MSG_MAP(CPolyTryView) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_POLYGONALIZE, OnUpdateEditPolygonalize) 
	ON_COMMAND(ID_EDIT_POLYGONALIZE, OnEditPolygonalize) 
	ON_WM_LBUTTONUP() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView construction/destruction 
 
CPolyTryView::CPolyTryView() 
{ 
   m_nPoints      = 0; 
   m_bFlushFaces  = false; 
} 
 
CPolyTryView::~CPolyTryView() 
{ 
} 
 
BOOL CPolyTryView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView drawing 
 
void CPolyTryView::OnDraw(CDC* pDC) 
{ 
	CPolyTryDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
   // 
   CPen  pen(PS_SOLID,1,RGB(255,0,0)); 
   CPen* previusPen=pDC->SelectObject(&pen); 
   // 
   if( m_bFlushFaces  ){ 
      CBrush   brush(RGB(255,255,0)); 
      CBrush*  previusBrush=pDC->SelectObject(&brush); 
      // 
      CTriDC      triangle(pDC); 
      Vector3F*   points   = new Vector3F[m_nPoints]; 
      float       Area=0.0f; 
      // 
      for( int i=0 ; i < m_nPoints ; i++ ){ 
         points[i][0]   = m_points[i].x; 
         points[i][1]   = m_points[i].y; 
         if( i ) Area+= ( m_points[i].x * m_points[i-1].y - m_points[i-1].x * m_points[i].y) * 0.5f; 
      } 
      // 
      int   nTriangle=triangle.Triangulate(points, 
                        Vector3F(0.0f,0.0f,( Area > 0.0f ? -11.0f : 1.0f ) ), 
                        m_nPoints); 
      // 
	   CString	str; 
	   str.Format(_T("Area %.2f nTriangle=%d"),Area,nTriangle); 
	   pDC->TextOut(0,20,str); 
      // 
      for( i=0 ; i < m_nPoints ; i++ ){ 
//	      str.Format(_T("%d"),i); 
 //	      pDC->TextOut(m_points[i].x,m_points[i].y,str); 
      } 
      // 
      pDC->SelectObject(previusBrush); 
   }else{ 
      pDC->MoveTo(m_points[0]); 
      for( int i=1 ; i < m_nPoints ; i++ ) 
         pDC->LineTo(m_points[i]); 
   } 
   pDC->SelectObject(previusPen); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView printing 
 
BOOL CPolyTryView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CPolyTryView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CPolyTryView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView diagnostics 
 
#ifdef _DEBUG 
void CPolyTryView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CPolyTryView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CPolyTryDoc* CPolyTryView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPolyTryDoc))); 
	return (CPolyTryDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CPolyTryView message handlers 
 
void CPolyTryView::OnUpdateEditPolygonalize(CCmdUI* pCmdUI)  
{ 
      pCmdUI->Enable( !m_bFlushFaces  && m_nPoints > 2 ); 
} 
 
void CPolyTryView::OnEditPolygonalize()  
{ 
      m_bFlushFaces        = true; 
      m_points[m_nPoints++]= m_points[0]; 
      InvalidateRect(NULL,TRUE); 
} 
 
void CPolyTryView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
      if( m_bFlushFaces ){ 
         m_bFlushFaces  = false; 
         m_nPoints      = 0; 
      } 
      if( m_nPoints < 99 ) 
         m_points[m_nPoints++]   = point; 
      // 
      InvalidateRect(NULL,TRUE); 
      // 
	   CView::OnLButtonUp(nFlags, point); 
}