www.pudn.com > my2cadtest07261930.rar > PolygonTool.cpp, change:2006-07-23,size:3586b

```
#include "stdafx.h"
#include "PolygonTool.h"
#include "list.h"
#include "shape.h"
#include "Polygon.h"

extern CList<CShape *> m_list;

CPolygonTool::CPolygonTool(HWND hWnd,int numPoly):
CTool(hWnd),m_numPoly(numPoly),m_IsFirstOpe(true)
{

}

CPolygonTool::~CPolygonTool()
{

}

void CPolygonTool::Draw()
{
//图形确定后再绘一次

double angle = 2*PII/m_numPoly;   //通过边数求得于圆心的夹角
POINT firstPoint ;
firstPoint.x = m_endPT.GetPoint().x;
firstPoint.y = m_endPT.GetPoint().y;

for(int i = 0 ;  i < m_numPoly ; i++ )
{
POINT nextPoint;
if ( i + 1 == m_numPoly )  //多算一个点
{

nextPoint.x = m_endPT.GetPoint().x;
nextPoint.y = m_endPT.GetPoint().y;

}
else
{

nextPoint.x = (int) (m_begPT.GetPoint().x + (firstPoint.x - m_begPT.GetPoint().x) * cos(angle) -
(firstPoint.y - m_begPT.GetPoint().y) * sin(angle));
nextPoint.y = (int) (m_begPT.GetPoint().y + (firstPoint.x - m_begPT.GetPoint().x) * sin(angle) +
(firstPoint.y - m_begPT.GetPoint().y) * cos(angle));

}

::MoveToEx(m_hdc,firstPoint.x,firstPoint.y,NULL);
::LineTo(m_hdc,nextPoint.x,nextPoint.y);
firstPoint = nextPoint;

}

m_oldPT = m_endPT;

m_IsFirstOpe=true;
}

void CPolygonTool::PreDraw()
{
double angle = 2*PII/m_numPoly;   //通过边数求得于圆心的夹角
POINT firstPoint ;
if(m_IsFirstOpe==true)
{
m_IsFirstOpe=false;
}
else
{
firstPoint.x = m_oldPT.GetPoint().x;
firstPoint.y = m_oldPT.GetPoint().y;
for( int i = 0 ;  i < m_numPoly ; i++ )
{
POINT nextPoint;
if ( i + 1 == m_numPoly )  //多算一个点
{
nextPoint.x = m_oldPT.GetPoint().x;
nextPoint.y = m_oldPT.GetPoint().y;
}
else
{
nextPoint.x = (int) (m_begPT.GetPoint().x + (firstPoint.x - m_begPT.GetPoint().x) * cos(angle) -
(firstPoint.y - m_begPT.GetPoint().y) * sin(angle));
nextPoint.y = (int) (m_begPT.GetPoint().y + (firstPoint.x - m_begPT.GetPoint().x) * sin(angle) +
(firstPoint.y - m_begPT.GetPoint().y) * cos(angle));
}

::MoveToEx(m_hdc,firstPoint.x,firstPoint.y,NULL);
::LineTo(m_hdc,nextPoint.x,nextPoint.y);
firstPoint = nextPoint;
}
}

firstPoint.x = m_endPT.GetPoint().x;
firstPoint.y = m_endPT.GetPoint().y;

for(int i = 0 ;  i < m_numPoly ; i++ )
{
POINT nextPoint;
if ( i + 1 == m_numPoly )  //多算一个点
{

nextPoint.x = m_endPT.GetPoint().x;
nextPoint.y = m_endPT.GetPoint().y;

}
else
{

nextPoint.x = (int) (m_begPT.GetPoint().x + (firstPoint.x - m_begPT.GetPoint().x) * cos(angle) -
(firstPoint.y - m_begPT.GetPoint().y) * sin(angle));
nextPoint.y = (int) (m_begPT.GetPoint().y + (firstPoint.x - m_begPT.GetPoint().x) * sin(angle) +
(firstPoint.y - m_begPT.GetPoint().y) * cos(angle));

}

::MoveToEx(m_hdc,firstPoint.x,firstPoint.y,NULL);
::LineTo(m_hdc,nextPoint.x,nextPoint.y);
firstPoint = nextPoint;

}

m_oldPT = m_endPT;

}

void CPolygonTool::SaveSelf()
{

CShape* pPolygon=new CPolygon(m_begPT,m_endPT,m_numPoly);

pPolygon->SetLOGPEN(penattrib->GetPenStyle(false));
pPolygon->SetLOGBRUSH(penattrib->GetBrushStyle(false));