有没有比PathGeometry.FillContainsWithDetail()检测多边形重叠/交叉更有效的方法?

Cur*_*tis 5 wpf polygon polygons pathgeometry point-in-polygon

我有一个方法吞噬了我的CPU时间的25%.我称这种方法每秒约27,000次.(是的,很多电话,因为它经常更新).我想知道是否有人知道更快的方法来检测2个多边形是否重叠.基本上,我必须检查屏幕上的移动物体与屏幕上的静止物体.我正在使用PathGeometry,下面的两个调用占用了我程序使用的cpu时间的25%.我传递的PointCollection对象只包含4个点,表示多边形的4个角.它们可能不会创建矩形区域,但所有点都是连接的.我猜一个trapazoid就是形状.

这些方法很简单,并且很容易实现,但我想如果我能让它比下面的代码更快地运行,我可能会选择更复杂的解决方案.有任何想法吗?

public static bool PointCollectionsOverlap(PointCollection area1, PointCollection area2)
{
    PathGeometry pathGeometry1 = GetPathGeometry(area1);
    PathGeometry pathGeometry2 = GetPathGeometry(area2);
    return pathGeometry1.FillContainsWithDetail(pathGeometry2) != IntersectionDetail.Empty;
}

public static PathGeometry GetPathGeometry(PointCollection polygonCorners)
{
    List<PathSegment> pathSegments = new List<PathSegment> 
                                         { new PolyLineSegment(polygonCorners, true) };
    PathGeometry pathGeometry = new PathGeometry();
    pathGeometry.Figures.Add(new PathFigure(polygonCorners[0], pathSegments, true));
    return pathGeometry;
}
Run Code Online (Sandbox Code Playgroud)

Cur*_*tis 7

好的,经过大量研究并找到了许多部分答案,但没有一个完全回答这个问题,我找到了一种更快的方法,它实际上比旧方法快4.6倍.

我创建了一个特殊的测试应用来测试速度.你可以在这里找到测试应用程序.如果您下载它,您可以在应用顶部看到一个复选框.检查并取消选中它以旧方式和新方式之间来回切换.该应用程序生成一堆随机多边形,当多边形与另一个多边形相交时,它们的边框会变为白色.到"重绘"按钮左边的数字是允许你进入多边形的数量,侧面的最大长度,和最大的广场偏移量(以使他们少方,更奇形).按"刷新"以使用您输入的设置清除和重新生成新多边形.

无论如何,这是两个不同实现的代码.您传入构成每个多边形的点的集合.旧方法使用较少的代码,但比新方式慢4.6倍.

哦,快速说明一下.新方式有几个调用'PointIsInsidePolygon'.这些是必要的,因为没有它,当一个多边形完全包含在不同的多边形中时,该方法返回false.但PointIsInsidePolygon方法修复了这个问题.

希望这一切都有助于其他人使用多边形截距和重叠.

旧路(慢4.6倍. 真的慢了4.6倍):

public static bool PointCollectionsOverlap_Slow(PointCollection area1, PointCollection area2)
{
    PathGeometry pathGeometry1 = GetPathGeometry(area1);
    PathGeometry pathGeometry2 = GetPathGeometry(area2);
    bool result = pathGeometry1.FillContainsWithDetail(pathGeometry2) != IntersectionDetail.Empty;
    return result;
}

public static PathGeometry GetPathGeometry(PointCollection polygonCorners)
{
    List<PathSegment> pathSegments = new List<PathSegment> { new PolyLineSegment(polygonCorners, true) };
    PathGeometry pathGeometry = new PathGeometry();
    pathGeometry.Figures.Add(new PathFigure(polygonCorners[0], pathSegments, true));
    return pathGeometry;
}
Run Code Online (Sandbox Code Playgroud)

新方式(快4.6倍. 真的快4.6倍):

public static bool PointCollectionsOverlap_Fast(PointCollection area1, PointCollection area2)
{
    for (int i = 0; i < area1.Count; i++)
    {
        for (int j = 0; j < area2.Count; j++)
        {
            if (lineSegmentsIntersect(area1[i], area1[(i + 1) % area1.Count], area2[j], area2[(j + 1) % area2.Count]))
            {
                return true;
            }
        }
    }

    if (PointCollectionContainsPoint(area1, area2[0]) ||
        PointCollectionContainsPoint(area2, area1[0]))
    {
        return true;
    }

    return false;
}

public static bool PointCollectionContainsPoint(PointCollection area, Point point)
{
    Point start = new Point(-100, -100);
    int intersections = 0;

    for (int i = 0; i < area.Count; i++)
    {
        if (lineSegmentsIntersect(area[i], area[(i + 1) % area.Count], start, point))
        {
            intersections++;
        }
    }

    return (intersections % 2) == 1;
}

private static double determinant(Vector vector1, Vector vector2)
{
    return vector1.X * vector2.Y - vector1.Y * vector2.X;
}

private static bool lineSegmentsIntersect(Point _segment1_Start, Point _segment1_End, Point _segment2_Start, Point _segment2_End)
{
    double det = determinant(_segment1_End - _segment1_Start, _segment2_Start - _segment2_End);
    double t = determinant(_segment2_Start - _segment1_Start, _segment2_Start - _segment2_End) / det;
    double u = determinant(_segment1_End - _segment1_Start, _segment2_Start - _segment1_Start) / det;
    return (t >= 0) && (u >= 0) && (t <= 1) && (u <= 1);
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢你做的这些。我能够将其移植到 javascript。为此,我借用了这个 Point 类 http://upshots.org/javascript/javascript-point-class。Javascript Polyoverlap 在此处创建为要点:https://gist.github.com/3753426 (2认同)