加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 合并触摸多边形,给出错误的结果

发布时间:2020-12-15 07:52:58 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一段代码,给出一个多边形列表(定义为IntPoints列表列表)检查是否有任何触摸,如果是,则将它们合并为一个多边形.为了做到这一点,我已经尝试了以下两种方法: ListListIntPoint output=new ListListIntPoint();output = Clipper.SimplifyPolygons
我正在尝试编写一段代码,给出一个多边形列表(定义为IntPoints列表列表)检查是否有任何触摸,如果是,则将它们合并为一个多边形.为了做到这一点,我已经尝试了以下两种方法:
List<List<IntPoint>> output=new List<List<IntPoint>>();
output = Clipper.SimplifyPolygons(input,PolyFillType.pftPositive);

Clipper c = new Clipper();
c.AddPaths(input,PolyType.ptClip,true);
c.Execute(ClipType.ctUnion,output);

现在这两个都很容易将多边形合并在一起,但它们有点过于粗糙,因为任何多边形开放空间都会被忽略,而开放区域只是组合成一个多边形,这意味着:

发生.现在这显然是错误的,因为这两个多边形不相互接触.两种方法都会出现相同的结果.结果与输入相同.知道如何解决这个问题吗? sollution不必使用限幅器库(我没有与它结合)但我确实需要使用由点列表定义的多边形的东西输入是List>其中Intpoint只是一个包含x和y的结构.

编辑我注意到当另一个多边形内没有多边形时也会出现这个问题,所以解决方案总是“填充”
编辑编辑:这里也是输入可能是什么样的例子

input[0][0]
{ClipperLib.IntPoint}
    X: -724
    Y: -472
input[0][1]
{ClipperLib.IntPoint}
    X: 428
    Y: -472
input[0][2]
{ClipperLib.IntPoint}
    X: 428
    Y: -472
  input[0][3]
{ClipperLib.IntPoint}
    X: 428
    Y: 632
input[0][4]
{ClipperLib.IntPoint}
    X: 428
    Y: 632
input[0][5]
{ClipperLib.IntPoint}
    X: -724
    Y: 632
input[0][6]
{ClipperLib.IntPoint}
    X: -724
    Y: 632
input[0][7]
{ClipperLib.IntPoint}
    X: -724
    Y: -472
input[0][8]
{ClipperLib.IntPoint}
    X: -88
    Y: -218
input[0][9]
{ClipperLib.IntPoint}
    X: -107
    Y: -218
input[0][10]
{ClipperLib.IntPoint}
    X: -107
    Y: -218
input[0][11]
{ClipperLib.IntPoint}
    X: -107
    Y: -209
input[0][12]
{ClipperLib.IntPoint}
    X: -107
    Y: -209
input[0][13]
{ClipperLib.IntPoint}
    X: -320
    Y: -172
input[0][14]
{ClipperLib.IntPoint}
    X: -320
    Y: -172
input[0][15]
{ClipperLib.IntPoint}
    X: -320
    Y: 132
input[0][16]
{ClipperLib.IntPoint}
    X: -320
    Y: 132
input[0][17]
{ClipperLib.IntPoint}
    X: -88
    Y: 173
input[0][18]
{ClipperLib.IntPoint}
    X: -88
    Y: 173
input[0][19]
{ClipperLib.IntPoint}
    X: -88
    Y: -201
input[0][20]
{ClipperLib.IntPoint}
    X: -88
    Y: -201
input[0][21]
{ClipperLib.IntPoint}
    X: -88
    Y: -218

这个描述的输入是一个正方形,其中有一个孔.

解决方法

需要有PolyType.ptSubject(代码中缺少)和PolyType.ptClip在执行之前添加到Clipper.您还需要选择将产生所需结果的ClipType,如下所示:
private void Form1_Paint(object sender,PaintEventArgs e)
    {
        clip = new List<List<IntPoint>>();
        clip.Add(pol2);

        input = new List<List<IntPoint>>();
        input.Add(pol1);

        output = new List<List<IntPoint>>();

        Clipper c = new Clipper();
        c.AddPaths(input,PolyType.ptSubject,true);
        c.AddPaths(clip,true);
        c.Execute(clipType,output);

        DrawPolygon(output,e.Graphics,Pens.Red);
    }

XOR:

联盟:

路口:

差异:pol1 – pol2

差异:pol2 – pol1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读