polygonsclipperlib

Prevent clipper from merging polygons?


When I compute the difference between two shapes which touches one another (for example a rectangle A in a bigger rectangle B with a hole at rectangle A) and a clip shape (rectangle C) the two touching shapes are merged because their share the same edges and then the clipping is executed.

Is it possible to avoid merging touching shapes when clipping?

Here is an example of the difference between two shapes (A in green and B in red) and a clip (so the operation is: A & B - Clip), it returns the blue shape:

Difference (Blue) two touching shapes (A in green and B in red) with a clip

Instead of the blue rectangle, I would like to have those two shapes:

Expected result

And the intersection would give:

Intersection

This would give me the four shapes I want:

Expected result

I know I could perform the operations on each shape separately, but I am afraid it will be more costly.

Note

Here is the result of a XOR:

XOR


Solution

  • In the hand I compute the operation myself:

    1. compute the intersections between edges (of the clip shape and other shapes).
    2. for each vertex: sort its edges by angle (mandatory)
    3. walk through each edge clockwise and counterclockwise to compute the new polygons with their holes

    This is efficient enough, but I need a space-partitioning data structure to sort edges and quickly their intersections.