内容:
1.鼠标输入顶点,建立多边形数据结构。
2.判断多边形类型。
3.若是凹多边形,则切割成多个凸多边形。
首先,多边形数据结构:
#include <iostream>
using namespace std;
struct PolygonNode
{
public:
int x; //点x坐标
int y; //点y坐标
int SeqNode; //记录序号,从零开始
PolygonNode * next;
PolygonNode(int xPos, int yPos)
{
x = xPos;
y = yPos;
next = NULL;
}
~PolygonNode();
};
class Polygon
{
public:
PolygonNode *first;
PolygonNode *last;
PolygonNode *current;
int numNode; //记录顶点数
int concave; //记录第几个顶点为凹角
Polygon()
{
last = current = NULL;
}
void InsertNode(int xPos, int yPos); //插入顶点
void RemoveNode(); //移除顶点
void pCurrentGoto(int n); //current指针指向第n个顶点
int CrossProduct(int n); //向量叉乘,从第n个顶点起始的向量叉乘于下一个向量
};
void Polygon::InsertNode(int xPos, int yPos)
{
PolygonNode * newNode = new PolygonNode(xPos, yPos);
//空链表
if (current == NULL)
{
first = last = current = newNode;
numNode = 1;
last->SeqNode = 0;
}
else
{
last->next = newNode;
newNode->next = first;
last = newNode;
last->SeqNode = numNode;
numNode++;
}
};
void Polygon::RemoveNode()
{
//链头
if (current == first)
{
first = last->next = current->next;
current->next = NULL;
current = first;
}
//链尾
else if (current == last)
{
pCurrentGoto(current->SeqNode - 1);
cur