问题
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:
I would have expected to have 15 full squares and 13 clipped squares as a result.
This is the code I'm using:
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);
When running the above code is takes about 0.5 seconds. Because the result looks like the clip and subject are switched I've switched them. The result is the same, but now it only takes 0.1 seconds. So something extra is done. I think it is the merging of the resulting squares.
I don't want the result to merge. How can I prevent that? Or perhaps my code is faulty?
回答1:
According to Clipper documentation of the Execute
method:
There are several things to note about the solution paths returned:
... polygons may rarely share a common edge (though this is now very rare as of version 6)
which I think means that paths get merged when performing any clipping operations.
I've tried to do the same thing with different PolyFillType`s with no success either.
You might want to try running the Execute
method on each square individually (subject) against the clipping area on each iteration which should do the job, though performance may suffer as a result.
回答2:
In this precise case you can easily compute the result by hand, without clipper lib.
The fact that all rectangles are axis-aligned objects makes it possible to speed up computations, so it could even be faster performing the operation yourself.
来源:https://stackoverflow.com/questions/46235176/clipperlib-clip-multiple-squares-with-rectangle-produces-1-result