【问题标题】:How to determine ring points' orientation correctly?如何正确确定环点的方向?
【发布时间】:2011-12-13 10:15:03
【问题描述】:

基于How to determine if a list of polygon points are in clockwise order?

我想出了以下代码:

bool PointsClockwise(const std::vector<MyPoint>& points)
{  
  double sum = 0.0;
  for(size_t i = 0; i < points.size() - 1; ++i)
    sum += (points[i+1].x()-points[i].x()) * (points[i+1].y()+points[i].y());

  return sum > 0.0;
}

但是,在某些情况下,这似乎有错误的结果。以下面的环为例:

LINESTRING(0 119,0 60,694 70,704 72,712 77,719 83,723 92,725 102,723 111,719 120,712 126,703 130)

按逆时针顺序,但函数返回true。

谢谢!

【问题讨论】:

    标签: math geometry polygon


    【解决方案1】:

    您需要包含案例i == points.size() - 1,但要做到这一点,您需要在循环中进行一些模运算,或者分离出最后一次迭代。实际上,只需将 sum 初始化为最后一次迭代:

    double sum = (points[0].x() - points[points.size() - 1].x())
      * (points[0].y() + points[points.size() - 1].y());
    

    并在i &lt; points.size() - 1结束迭代

    【讨论】:

    • 啊。所以不是。修改我的答案。
    【解决方案2】:

    您错过了求和中的一个线段 - 即连接最后一个点和第一个点的线段。 试试看:

    bool PointsClockwise(const std::vector<MyPoint>& points)
    {  
      double sum = 0.0;
      for(size_t i = 0; i < points.size() - 1; ++i)
        sum += (points[i+1].x()-points[i].x()) * (points[i+1].y()+points[i].y());
    
      sum += (points[0].x()-points[points.size()-1].x()) * (points[0].y()+points[points.size()-1].y());
    
      return sum > 0.0;
    }
    

    【讨论】:

    • 对了,忘记关门了:)谢谢。
    猜你喜欢
    • 2017-12-13
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多