C++ 多边形内的检查点

C++ 多边形内的检查点,c++,polygon,C++,Polygon,我尝试检查多边形中的一个有效点,它返回0。从 在大多数情况下使用的两种方法(我知道的两种)是交叉数和绕组数它们都不受多边形/点坐标的符号的影响。所以它一定是你代码中的一个bug 为了完整性,我将代码放在一个交叉数测试中,这似乎是您试图在代码中执行的操作 float x[3] = { 0.16, 1.2, -10 }; float y[3] = { 1.8, 10, -5.5 }; //点由其坐标{int x,y;}定义 //isLeft():测试一个点是否在无限直线的左|右|上。 //输入:三

我尝试检查多边形中的一个有效点,它返回0。

在大多数情况下使用的两种方法(我知道的两种)是交叉数绕组数它们都不受多边形/点坐标的符号的影响。所以它一定是你代码中的一个bug

为了完整性,我将代码放在一个交叉数测试中,这似乎是您试图在代码中执行的操作

float x[3] = { 0.16, 1.2, -10 };
float y[3] = { 1.8, 10, -5.5 };
//点由其坐标{int x,y;}定义
//isLeft():测试一个点是否在无限直线的左|右|上。
//输入:三个点P0、P1和P2
//返回:通过P0和P1的P2线左侧大于0
//=0表示线路上的P2

//请注意,如果多边形N和N+1具有相同的y值,则此测试可能无法计算vt。例如:确定点是否位于轴对齐的正方形内


您需要处理y上的平行线

您的测试点是什么(对于给定的示例)?这段代码和那个示例工作只针对测试点(-8.0,-4.0)和(0,6)进行查找。确切地说,负坐标在这里似乎不是问题。你知道这个算法是如何工作的吗?float x[3]={-10,0.17,10};浮动y[3]={-5.56,1.85,1.69};测试点是(2.5,-1.6),返回0。非常感谢。解决了我的问题。
float x[3] = { 0.16, 1.2, -10 };
float y[3] = { 1.8, 10, -5.5 };
// a Point is defined by its coordinates {int x, y;}

// isLeft(): tests if a point is Left|On|Right of an infinite line.
//    Input:  three points P0, P1, and P2
//    Return: >0 for P2 left of the line through P0 and P1
//            =0 for P2  on the line
//            <0 for P2  right of the line
//    See: Algorithm 1 "Area of Triangles and Polygons"
inline int isLeft( Point P0, Point P1, Point P2 )
{
    return ( (P1.x - P0.x) * (P2.y - P0.y) - (P2.x -  P0.x) * (P1.y - P0.y) );
}
//===================================================================

// cn_PnPoly(): crossing number test for a point in a polygon
//      Input:   P = a point,
//               V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
//      Return:  0 = outside, 1 = inside
// This code is patterned after [Franklin, 2000]
int cn_PnPoly( Point P, Point* V, int n )
{
    int    cn = 0;    // the  crossing number counter

    // loop through all edges of the polygon
    for (int i=0; i<n; i++) {    // edge from V[i]  to V[i+1]
       if (((V[i].y <= P.y) && (V[i+1].y > P.y))     // an upward crossing
        || ((V[i].y > P.y) && (V[i+1].y <=  P.y))) { // a downward crossing
            // compute  the actual edge-ray intersect x-coordinate
            float vt = (float)(P.y  - V[i].y) / (V[i+1].y - V[i].y);
            if (P.x <  V[i].x + vt * (V[i+1].x - V[i].x)) // P.x < intersect
                 ++cn;   // a valid crossing of y=P.y right of P.x
        }
    }
    return (cn&1);    // 0 if even (out), and 1 if  odd (in)

}
//===================================================================