具体问题是:
n行,每行包含两个整数。第i行包含xi,yi - 按顺时针或逆时针顺序的多边形的第i个顶点。请注意,一侧可能会出现两个以上的顶点,如下图所示:
现在你需要判断顶点的多边形顺序是顺时针还是逆时针?
c ++代码是:
struct Node
{
int x, y;
Node operator-(Node node) const
{
Node t;
t.x = x - node.x;
t.y = y - node.y;
return t;
}
int operator*(Node node) const // I konow this is Cross-Product
{
return x * node.y - y * node.x;
}
}node[1000];
for (int i = 0; i < n; i++)
scanf("%d %d", &node[i].x, &node[i].y);
int tmp = 0;
node[n].x = node[0].x, node[n].y = node[0].y;
for (int i = 0; i < n; i++)
tmp += (node[i] * node[i + 1]);
if (tmp > 0)
it is counterclockwise order;
但我不明白代码,谁可以证明它?
答案 0 :(得分:1)
shoelace formula将给出任何多边形的面向区域。通过检查其符号,您可以确定方向。你所拥有的代码确实计算了两倍的面积,但由于标志是最重要的,这是无关紧要的。