确定形状c ++中的点

时间:2013-11-06 05:12:52

标签: c++

我正在尝试确定一个形状内的一个点。

我找到了一个可以胜任这项工作的算法 http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

我用方形测试了算法。

正方形的总角= 4

但是在我的代码

之后它会给我一个错误的结果。(参见下面的输出结果)

Shape.h

#ifndef __Shape__Shape__
#define __Shape__Shape__

class Shape   {


private:
        int xArray[4];
        int yArray[4];
        int x;
        int y;
public:
        bool inPoly(int x,int y);
        void pointInShape();
};

#endif

Shape.cpp

#include "Shape.h"
#include <iostream>

bool Shape::inPoly(int x,int y) {

   xArray[0] = 1;
   xArray[1] = 1;
   xArray[2] = 3;
   xArray[3] = 3;

   yArray[0] = 1;
   yArray[1] = 3;
   yArray[2] = 3;
   yArray[3] = 1;

   int i, j, nvert = 4, c = 0;
   for (i = 0, j = nvert - 1; i < nvert; j = i++) {
       if ( ((yArray[i]>y) != (yArray[j]>y)) &&
           (x < (xArray[j]-xArray[i]) * (y-yArray[i]) / (yArray[j]-yArray[i]) + xArray[i]) )
           c = !c;
   }
   return c;
}


void Shape::pointInShape() {
    std::cout << "results" << std::endl;
    std::cout << inPoly(1,1) << std::endl;
    std::cout << inPoly(1,2) << std::endl;
    std::cout << inPoly(1,3) << std::endl;
    std::cout << inPoly(2,1) << std::endl;
    std::cout << inPoly(2,2) << std::endl;
    std::cout << inPoly(2,3) << std::endl;
    std::cout << inPoly(3,1) << std::endl;
    std::cout << inPoly(3,2) << std::endl;
    std::cout << inPoly(3,3) << std::endl;
}

的main.cpp

#include "Shape.h"
#include <iostream>
int main() {
   Shape shape;
   shape.pointInShape();
}

它返回此输出

   results
   1 <-- (means that 1,1 is is within polygon)
   1 <-- (means that 1,2 is is within polygon)
   0 <-- (means that 1,3 is is not within polygon)
   1 <-- (means that 2,1 is is within polygon)
   1 <-- (means that 2,2 is is within polygon)
   0 <-- (means that 2,3 is is not within polygon)
   0 <-- (means that 3,1 is is not within polygon)
   0 <-- (means that 3,2 is is not within polygon)
   0 <-- (means that 3,3 is is not within polygon)

右边正确的输出应该只返回2,2为真

正确输出

   results
   0 <-- (means that 1,1 is not within polygon)
   0 <-- (means that 1,2 is not within polygon)
   0 <-- (means that 1,3 is not within polygon)
   0 <-- (means that 2,1 is not within polygon)
   1 <-- (2,2 is is within polygon)
   0 <-- (means that 2,3 is is not within polygon)
   0 <-- (means that 3,1 is is not within polygon)
   0 <-- (means that 3,2 is is not within polygon)
   0 <-- (means that 3,3 is is not within polygon)

有任何意见/建议吗?

2 个答案:

答案 0 :(得分:3)

编写的PNPOLY代码实际上是用于浮动而不是整数。如果您将数组和顶点定义为浮点数,它将为您提供预期的结果。

1.0f和2.0f在float数学中不相邻,但是1和2在int数学中。

应该提示你的是功能原型

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)

此外行:

(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )

将在这些除法之后将你的整数截断为最接近的int。

如果你真的想使用整数,你必须用更多的空间来定义你的形状,比如100x100而不是2x2。

答案 1 :(得分:1)

根据您的链接和这一个:http://www.faqs.org/faqs/graphics/algorithms-faq/(请参阅第2.03节),您使用的算法仅适用于多边形内部/外部的点。边框上的点可能返回1或0

  

如果你想知道一个点何时正好在边界上,你需要另一个程序。这只是PNPOLY缺乏的众多功能之一;它也没有预测明天的天气。您可以自由扩展PNPOLY的源代码。