gpt4 book ai didi

C# 剪辑线段与剪辑库

转载 作者:行者123 更新时间:2023-11-30 14:31:53 26 4
gpt4 key购买 nike

我正在使用 clipper 对代表我程序中对象轮廓的多边形执行一系列操作。问题是,我现在想剪下一个线段网格来填充这些轮廓,但我很难用 Clipper 做到这一点。

目前我正在将我的线转换为厚度为 2 个单位的矩形,然后我对其进行交集操作,最后我需要将新的多边形恢复为线段。这虽然效率很低,但会产生很多错误。

这可以用 Clipper 来完成吗,如果不能,我可以使用其他东西吗?或者我是否需要实现我自己的线段剪裁器?

最佳答案

使用 Clipper 版本 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

}

enter image description here

关于C# 剪辑线段与剪辑库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com