Observablerxjs
Observablerxjs

Reputation: 732

Draw polygon with hole inside

I need to draw a rectangle polygon with a rectangle hole inside. I found this piece of code but I couldn't manage to understand how I can modify to meet my requirement.

 figure()
 p  = [0 0; 0 1; 1 1; 1 0]; %ccw
 pp = [0 0; 1 0; 1 1; 0 1]; %cw
 ph = p + [1.2 0];
 # add hole
 ph(end+1,:) = nan;
 ph = [ph; (pp-[0.5 0.5])*0.5+[1.7 0.5]];
 po = polygon2patch (ph);
 patch (po(:,1), po(:,2), 'b', 'facecolor', 'c');
 axis image

Upvotes: 0

Views: 122

Answers (1)

HansHirse
HansHirse

Reputation: 18925

The polygon2patch function certainly seems useful, but maybe for only drawing two rectangles, you could also use just two patch commands, and simply set the inner rectangle, i.e. the hole, to white foreground color, like so:

outer = [0 0; 2 0; 2 1; 0 1];
inner = [0.4 0.2; 1.6 0.2; 1.6 0.8; 0.4 0.8];
patch(outer(:, 1), outer(:, 2), 'c');
patch(inner(:, 1), inner(:, 2), 'w');
axis equal;

This will produce such an output:

Output

Hope that helps!

Upvotes: 1

Related Questions