www.pudn.com > my2cadtest07261930.rar > Polygon.cpp, change:2006-07-25,size:6162b


// Polygon.cpp: implementation of the CPolygon class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Polygon.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CPolygon::CPolygon(CSuperPoint& begPos,CSuperPoint& endPos,int num):m_polycount(num) 
{ 
  this->m_whatshape=Polygon;   
  double angle = 2*PII/m_polycount;   //通过边数求得于圆心的夹角         
  m_pointcount=num+1; 
   
  m_point[0]=begPos; 
  m_point[1]=endPos; 
 
    POINT fisrtPoint; 
	fisrtPoint.x=endPos.GetPoint().x; 
	fisrtPoint.y=endPos.GetPoint().y; 
  for(int i = 2 ;  i < m_pointcount ; i++ ) 
	{				 
		    POINT nextPoint; 
            		 
             nextPoint.x = (int) (begPos.GetPoint().x + (fisrtPoint.x - begPos.GetPoint().x) * cos(angle) - 
							(fisrtPoint.y - begPos.GetPoint().y) * sin(angle)); 
             nextPoint.y = (int) (begPos.GetPoint().y + (fisrtPoint.x - begPos.GetPoint().x) * sin(angle) +  
							(fisrtPoint.y - begPos.GetPoint().y) * cos(angle)); 
			m_point[i]=nextPoint; 
			fisrtPoint=	nextPoint; 
             
	} 
 
   this->CalcCenterPoint(); 
   this->CalcCenterRECT(); 
   
} 
 
CPolygon::CPolygon() 
{ 
 
} 
 
 
CPolygon::~CPolygon() 
{ 
 
} 
 
void CPolygon::CalcCenterPoint() 
{ 
   m_centerpoint=m_point[0]; 
} 
 
void CPolygon::CalcCenterRECT() 
{ 
	double radius; 
    radius = m_point[0].DistanceTo(m_point[1]); 
 
    m_rect.left = m_centerpoint.GetPoint().x - radius - 6; 
	m_rect.top  = m_centerpoint.GetPoint().y - radius - 6; 
	m_rect.right = m_centerpoint.GetPoint().x + radius + 6; 
	m_rect.bottom = m_centerpoint.GetPoint().y + radius + 6; 
 
} 
 
void CPolygon::PreDraw(HDC &hdc) 
{ 
  POINT pt[100]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_copypoint[nI].GetPoint(); 
	} 
	 		 
	::Polygon(hdc,pt+1,m_pointcount-1);  
		  
} 
 
 
 
CShape * CPolygon::GetShapeCopy() 
{ 
  CShape * pcopy=new CPolygon; 
  *pcopy=*this;   //值拷贝 
   
  return pcopy ; 
 
} 
 
 
bool CPolygon::IsBound(CSuperPoint &curPt) 
{ 
 for(int nI=1;nI<m_pointcount-1;nI++) 
	 { 
		if(curPt.IsOnLine(m_point[nI],m_point[nI+1])) 
			return true; 
	 }	  
 return false; 
 
} 
 
void CPolygon::SetMark(HDC &hdc) 
{ 
   for(int nI=1;nI<m_pointcount;nI++) 
   { 
    m_point[nI].SetMark(hdc); 
   } 
	 
 
} 
 
void CPolygon::ClearMark(HDC &hdc) 
{ 
    for(int nI=1;nI<m_pointcount;nI++) 
	   { 
		m_point[nI].ClearMark(hdc); 
	   } 
			 
} 
 
void CPolygon::Lock(HDC &hdc) 
{ 
	this->m_selected = true;	//已锁定 
	for(int nI=1;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].SetLockMark(hdc); 
	} 
} 
 
void CPolygon::UnLock(HDC &hdc) 
{ 
	this->m_selected = false; 
    for(int nI=1;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].ClearLockMark(hdc); 
	} 
	 
} 
 
void CPolygon::EnterOper(HDC &hdc)   
{ 
    	    
     for(int nI=0;nI<m_pointcount;nI++) 
	 { 
	 m_point[nI]=m_copypoint[nI];	   
	 } 
	 ::InvalidateRect(g_hwnd,&m_rect,true); 
 
     penattrib->GetPen(hdc,false,false); 
     DrawSelf(hdc);  
   
    this->CalcCenterPoint(); 
    this->CalcCenterRECT(); 
 
   ::InvalidateRect(g_hwnd,&m_rect,true); 
 
	m_operStatus=FirstOper; 
     
    penattrib->ReleasePen(false,false); 
} 
 
void CPolygon::DrawSelf(HDC &hdc) 
{ 
   POINT pt[100]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	} 
		 
		::Polygon(hdc,pt+1,m_pointcount-1);  
	 
	if(m_selected)   //如果这是一个被选中图形 在操作后的图形上打上标记 
   { 
		for(int nI=1;nI<m_pointcount;nI++) 
		{ 
			m_point[nI].SetStatus(); 
		} 
    SetMark(hdc); 
    Lock(hdc); 
   } 
} 
 
void CPolygon::SavetoEmf(HDC &hdc) 
{ 
	if	( 
		penattrib->GetPenStyle().lopnColor!=m_penstruct.lopnColor|| 
		penattrib->GetPenStyle().lopnWidth.x!=m_penstruct.lopnWidth.x|| 
		penattrib->GetPenStyle().lopnStyle!=m_penstruct.lopnStyle 
		) 
		{ 
		    penattrib->ReleasePen(false); 
			penattrib->SetPenStyle(m_penstruct); 
			penattrib->GetPen(hdc,false); 
		} 
 
	if	( 
		penattrib->GetBrushStyle().lbColor!=m_brushstruct.lbColor|| 
		penattrib->GetBrushStyle().lbHatch!=m_brushstruct.lbHatch|| 
		penattrib->GetBrushStyle().lbStyle!=m_brushstruct.lbStyle 
		) 
		{ 
		    penattrib->ReleaseBrush(); 
			penattrib->SetBrushStyle(m_brushstruct); 
			penattrib->GetBrush(hdc); 
		} 
	 
	POINT pt[100]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	}		 
	::Polygon(hdc,pt+1,m_pointcount-1);  
 
} 
 
 
 
 
 
void CPolygon::MoveSelf(HDC &hdc,int dx,int dy) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].Move(dx,dy); 
	}	 
	 PreDraw(hdc);  
    penattrib->ReleasePen(true); 
 
} 
 
void CPolygon::ZoomSelf(HDC &hdc,double scale) 
{ 
   penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].ZoomIn(scale,m_centerpoint); 
	} 
	 
	 PreDraw(hdc);  
     
     penattrib->ReleasePen(true); 
} 
 
 
void CPolygon::RotateSelf(HDC &hdc,double angle) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].RotateOnFix(this->GetCenterPoint(),angle); 
	} 
	     
	 PreDraw(hdc);  
 
    penattrib->ReleasePen(true); 
} 
 
 
void CPolygon::MirrorSelf(HDC &hdc,double angle,CSuperPoint &ptPos) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].MirrorOnAxis(angle,ptPos); 
	}	 
	 PreDraw(hdc);  
 
	 penattrib->ReleasePen(true); 
}