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


// Triangle.cpp: implementation of the CTriangle class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Triangle.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CTriangle::CTriangle(CSuperPoint &pt1,CSuperPoint &pt2,CSuperPoint &pt3):CShape() 
{ 
   this->m_whatshape=Triangle;  
   m_point[0]=pt1; 
   m_point[1]=pt2; 
   m_point[2]=pt3;   
   m_pointcount=3; 
   
  this->CalcCenterPoint(); 
  this->CalcCenterRECT(); 
} 
 
CTriangle::CTriangle(POINT *point,int ptcount, LOGPEN &pen, LOGBRUSH &brush):CShape() 
{ 
  for(int nI=0;nI<ptcount;nI++) 
  { 
	  m_point[nI]=point[nI]; 
  } 
 
	m_penstruct.lopnColor=pen.lopnColor; 
	m_penstruct.lopnStyle=pen.lopnStyle; 
	m_penstruct.lopnWidth.x=pen.lopnWidth.x; 
	m_penstruct.lopnWidth.y=pen.lopnWidth.y; 
 
	m_brushstruct.lbColor=brush.lbColor; 
	m_brushstruct.lbStyle=brush.lbStyle; 
	m_brushstruct.lbHatch=brush.lbHatch; 
 
	m_pointcount=ptcount; 
	this->m_whatshape=Triangle; 
	this->CalcCenterPoint(); 
	this->CalcCenterRECT(); 
 
} 
 
CTriangle::CTriangle() 
{ 
 
} 
 
CTriangle::~CTriangle() 
{ 
 
} 
 
 
void CTriangle::CalcCenterPoint() 
{ 
    POINT curpoint; 
	curpoint.x=0; 
	curpoint.y=0; 
    for(int i=0;i<m_pointcount;i++) 
	{ 
      curpoint.x=curpoint.x+m_point[i].GetPoint().x; 
      curpoint.y=curpoint.y+m_point[i].GetPoint().y; 
	} 
  curpoint.x=curpoint.x/m_pointcount; 
  curpoint.y=curpoint.y/m_pointcount; 
  m_centerpoint=curpoint; 
} 
 
void CTriangle::CalcCenterRECT() 
{ 
    int maxX,maxY; 
	int minX,minY; 
	maxX=maxY=-1; 
	minX=minY=65536; 
	for(int i=0;i<m_pointcount;i++) 
	{ 
		if(m_point[i].GetPoint().x>maxX) 
		{ 
			maxX=m_point[i].GetPoint().x; 
		} 
		if(m_point[i].GetPoint().y>maxY) 
		{ 
			maxY=m_point[i].GetPoint().y; 
		} 
		if(m_point[i].GetPoint().x<minX) 
		{ 
			minX=m_point[i].GetPoint().x; 
		} 
		if(m_point[i].GetPoint().y<minY) 
		{ 
			minY=m_point[i].GetPoint().y; 
		} 
		 
	} 
	m_rect.left = minX-5; 
	m_rect.top  = minY-5; 
	m_rect.right = maxX+5; 
	m_rect.bottom = maxY+5; 
} 
 
void CTriangle::PreDraw(HDC &hdc) 
{ 
  POINT pt[100]; 
	for(int nI=0;nI<3;nI++) 
	{ 
      pt[nI]=m_copypoint[nI].GetPoint(); 
	}	 
    ::Polygon(hdc,pt,m_pointcount);    
} 
 
CShape * CTriangle::GetShapeCopy() 
{ 
  CShape * pcopy=new CTriangle; 
  *pcopy=*this;   //值拷贝 
   
  return pcopy ; 
 
} 
 
 
bool CTriangle::IsBound(CSuperPoint &curPt) 
{ 
 for(int nI=0;nI<m_pointcount-1;nI++) 
	 { 
		if(curPt.IsOnLine(m_point[nI],m_point[nI+1])) 
			return true; 
	 }	  
 return false; 
 
} 
 
void CTriangle::SetMark(HDC &hdc) 
{ 
   for(int nI=0;nI<m_pointcount;nI++) 
   { 
    m_point[nI].SetMark(hdc); 
   } 
	 
 
} 
 
void CTriangle::ClearMark(HDC &hdc) 
{ 
    for(int nI=0;nI<m_pointcount;nI++) 
	   { 
		m_point[nI].ClearMark(hdc); 
	   } 
			 
} 
 
void CTriangle::Lock(HDC &hdc) 
{ 
	this->m_selected = true;	//已锁定 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].SetLockMark(hdc); 
	} 
} 
 
void CTriangle::UnLock(HDC &hdc) 
{ 
	this->m_selected = false; 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].ClearLockMark(hdc); 
	} 
	 
} 
 
void CTriangle::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 CTriangle::DrawSelf(HDC &hdc) 
{ 
	POINT pt[3]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	}	 
	::Polygon(hdc,pt,m_pointcount); 
	 
	if(m_selected)   //如果这是一个被选中图形 在操作后的图形上打上标记 
	   { 
			for(int nI=0;nI<m_pointcount;nI++) 
			{ 
				m_point[nI].SetStatus(); 
			} 
		SetMark(hdc); 
		Lock(hdc); 
		} 
} 
 
void CTriangle::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[3]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	}	 
	::Polygon(hdc,pt,m_pointcount); 
	 
} 
 
void CTriangle::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 CTriangle::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 CTriangle::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 CTriangle::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); 
}