首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >裁剪如何在剪贴库中的多边形并集中工作

裁剪如何在剪贴库中的多边形并集中工作
EN

Stack Overflow用户
提问于 2013-06-14 06:40:22
回答 1查看 7K关注 0票数 2

我使用clipper library。在该图中,红色和黑色是剪辑,绿色是多边形。代码如下所示。然而,我不明白为什么生成的并集多边形是(7 3 4 14 9 1 2 6).,我假设它应该是(1 4 14 9)?该数字为图中所示的顶点。

代码语言:javascript
复制
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,它会工作得很好。有人能解释一下它是如何影响结果的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 07:05:09

这是因为您对subjclip都使用了PolyFillType.pftEvenOdd填充类型。指定的填充类型操作分别在两个输入集上执行。在您的示例中,它在subj上不做任何事情,但是清除了clip的两个矩形的公共部分,给出了不相交的矩形。联合包含这两个单独的矩形,加上主题不变。

只需将clip的填充类型更改为PolyFillType.pftPositive,就会得到预期的结果。

我使用了这个来源:

  • http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/Clipper/Methods/Execute.htm
  • http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/PolyFillType.htm
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17098403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档