
我使用clipper library。在该图中,红色和黑色是剪辑,绿色是多边形。代码如下所示。然而,我不明白为什么生成的并集多边形是(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);
}
}
}编辑:
如果我将PolyFillType.pftEvenOdd更改为PolyFillType.pftNonZero,它会工作得很好。有人能解释一下它是如何影响结果的吗?
发布于 2013-06-14 07:05:09
这是因为您对subj和clip都使用了PolyFillType.pftEvenOdd填充类型。指定的填充类型操作分别在两个输入集上执行。在您的示例中,它在subj上不做任何事情,但是清除了clip的两个矩形的公共部分,给出了不相交的矩形。联合包含这两个单独的矩形,加上主题不变。
只需将clip的填充类型更改为PolyFillType.pftPositive,就会得到预期的结果。
我使用了这个来源:
https://stackoverflow.com/questions/17098403
复制相似问题