Replies: 3 comments 1 reply
-
In case it helps any other beginners, I managed to get this working. The following code will work with any number of polygons, three shown as an example... List<PathD> polygons = [
new() {
new PointD(0, 0),
new PointD(1, 0),
new PointD(1, 0.5),
new PointD(0, 0.5)
},
new() {
new PointD(0, 0),
new PointD(0.5, 0),
new PointD(0.5, 1),
new PointD(0, 1)
},
new() {
new PointD(0.5, 0),
new PointD(1.5, 0),
new PointD(1.5, 0.75),
new PointD(0, 0.75)
}
];
// Start with the first polygon as our result
PathsD result = [polygons[0]];
// Iteratively union with each remaining polygon
polygons.Skip(1).ToList().ForEach(polygon => {
ClipperD clipper = new();
clipper.AddSubject(result);
clipper.AddClip([polygon]);
PathsD intermediateResult = [];
clipper.Execute(ClipType.Union, FillRule.NonZero, intermediateResult);
// Update our result for the next iteration
result = intermediateResult;
});
// Ensure each resulting polygon is closed
result.Where(polygon => polygon.Count > 0 && !Helpers.PointsEqual(polygon[0], polygon[^1]))
.ToList()
.ForEach(polygon => polygon.Add(new PointD(polygon[0].x, polygon[0].y))); You can use the following to dump out the results... Console.WriteLine($"Found {result.Count} polygon(s)");
result.ForEach(path => {
path.ForEach(p => {
Console.WriteLine($" ({p.x}, {p.y})");
});
}); I'd be grateful if any of the experts here would look at this and see if it's right, or could be improved. For the three polygons shown above, this produces... Found 1 polygon(s) One thing that this code doesn't do as well as it could is that it can end up with multiple segments for what should be one line. For example, if you draw the three shapes above on some graph paper, and then compare these results with that, you'll see that the last three points define two lines that should be one single line from (0, 0) to (1, 0). This isn't an issue for my use case, but might be on others. |
Beta Was this translation helpful? Give feedback.
-
You have 4 discrete paths, not a single path for the rectangle |
Beta Was this translation helpful? Give feedback.
-
You are creating 4 "paths" each of a single vertex, instead of a single path with 4 vertices. Hence, you have no surface area, so when you union you get all the individual vertices back each in their one "path". Make your rectangles each one path with 4 vertices and it should work (you can see that your second example does that, while the first example is wrong) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Sorry if this is a dumb question, or if I have missed the point of this library.
Let's say I have a square (blue) and a rectangle (green) as follows...
I would like to get a polygon that is the union of these two, ie which has six points:
(0, 0)
(1, 0)
(1, 0.5)
(0.5, 0.5)
(0.5, 1)
(0, 1)
I tried the following...
When I dump out the points in
joined
, I get...(0, 0)
(0, 1)
(1, 1)
(1, 0)
(0, 0)
(0, 2)
(0.5, 2)
(0.5, 0)
...which is simply a union of the two sets of points.
I also tried the following...
...but this produced an empty set for every value of the
FillRule
enum.I tried adding the point
(0, 0)
to each path, to make sure the two shapes were both closed, but it didn't help.Please can someone explain what I'm doing? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions