c++ - 如何判断多边形顶点的顺序是顺时针还是逆时针?

标签 c++ algorithm math computational-geometry

具体问题是:

n lines, each line containing two integers. The i-th line contains xi, yi — the i-th vertex of the polygon in clockwise or counterclockwise order. Note that it is possible that more than two vertices appear in a side, such as the follow picture:

enter image description here

Now you need to judge that the vertices's order of polygon is clockwise or counterclockwise?

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;

但是我看不懂代码,谁能证明一下?

最佳答案

shoelace formula将给出任何多边形的定向区域。因此,通过检查其符号,您可以确定方向。您拥有的代码确实计算了两倍的面积,但由于符号是最重要的,所以这无关紧要。

关于c++ - 如何判断多边形顶点的顺序是顺时针还是逆时针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47617622/

相关文章:

c++ - 使用基类和派生类创建链表

c# - 将字符串列表随机分组

python - 为什么十进制乘法有点不准确?

python - 在pygame中制作平滑的轨道

c - 如何使用最少的系统资源在 ANSI C 中创建 "infinite"系列的交替符号?

c++ - 如何将 int 复制到 u_char*

C++ printf 字段宽度说明符 ‘.*’ 需要 int 而不是 size_t

c++ - 需要一个正态分布的随机数生成器

C 在文本中沿对角线查找字符串

确定是否可以通过翻转矩阵的行和列来达到给定二进制矩阵的算法