多边形以 Vector2I 对象列表的形式给出(二维、整数坐标)。如何测试给定点是否在内部?我在网上找到的所有实现都因一些微不足道的反例而失败。编写正确的实现似乎确实很难。语言并不重要,因为我会自己移植。

有帮助吗?

解决方案

如果它是凸的,一个简单的方法来检查它是该点上的所有段的相同侧铺设(如果遍历以相同的顺序)。

可以检查容易地与叉积(因为它正比于段与点之间所形成的角的余弦,那些具有正号会躺在右侧和那些具有负号在左侧)。

下面是在Python代码:

RIGHT = "RIGHT"
LEFT = "LEFT"

def inside_convex_polygon(point, vertices):
    previous_side = None
    n_vertices = len(vertices)
    for n in xrange(n_vertices):
        a, b = vertices[n], vertices[(n+1)%n_vertices]
        affine_segment = v_sub(b, a)
        affine_point = v_sub(point, a)
        current_side = get_side(affine_segment, affine_point)
        if current_side is None:
            return False #outside or over an edge
        elif previous_side is None: #first segment
            previous_side = current_side
        elif previous_side != current_side:
            return False
    return True

def get_side(a, b):
    x = x_product(a, b)
    if x < 0:
        return LEFT
    elif x > 0: 
        return RIGHT
    else:
        return None

def v_sub(a, b):
    return (a[0]-b[0], a[1]-b[1])

def x_product(a, b):
    return a[0]*b[1]-a[1]*b[0]

其他提示

在光线投射或绕线方法是最常见的为这个问题。参阅对于细节维基百科文章

此外,检查出了这个页面充分证明溶液在℃。

如果多边形是凸多边形,则在 C# 中,以下实现“测试是否总是在同一侧" 方法,最多运行 O(n 个多边形点):

public static bool IsInConvexPolygon(Point testPoint, List<Point> polygon)
{
    //Check if a triangle or higher n-gon
    Debug.Assert(polygon.Length >= 3);

    //n>2 Keep track of cross product sign changes
    var pos = 0;
    var neg = 0;

    for (var i = 0; i < polygon.Count; i++)
    {
        //If point is in the polygon
        if (polygon[i] == testPoint)
            return true;

        //Form a segment between the i'th point
        var x1 = polygon[i].X;
        var y1 = polygon[i].Y;

        //And the i+1'th, or if i is the last, with the first point
        var i2 = i < polygon.Count - 1 ? i + 1 : 0;

        var x2 = polygon[i2].X;
        var y2 = polygon[i2].Y;

        var x = testPoint.X;
        var y = testPoint.Y;

        //Compute the cross product
        var d = (x - x1)*(y2 - y1) - (y - y1)*(x2 - x1);

        if (d > 0) pos++;
        if (d < 0) neg++;

        //If the sign changes, then point is outside
        if (pos > 0 && neg > 0)
            return false;
    }

    //If no change in direction, then on same side of all segments, and thus inside
    return true;
}

在OpenCV中的pointPolygonTest功能“确定点是否是轮廓内部,外部,或位于边缘”: http://docs.opencv.org/modules/imgproc /doc/structural_analysis_and_shape_descriptors.html?highlight=pointpolygontest#pointpolygontest

FORTRAN的回答几乎是为我工作,除了我发现我有翻译的多边形,这样你正在测试的点是一样的起源。下面是我写的,使这项工作的JavaScript的:

function Vec2(x, y) {
  return [x, y]
}
Vec2.nsub = function (v1, v2) {
  return Vec2(v1[0]-v2[0], v1[1]-v2[1])
}
// aka the "scalar cross product"
Vec2.perpdot = function (v1, v2) {
  return v1[0]*v2[1] - v1[1]*v2[0]
}

// Determine if a point is inside a polygon.
//
// point     - A Vec2 (2-element Array).
// polyVerts - Array of Vec2's (2-element Arrays). The vertices that make
//             up the polygon, in clockwise order around the polygon.
//
function coordsAreInside(point, polyVerts) {
  var i, len, v1, v2, edge, x
  // First translate the polygon so that `point` is the origin. Then, for each
  // edge, get the angle between two vectors: 1) the edge vector and 2) the
  // vector of the first vertex of the edge. If all of the angles are the same
  // sign (which is negative since they will be counter-clockwise) then the
  // point is inside the polygon; otherwise, the point is outside.
  for (i = 0, len = polyVerts.length; i < len; i++) {
    v1 = Vec2.nsub(polyVerts[i], point)
    v2 = Vec2.nsub(polyVerts[i+1 > len-1 ? 0 : i+1], point)
    edge = Vec2.nsub(v1, v2)
    // Note that we could also do this by using the normal + dot product
    x = Vec2.perpdot(edge, v1)
    // If the point lies directly on an edge then count it as in the polygon
    if (x < 0) { return false }
  }
  return true
}

我知道的方式是类似的东西。

你选择一个点某处可能远离几何多边形之外。 然后你画从这个点线。我的意思是创建与这两个点的直线方程。

然后在此多边形的每一行,则检查它们是否相交。

他们的相交线的数目的总和给你它里面或没有。

如果它是奇数:内部

如果它甚至:外

或从写的书看的人 - 几何页

此页面,他讨论了为什么缠绕规则通常比射线交叉更好。

编辑 - 抱歉,这并不是 Jospeh奥罗克谁写的优秀的书计算几何用C ,这是保罗·伯克但仍然是一个非常非常的几何算法良好来源。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top