【问题标题】:How to judge that the vertices's order of polygon is clockwise or counterclockwise?如何判断多边形顶点的顺序是顺时针还是逆时针?
【发布时间】:2017-12-03 10:52:51
【问题描述】:

具体问题是:

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;

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

【问题讨论】:

  • “每一行包含两个整数”是什么意思?一条线由两个端点定义,每个端点由两个数字定义。你的意思是每个 point 都有积分坐标吗? (这就是您的代码所显示的内容。)
  • @Rory Daulton 是的。每个点都有积分坐标。
  • @RoryDaulton:我假设问题描述中的“行”是指文本行,即行。
  • @MvG:啊,是的,OP省略了问题描述的开头,“行”是一个输入行。这就说得通了。谢谢。

标签: c++ algorithm math computational-geometry


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多