Perform boolean operation on polygon(s) using the algorithm by Martinez, Rueda and Feito.
inpol = Nx2 matrix of (X, Y) coordinates constituting the polygons(s) to be clipped (subject polygon). clippol = Nx2 matrix of (X, Y) coordinates representing the clip polygon(s).
Polygons may have multiple non-intersecting regions. The contours are separated by [Nan NaN] rows. Polygons may also contain holes. Holes are defined as the region enclosed within the boundaries of polygon which are not part of the polygon region. Here again, the inner and outer contours are separated by [Nan NaN] rows. Specifically, if a contour is lying inside another contour, it is automatically treated as a hole.
The following features are allowed in input polygons:
The following features are not allowed in input polygons:
The argument op, the boolean operation, can be:
Output array outpol will be an Nx2 array of polygons resulting from the requested boolean operation
Optional output argument npol indicates the number of output polygons.
Know more about the algorithm by Martinez, Rueda and Feito[1].
[1]: http://www4.ujaen.es/~fmartin/bool_op.html
See also: clipPolygon_clipper,clipPolygon.
The following code
pol1 = [0.15 0.15; 0.55 0.45; 0.15 0.75]; pol2 = [0.35 0.45; 0.75 0.15; 0.75 0.75]; pol1a = polygon2patch(pol1); pol2a = polygon2patch(pol2); lw = 2; subplot (2, 6, [2 3]) patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw); axis image grid on title ("1. Subject polygon") subplot (2, 6, [4 5]) patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw); axis image grid on title "2. Clip polygon" op = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"}; for i=1:numel(op) subplot (6, 4, [12 16]+i); [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1); opol = polygon2patch (opol); patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none'); patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ... 'linewidth', lw); axis image grid on title (sprintf("%d. %s", i+2, op{i})); axis off endfor subplot (10, 4, 37); [opol, npol] = clipPolygon_mrf (pol2, pol1, 0); opol = polygon2patch (opol); patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none'); patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ... 'linewidth', lw); axis image grid on axis off title "7. Clip - sub";
Produces the following figure
Figure 1 |
---|
The following code
pol1 = [1 1; 5 1; 3 7; NaN NaN; 2 2; 3 5; 4 2]; pol2 = [3 1; 5 6; 1 6]; pol1a = polygon2patch(pol1); pol2a = polygon2patch(pol2); lw = 2; subplot (2, 6, [2 3]) patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw); axis image grid on title ("1. Subject polygon") subplot (2, 6, [4 5]) patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw); axis image grid on title "2. Clip polygon" op = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"}; for i=1:numel(op) subplot (6, 4, [12 16]+i); [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1); opol = polygon2patch (opol); patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none'); patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ... 'linewidth', lw); axis image grid on title (sprintf("%d. %s", i+2, op{i})); axis off endfor subplot (10, 4, 37); [opol, npol] = clipPolygon_mrf (pol2, pol1, 0); opol = polygon2patch (opol); patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none'); hold on patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none'); patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ... 'linewidth', lw); axis image grid on axis off title "7. Clip - sub";
Produces the following figure
Figure 1 |
---|
Package: geometry