MATLAB Answers

improving the creation of subpolygons

6 views (last 30 days)
Riccardo Tronconi
Riccardo Tronconi on 29 Jul 2021
Commented: Adam Danz on 31 Jul 2021
HI guys! I have a macro-polygon 10x10 and I would like to divide it into 25 equal parts of width and height equal to 2. So far I have created this lines of code but I was wondering if there is a much efficient way to implement it.
pgon = polyshape([0 0 10 10],[10 0 0 10]);
plot(pgon);
p(1) = rectangle('position', [0,0,2,2] );
p(2) = rectangle('position', [2,0,2,2] );
p(3) = rectangle('position', [4,0,2,2] );
p(4) = rectangle('position', [6,0,2,2] );
p(5) = rectangle('position', [8,0,2,2] );
p(6) = rectangle('position', [0,2,2,2] );
p(7) = rectangle('position', [2,2,2,2] );
p(8) = rectangle('position', [4,2,2,2] );
p(9) = rectangle('position', [6,2,2,2] );
p(10) = rectangle('position', [8,2,2,2] );
p(11) = rectangle('position', [0,4,2,2] );
p(12) = rectangle('position', [2,4,2,2] );
p(13) = rectangle('position', [4,4,2,2] );
p(14) = rectangle('position', [6,4,2,2] );
p(15) = rectangle('position', [8,4,2,2] );
p(16) = rectangle('position', [0,6,2,2] );
p(17) = rectangle('position', [2,6,2,2] );
p(18) = rectangle('position', [4,6,2,2] );
p(19) = rectangle('position', [6,6,2,2] );
p(20) = rectangle('position', [8,6,2,2] );
p(21) = rectangle('position', [0,8,2,2] );
p(22) = rectangle('position', [2,8,2,2] );
p(23) = rectangle('position', [4,8,2,2] );
p(24) = rectangle('position', [6,8,2,2] );
p(25) = rectangle('position', [8,8,2,2] );
Many thanks to all in advance!!
  1 Comment
Adam Danz
Adam Danz on 31 Jul 2021
There are much easier ways to accomplish this but based on your comment in a related thread, it sounds like you don't need to display the rectangles at all.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 29 Jul 2021
Divide visually or computationally?
big = polyshape([0 10 10 0 0], [0 0 10 10 0]);
p = repmat(big, 5, 5); % Preallocate
edges = 0:2:10;
for x = 1:(numel(edges)-1)
E1 = edges(x + [0 1 1 0 0]);
for y = 1:(numel(edges)-1)
E2 = edges(y + [0 0 1 1 0]);
p(x, y) = polyshape(E1, E2);
end
end
figure
plot(big)
figure
plot(p(1:2:25), 'EdgeColor', 'k')
Note that by the way I constructed the elements of p, the first one is in the lower-left corner.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!