polygon clipping
xufriedman
Hello, all. I'm wondering if anyone has written an Igor implementation to figure the union of two polygons. For example, if polygon 1 is defined by waves x1 and y1, and polygon 2 is defined by waves x2 and y2, I want to generate x3 and y3 that forms the union of those two. I didn't find anything in the existing Igor libraries that fit the bill. There are quite a few hits on various programming websites, and I'm wondering if somebody has already spent the time to translate them to Igor. Thanks!
If the points are ordered in the same direction (clockwise/anticlockwise) in each pair of waves, you can probably do it with FindPointsInPoly.
FindPointsInPoly x1 y1 x2 y2 should identify the points of y1 vs x1 that fall within y2 vs x2. Collect those points, then do the reverse - FindPointsInPoly x2 y2 x1 y1 - to find the other set of points that define the intersection. Concatenate the two sets of points without reordering and you should get x3, y3.
I haven't tested this, but I imagine that it should work, provided you have a single fully bounded intersection...
If the points are a bit sparse you might want to figure out a way to add the two line intersections where the polygons cross.
May 23, 2019 at 02:18 am - Permalink
There may be a tricky way of getting the result without much effort if you can accept integer representation:
1. Use ImageBoundaryToMask for the two polygons and add the two masks.
2. Use ImageAnalyzeParticles/W on the resulting mask and use the resulting W_boundaryX and W_boundaryY as the combined poly representation.
May 23, 2019 at 02:06 pm - Permalink
Thanks for your suggestions. They were much appreciated. I went with the FindPointsInPoly approach, so that the original vertices would be preserved as much as possible. It works okay when the polygons are well-behaved (no internal twisting) and vertices are dense (don't need to figure out where lines intersect). I guess the on-line versions I found are so lengthy and complicated because they take all the worst-case scenarios into account. Code attached if anyone is interested. If it were more robust, it might deserve to be a code snippet.
May 30, 2019 at 10:54 am - Permalink