如下代码,很多地方并不合理,可能会有bug,或者跟实际需求有偏差,仅供参考:
#pragma once
#include <list>
// 点类
class SPoint
{
public:
double m_dX; // x坐标
double m_dY; // y坐标
void Move(const SPoint& rPoint)
{
m_dX += rPoint.m_dX;
m_dY += rPoint.m_dY;
}
};
// 多边形类
class CPolygon
{
public:
CPolygon()
{
}
~CPolygon()
{
Clear();
}
// 清空所有点
void Clear()
{
for (std::list<SPoint*>::const_iterator it = m_listPoint.begin(); it != m_listPoint.end(); ++it)
{
delete *it;
}
m_listPoint.clear();
}
// 插入点
void InsertPoint(const SPoint& rPoint)
{
SPoint* pPoint = new SPoint(rPoint);
m_listPoint.push_back(pPoint);
}
// 删除某个点 todo:查找要删除的点时效率太低,顶点少的话影响不大
bool RemovePoint(const SPoint& rPoint)
{
for (std::list<SPoint*>::iterator it = m_listPoint.begin(); it != m_listPoint.end(); ++it)
{
if ((rPoint.m_dX == (*it)->m_dX) && (rPoint.m_dY == (*it)->m_dY)) // todo:这个比较可能有问题
{
delete *it;
m_listPoint.erase(it);
return true;
}
}
return false;
}
// 移动 水平和垂直移动距离由rPoint确定
void Move(const SPoint& rPoint)
{
for (std::list<SPoint*>::iterator it = m_listPoint.begin(); it != m_listPoint.end(); ++it)
{
(**it).Move(rPoint);
}
}
// 读取顶点列表
std::list<SPoint*> GetPoints() const
{
return m_listPoint;
}
private:
std::list<SPoint*> m_listPoint; // 顶点坐标集合
};
[此贴子已经被作者于2017-5-5 17:11编辑过]