How to draw a polygon with multiple holes efficiently?

I would like to build a polygon with several holes in it, for example:

P = [ 0.5, 0.8; 1.0, 0.0; % outer boundary 0.0, 0.0; 0.5, 0.8; %{ %} 0.5, 0.3; 0.3, 0.1; % inner boundary I 0.7, 0.1; % (hole) 0.5, 0.3; %{ %} 0.5, 0.6; 0.3, 0.4; % inner boundary II 0.7, 0.4; % (hole) 0.5, 0.6; ]; figure, clf, hold on patch(P(:,1),P(:,2), 'r', 'linestyle', 'none') 

two holes as expected

However, patch does not work as I expected. When I change the last hole to this:

 % ... 0.45, 0.6; % <- slightly offset in X-direction 0.3, 0.4; 0.7, 0.4; 0.45, 0.6; % <- slightly offset in X-direction % ... 

it happens:

too many holes!

This method works well with a single hole, but can I apply the same technique to multiple holes? Or do I need to resort to splitting it, as shown here ?

NOTE In the end, I will build hundreds (possibly partially transparent) of polygons in 3D space, and I want to be able to "view" the holes. Therefore, solutions with a โ€œwhite overlayโ€ polygon are not what I'm looking for.

+5
source share
1 answer

The best way to deal with this might be to break your polygons into Delaunay triangulation with squeezed edges , extract only the triangles that are in the interior of the limited edges, then create a set of boundary / vertex data that you can use with patch . Given the second P matrix from your example, here's how to do it:

 C = [1:3 5:7 9:11; 2:4 6:8 10:12].'; DT = delaunayTriangulation(P, C); vertices = DT.Points; faces = DT.ConnectivityList(isInterior(DT), :); patch('Faces', faces, 'Vertices', vertices, 'FaceColor', 'r', 'EdgeColor', 'none'); 

And the final figure:

enter image description here

When you create a triangulation, you will receive a warning that โ€œduplicate data points have been detected and deleted,โ€ nothing to worry about. You could even put all your data from the polygon and constraints together into one set of matrices and split the triangulation once to create one large set of face / vertex data for several polygons.

+4
source

Source: https://habr.com/ru/post/1271117/


All Articles