具有Clipper库的C#剪辑线段 [英] C# clip linesegment with clipper library

查看:196
本文介绍了具有Clipper库的C#剪辑线段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Clipper在多边形上执行一系列操作,这些多边形代表程序中对象的轮廓.事实是,我现在想裁剪线段网格以填充这些轮廓,但是我正努力使用Clipper来做到这一点.

I am using clipper for performing a bunch of operations on polygons which represent the outlines of objects in my program. The thing is, I now want to clip a grid of line segments to fill these outlines but I am struggling to do this with Clipper.

目前,我正在将线条转换为厚度为2个单位的矩形,然后与之进行相交运算,最后需要将新的多边形还原为线段.但是,这效率很低,并且会产生很多错误.

At the moment I am converting my lines to rectangles with a thickness of 2 units which I then do an intersection operation with and then finally I need to revert the new polygons back to line segments. This is quite inefficient though and produces quite a few errors.

可以使用Clipper进行此操作吗,如果没有其他可以使用的东西,或者我需要实现自己的线段Clipper?

Can this be done with Clipper, if not is there something else that I can use or do I need to implement my own line segment clipper?

推荐答案

使用Clipper ver 6 ...

Using Clipper ver 6 ...

using ClipperLib;

using Path = List<IntPoint>;
using Paths = List<List<IntPoint>>;

static Polygon IntsToPolygon(Int64[] ints)
{
  int len1 = ints.Length / 2;
  Polygon result = new Polygon(len1);
  for (int i = 0; i < len1; i++)
    result.Add(new IntPoint(ints[i * 2], ints[i * 2 + 1]));
  return result;
}


static void Main(string[] args)
{
    Paths clip = new Paths(); //clipping polygon(s)
    //make a polygon in the shape of a pentagram
    //with center (200,200) and radius 100 ...
    Int64[][] ints1 = new Int64[][] 
    {
    new Int64[]{222, 169, 295, 169, 236, 212, 259, 281, 200, 238, 
      141, 281, 164, 212, 105, 169, 178, 169, 200, 100}
    };
    clip.Add(IntsToPolygon(ints1[0]));

    Paths subj = new Paths(); //subject paths, could be open or closed here
    int gridWidth = 5;
    int loopCnt = 200 / gridWidth;

    //now make grid lines and add them to subj ready for clipping ...

    //add horizontal grid lines ...
    for (int i = 0; i <= loopCnt; i++)
    {
    Path gridLine = new Path(2);
    gridLine.Add(new IntPoint(100, 100 + i * gridWidth));
    gridLine.Add(new IntPoint(300, 100 + i * gridWidth));
    subj.Add(gridLine);
    }
    //add vertical grid lines ...
    for (int i = 0; i <= loopCnt; i++)
    {
    Path gridLine = new Path(2);
    gridLine.Add(new IntPoint(100 + i * gridWidth, 100));
    gridLine.Add(new IntPoint(100 + i * gridWidth, 300));
    subj.Add(gridLine);
    }

    //now clip the gridlines (subj) with the pentagram (clip) ...
    Clipper c = new Clipper();
    PolyTree solution = new PolyTree();
    c.AddPaths(subj, PolyType.ptSubject, false); //ie paths NOT closed
    c.AddPaths(clip, PolyType.ptClip, true); //nb: clip paths MUST be closed
    c.Execute(ClipType.ctIntersection, 
      solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);
    Paths clippedGrids = Clipper.PolyTreeToPaths(solution);

    //Display the result (sorry, make your own display function) ...
    //Display(Paths p, Color stroke, Color brush, PolyFillType pft, bool IsClosed) 
    Display(subj, Color.FromArgb(0x18, 0, 0, 0x9c), 
        Color.Blue, PolyFillType.NonZero, false); //unclipped grid
    Display(clippedGrids, FromArgb(0, 0, 0, 0), 
        Color.Red, false); //clipped grid 

}

这篇关于具有Clipper库的C#剪辑线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆