?? polyobj.cpp
字號:
#include "StdAfx.h"
#include "polyobj.h"
#include "visdrawview.h"
#include "visdrawdoc.h"
#include ".\polyobj.h"
//#include <math.h>
IMPLEMENT_SERIAL(CPolyObj, CFigureObj, 0)
CFloatPoint::CFloatPoint(void)
{
pointx = 0.0;
pointy = 0.0;
}
CFloatPoint::~CFloatPoint(void)
{
}
//////////////////////////////////////////////////////
CPolyObj::CPolyObj(void)
{
}
CPolyObj::~CPolyObj(void)
{
//if (m_points != NULL)
// delete[] m_points;
if(m_floatpoints != NULL)
delete[] m_floatpoints;
}
CPolyObj::CPolyObj(const CRect& position)
: CFigureObj(position)
{
ASSERT_VALID(this);
m_nShape = polygonshape;
m_points = NULL;
m_floatpoints = NULL;
m_nPoints = 0;
m_nAllocPoints = 0;
}
void CPolyObj::Serialize(CArchive& ar)
{
ASSERT_VALID(this);
int i;
//調(diào)用基類串行化
CFigureObj::Serialize( ar );
if( ar.IsStoring() )
{
ar << (WORD) m_nShape;
ar << (WORD) m_nPoints;
ar << (WORD) m_nAllocPoints;
for (i = 0;i< m_nPoints; i++)
ar << m_floatpoints[i].pointx << m_floatpoints[i].pointy;
}
else
{
WORD wTemp;
ar >> wTemp; m_nShape = (Shape)wTemp;
ar >> wTemp; m_nPoints = wTemp;
ar >> wTemp; m_nAllocPoints = wTemp;
m_floatpoints = new CFloatPoint[m_nAllocPoints];
for (i = 0;i < m_nPoints; i++)
ar >> m_floatpoints[i].pointx >> m_floatpoints[i].pointy;
}
}
void CPolyObj::Draw(CVisDrawView* pView, CDC* pDC)
{
ASSERT_VALID(this);
CBrush brush;
if (!brush.CreateBrushIndirect(&m_logbrush))
return;
CPen pen;
if (!pen.CreatePenIndirect(&m_logpen))
return;
CBrush* pOldBrush;
CPen* pOldPen;
if (m_bBrush)
pOldBrush = pDC->SelectObject(&brush);
else
pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
if (m_bPen)
pOldPen = pDC->SelectObject(&pen);
else
pOldPen = (CPen*)pDC->SelectStockObject(NULL_PEN);
//創(chuàng)建一個臨時CPoint對象數(shù)組,保存多邊形邏輯頂點邏輯坐標
CPoint* m_points = new CPoint[m_nPoints];
double ptx,pty;
CPoint pt;
//把所有頂點的世界坐標轉(zhuǎn)化為邏輯坐標
for(int i = 0;i<m_nPoints;i++)
{
ptx = m_floatpoints[i].pointx;
pty = m_floatpoints[i].pointy;
pView->WorldToClient(pt,ptx,pty);
m_points[i].x=pt.x;
m_points[i].y =pt.y;
}
switch(m_nShape)
{
case polygonshape: //繪制多邊形
pDC->Polygon(m_points, m_nPoints);
break;
case polylineshape: //繪制多邊線
pDC->Polyline(m_points, m_nPoints);
break;
}
//刪除臨時頂點數(shù)組
delete[] m_points;
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
//計算多邊形圖形元邊界矩形,以邏輯坐標表示
CRect CPolyObj::CalcBounds(CVisDrawView* pView)
{
ASSERT_VALID(this);
//創(chuàng)建臨時頂點邏輯坐標數(shù)組
CPoint* m_points = new CPoint[m_nPoints];
double ptx,pty;
CPoint pt;
//把所有頂點的世界坐標轉(zhuǎn)化為邏輯坐標
for(int i = 0;i<m_nPoints;i++)
{
ptx = m_floatpoints[i].pointx;
pty = m_floatpoints[i].pointy;
pView->WorldToClient(pt,ptx,pty);
m_points[i].x=pt.x;
m_points[i].y =pt.y;
}
//找出所有頂點中x,y的最大與最小值,并由矩形對象bounds保存
CRect bounds(m_points[0], CSize(0, 0));
for (int i = 1; i < m_nPoints; ++i)
{
if (m_points[i].x < bounds.left)
bounds.left = m_points[i].x;
if (m_points[i].x > bounds.right)
bounds.right = m_points[i].x;
if (m_points[i].y < bounds.top)
bounds.top = m_points[i].y;
if (m_points[i].y > bounds.bottom)
bounds.bottom = m_points[i].y;
}
//刪除臨時頂點數(shù)組
delete[] m_points;
//保存多邊形邊界矩形
m_position = bounds;
return bounds;
}
//添加多邊形節(jié)點
void CPolyObj::AddPoint(const CPoint& point, CVisDrawView* pView)
{
ASSERT_VALID(this);
//參數(shù)point是邏輯坐標,需要轉(zhuǎn)化為實際坐標
double ptx,pty;
pView->ClientToWorld(point,ptx,pty);
//動態(tài)創(chuàng)建數(shù)組
if (m_nPoints == m_nAllocPoints)
{
CFloatPoint* newfloatPoints = new CFloatPoint[m_nAllocPoints + 1];
if (m_floatpoints != NULL)
{
memcpy(newfloatPoints, m_floatpoints, sizeof(CFloatPoint) * m_nAllocPoints);
delete[] m_floatpoints;
}
m_floatpoints = newfloatPoints;
m_nAllocPoints += 1;
}
//如果是第一個頂點或不重合的頂點,添加到數(shù)組中去
if(m_nPoints == 0 ||((m_floatpoints[m_nPoints - 1].pointx != ptx)
&&(m_floatpoints[m_nPoints - 1].pointy != pty)))
{
m_floatpoints[m_nPoints].pointx = ptx;
m_floatpoints[m_nPoints].pointy = pty;
m_nPoints++;
//如果頂點數(shù)不為0,計算多邊形邊界矩形
if(m_nPoints != 0)
CalcBounds(pView);
//修改文檔標致
pView->GetDocument()->SetModifiedFlag();
}
}
//返回手柄個數(shù)
int CPolyObj::GetHandleCount()
{
ASSERT_VALID(this);
return m_nPoints;
}
// 返回手柄中心邏輯坐標
CPoint CPolyObj::GetHandle(CVisDrawView* pView, int nHandle)
{
ASSERT_VALID(this);
ASSERT(nHandle >= 1 && nHandle <= m_nPoints);
double ptx,pty;
CPoint pt;
//把頂點世界坐標轉(zhuǎn)化為邏輯坐標
ptx = m_floatpoints[nHandle - 1].pointx;
pty = m_floatpoints[nHandle - 1].pointy;
pView->WorldToClient(pt,ptx,pty);
return pt;
}
//移動手柄
void CPolyObj::MoveHandleTo(int nHandle, CPoint point, CVisDrawView* pView)
{
ASSERT_VALID(this);
ASSERT(nHandle >= 1 && nHandle <= m_nPoints);
//把鼠標邏輯坐標轉(zhuǎn)化為世界坐標
double pointx,pointy;
pView->ClientToWorld(point,pointx,pointy);
if (m_floatpoints[nHandle - 1].pointx == pointx &&
m_floatpoints[nHandle - 1].pointy == pointy)
return;
m_floatpoints[nHandle - 1].pointx = pointx;
m_floatpoints[nHandle - 1].pointy = pointy;
CalcBounds(pView);
}
// delta為邏輯坐標
void CPolyObj::MoveTo(CPoint delta, CVisDrawView* pView)
{
ASSERT_VALID(this);
//把delta轉(zhuǎn)化為世界坐標
double pointx,pointy;
pointx = pView->ClientToWorld(delta.x);
pointy = -pView->ClientToWorld(delta.y);
//修改所有頂點坐標
// double ptx,pty;
// CPoint pt;
//把所有頂點的世界坐標轉(zhuǎn)化為邏輯坐標
for(int i = 0;i<m_nPoints;i++)
{
m_floatpoints[i].pointx += pointx;
m_floatpoints[i].pointy += pointy;
}
//重新計算邊界矩形
CalcBounds(pView);
//修改文檔標志
m_pDocument->SetModifiedFlag();
}
//BOOL CPolyObj::IsSelected(CVisDrawView* pView,const CPoint& point)
//{
// ASSERT_VALID(this);
//參數(shù)point是鼠標的邏輯坐標
//double distance,ptx,pty;
//int nSelectDistance;
//CPoint local,StartPoint,EndPoint;
//local = point;
//鼠標點的設(shè)備坐標
//pView->DocToClient(local);
//識別精度值
//nSelectDistance = pView->GetDocument()->GetSetectDistance()/2;
//平移所有頂點坐標
//for(int i=0;i<m_nPoints;i++)
//{
// int j=i+1;
// if(j==m_nPoints) j=0;
//把頂點實際坐標轉(zhuǎn)化為邏輯坐標
// ptx=m_floatpoints[i].pointx;
// pty=m_floatpoints[i].pointy;
// pView->WorldToClient(StartPoint,ptx,pty);
//把頂點實際坐標轉(zhuǎn)化為邏輯坐標
// ptx=m_floatpoints[i].pointx;
// pty=m_floatpoints[i].pointy;
// pView->WorldToClient(EndPoint,ptx,pty);
// pView->DocToClient(StartPoint);
// pView->DocToClient(EndPoint);
//鼠標點到直線的距離
// distance =(int)PointToLine(StartPoint,EndPoint,local);
//根據(jù)拾取條件判斷圖元是否被拾取
// if(distance < nSelectDistance)
// return TRUE;
//}
//return false;
//}
//double CPolyObj::PointToLine(CPoint nStartPt, CPoint nEndPt, CPoint pt)
//{
//int A,B,C;
//double distance;
//計算直線參數(shù)
//A = nStartPt.y - nEndPt.y;
//B = nEndPt.x - nStartPt.x;
//C = nStartPt.x*nEndPt.y - nEndPt.x*nStartPt.y;
//計算點到直線距離
//distance = (A*pt.x + B*pt.y + C)*(A*pt.x + B*pt.y + C)/(A*A + B*B);
//distance = sqrt(distance);
//return distance;
//}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -