Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 是否有比PathGeometry.FillContainsWithDetail()更有效的方法检测多边形重叠/相交?_Wpf_Polygon_Polygons_Pathgeometry_Point In Polygon - Fatal编程技术网

Wpf 是否有比PathGeometry.FillContainsWithDetail()更有效的方法检测多边形重叠/相交?

Wpf 是否有比PathGeometry.FillContainsWithDetail()更有效的方法检测多边形重叠/相交?,wpf,polygon,polygons,pathgeometry,point-in-polygon,Wpf,Polygon,Polygons,Pathgeometry,Point In Polygon,我有一种方法占用了我25%的cpu时间。我每秒调用这个方法27000次。(是的,有很多电话,因为它经常更新)。我想知道是否有人知道一种更快的方法来检测两个多边形是否重叠。基本上,我必须检查屏幕上移动的物体和屏幕上静止的物体。我正在使用PathGeometry,下面的两个调用占用了我的程序所用cpu时间的25%。我正在传递的PointCollection对象仅包含表示多边形4个角的4个点。它们可能不会创建矩形区域,但所有点都是连接的。我想特拉帕佐德就是这个形状 这些方法很短,而且很容易实现,但是我

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

这些方法很短,而且很容易实现,但是我想如果我能让它比下面的代码运行得更快,我可能会选择更复杂的解决方案。有什么想法吗

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;
}
公共静态布尔点集合重叠(点集合区域1、点集合区域2)
{
pathGeometry1=GetPathGeometry(区域1);
PathGeometry pathGeometry2=GetPathGeometry(区域2);
返回pathGeometry1.FillContainsWithDetail(pathGeometry2)!=IntersectionDetail.Empty;
}
公共静态PathGeometry GetPathGeometry(点集合多边形角点)
{
列表路径段=新列表
{新的多段线段(多边形角,真)};
PathGeometry PathGeometry=新的PathGeometry();
pathGeometry.Figures.Add(新的PathFigure(polygonCorners[0],pathSegments,true));
返回路径几何;
}

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

我创建了一个特殊的测试应用程序来测试这个速度。您可以找到测试应用程序。如果你下载它,你可以在应用程序顶部看到一个复选框。勾选和取消勾选可在旧方式和新方式之间来回切换。该应用程序生成一组随机多边形,当这些多边形与另一个多边形相交时,其边界将变为白色。“重画”按钮左侧的数字允许您输入多边形的数量、边的最大长度和与正方形的最大偏移量(使它们不那么正方形,而形状更奇怪)。按“刷新”以清除并重新生成具有您输入的设置的新多边形

无论如何,下面是两种不同实现的代码。传入组成每个多边形的点集合。旧方法使用更少的代码,但比新方法慢4.6倍

哦,一张便条。新方法有几个对“PointIsInsidePolygon”的调用。这些是必要的,因为如果没有它,当一个多边形完全包含在另一个多边形中时,该方法返回false。但是PointIsInsidePolygon方法解决了这个问题

希望这些都能帮助其他人完成多边形截取和重叠

老办法(慢4.6倍。是的,确实慢4.6倍):

public static bool PointCollectionsOverlap\u Slow(点收集区域1、点收集区域2)
{
pathGeometry1=GetPathGeometry(区域1);
PathGeometry pathGeometry2=GetPathGeometry(区域2);
bool result=pathGeometry1.FillContainsWithDetail(pathGeometry2)!=IntersectionDetail.Empty;
返回结果;
}
公共静态PathGeometry GetPathGeometry(点集合多边形角点)
{
List pathSegments=新列表{new PolylineSegments(polygonCorners,true)};
PathGeometry PathGeometry=新的PathGeometry();
pathGeometry.Figures.Add(新的PathFigure(polygonCorners[0],pathSegments,true));
返回路径几何;
}
新方法(快4.6倍。是的,真的快4.6倍):

公共静态布尔点集合重叠\u Fast(点集合区域1、点集合区域2)
{
对于(int i=0;ireturn(t>=0)&(u>=0)&&(t)如果你有这么多的对象,你总是针对每个形状测试每个形状吗?因为首先,你不需要针对所有形状测试所有形状,设置{a,b}1)a->a,2)a->b3)b->a,4)b->a;4个可以简化为1的测试(只是为了解释我的意思)。否则,为了简单起见,您可以将形状排序为四叉树或简单网格,并且只有在两个形状共享同一单元格时才使用昂贵的重叠测试。您还可以在第二种方法中缓存PathGeometry对象,至少对于静止的形状。最多有100个
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;
}
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);
}