I'm using v6.4.2 of the C# version of the ClipperLib.
I have a lot of squares making up a fishnet. I also have a rectangle.
I want to get a result where only the squares that are inside the rectangle are returned and partially overlapped get clipped.
The subjects are in green and the clip is in red:
The result I get is the brown/gray rectangle, which is only one polygon:
我原本期望得到 15 个完整的正方形和 13 个剪裁的正方形。
这是我正在使用的代码:
var startX = 100;
var startY = 250;
const int numPolygons = 10;
var subj = new Polygons(numPolygons * numPolygons);
for (var i = 0; i < numPolygons; i++)
{
for (var j = 0; j < numPolygons; j++)
{
var square = new Polygon(4)
{
new IntPoint(startX, startY),
new IntPoint(startX + 10, startY),
new IntPoint(startX + 10, startY - 10),
new IntPoint(startX, startY - 10)
};
subj.Add(square);
// Moving to the right
startX = startX + 10;
}
// Moving down
startY = startY - 10;
startX = 100;
}
var clip = new Polygons(1);
clip.Add(new Polygon(4));
clip[0].Add(new IntPoint(165, 215));
clip[0].Add(new IntPoint(255, 215));
clip[0].Add(new IntPoint(255, 155));
clip[0].Add(new IntPoint(165, 155));
var solution = new Polygons();
var c = new Clipper.Clipper();
c.AddPaths(subj, PolyType.ptSubject, true);
c.AddPaths(clip, PolyType.ptClip, true);
c.Execute(ClipType.ctIntersection, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);
Debug.WriteLine("solution.Count: " + solution.Count);
运行上面的代码大约需要0.5秒。因为结果看起来剪辑和主题被切换了,所以我已经切换了它们。结果是一样的,但现在只需要0.1秒。所以做了一些额外的事情。我认为这是所得正方形的合并。
我不希望结果合并。我怎样才能防止这种情况发生?或者我的代码有问题?