简体   繁体   English

C#中的多边形碰撞测试/多边形重叠测试 - 多边形中的非点

[英]Polygon Collision Testing / Polygon Overlap Test in C# - Not Point in Polygon

I am testing to determine if two polygons overlap. 我正在测试以确定两个多边形是否重叠。 I have developed a first version which does a simple point in polygon test (Fig 1). 我开发了第一个版本,它在多边形测试中做了一个简单的点(图1)。 However I am looking to revamp that method to deal with situations where no vertices of polygon A are in polygon B but their line segments overlap (Fig B). 但是,我希望改进这种方法来处理多边形A的顶点不在多边形B但是它们的线段重叠的情况(图B)。

Any help getting started would be greatly appreciated. 任何帮助入门将不胜感激。

在此输入图像描述

Here is an example with using Region: 以下是使用Region的示例:

  GraphicsPath grp = new GraphicsPath();

  // Create an open figure
  grp.AddLine(10, 10, 10, 50); // a of polygon
  grp.AddLine(10, 50, 50, 50); // b of polygon
  grp.CloseFigure();           // close polygon

  // Create a Region regarding to grp
  Region reg = new Region(grp);

Now you can use the Method Region.IsVisible to determine whether the region is in an Rectangle or Point. 现在,您可以使用方法Region.IsVisible来确定区域是在Rectangle还是Point中。

The solution: 解决方案:

I modified some code found here . 我在这里修改了一些代码。

private Region FindIntersections(List<PolyRegion> regions)
{
    if (regions.Count < 1) return null;

    Region region = new Region();
    for (int i = 0; i < regions.Count; i++)
    {
        using (GraphicsPath path = new GraphicsPath())
        {
            path.AddPath(regions[i].Path, false);
            region.Intersect(path);
        }
    }

    return region;
}

The result: 结果:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM