剪辑如何在Clipper库中的多边形联合中起作用

Sea*_*ean 2 c# union polygon clipperlib

在此输入图像描述

我使用限幅器库.在图中,红色和黑色是剪辑,绿色是多边形.代码如下所示.但是,我不明白为什么生成的联合多边形是(7 3 4 14 9 1 2 6).我认为它应该是(1 4 14 9)?数字是图中所示的顶点.

using System;
using ClipperLib;

using Polygon = System.Collections.Generic.List<ClipperLib.IntPoint>;
using Polygons = System.Collections.Generic.List<System.Collections.Generic.List<ClipperLib.IntPoint>>;

namespace ClipperLibrary_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Polygons subj = new Polygons(1);
            subj.Add(new Polygon(4));
            subj[0].Add(new IntPoint(0, 0));
            subj[0].Add(new IntPoint(0, 70));
            subj[0].Add(new IntPoint(100, 70));
            subj[0].Add(new IntPoint(100, 0));

            Polygons clip = new Polygons();
            clip.Add(new Polygon(4));
            clip[0].Add(new IntPoint(40, 0));
            clip[0].Add(new IntPoint(40, 100));
            clip[0].Add(new IntPoint(150, 100));
            clip[0].Add(new IntPoint(150, 0));

            clip.Add(new Polygon(4));
            clip[1].Add(new IntPoint(-50, 0));
            clip[1].Add(new IntPoint(-50, 100));
            clip[1].Add(new IntPoint(60, 100));
            clip[1].Add(new IntPoint(60, 0));

            Polygons solution = new Polygons();
            Clipper c = new Clipper();
            c.AddPolygons(subj, PolyType.ptSubject);
            c.AddPolygons(clip, PolyType.ptClip);

            c.Execute(ClipType.ctUnion, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);

            foreach (Polygon p in solution)
            {
                Console.WriteLine("next ");
                foreach (IntPoint pt in p)
                {
                    Console.WriteLine("(" + pt.X + "; " + pt.Y + ")");
                }
            }
            //Console.WriteLine("area: " + Clipper.Area(solution[0]).ToString());
            Console.WriteLine(solution.Count);
            Console.WriteLine("right: " + c.GetBounds().right + ": left: " + c.GetBounds().left);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

如果我换PolyFillType.pftEvenOddPolyFillType.pftNonZero,它工作正常.任何人都可以解释它如何影响结果?

jmi*_*cza 7

这是因为你正在使用PolyFillType.pftEvenOddfilltype subjclip.指定的填充类型操作分别在两个输入集上执行.在你的例子中,它什么也没做subj,但是清除了两个矩形的公共部分clip,给出了不相交的矩形.联合包含这两个单独的矩形加上主题不变.

只需更改clipto 的填充类型即可PolyFillType.pftPositive获得预期结果.

我用过这个来源: