How can I repeat this shape NxN times with a certain spacing between repeated shapes?

1 回表示 (過去 30 日間)
Love Matlab
Love Matlab 2020 年 6 月 9 日
回答済み: Rik 2020 年 6 月 9 日
I attached a shape of square that I need to repeat NxN times to form basically an array. I need to have spacing between shapes as an option as well.
Thank you in advance
  2 件のコメント
Rik
Rik 2020 年 6 月 9 日
Probably the easiest way is to create an array for one of the corners and then use a loop to create all objects based on that coordinate offset.
Love Matlab
Love Matlab 2020 年 6 月 9 日
x1=0.25;
x2=.75;
y1=0.25;
y2=.75;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-');
xlim([-1, 2]);
ylim([-1, 2]);
patch(x,y,'b')
Here is my code for the square. How can I make an array with one of the corners?
Thank you!!

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2020 年 6 月 9 日
Make the coordinates depend on x1 and y1:
x1=0.25;
y1=0.25;
draw_line_and_patch(x1,y1)
function draw_line_and_patch(x1,y1,width,height,ax)
if nargin<3,width=0.5;end
if nargin<4,height=0.5;end
if nargin<5,ax=gca;end%create axes object if missing
x2=x1+width;
y2=y1+height;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-','Parent',ax);
xlim(ax,[-1, 2]);
ylim(ax,[-1, 2]);
patch(x,y,'b','Parent',ax)
end
Now you have a function that creates a box for you, you can easily loop through a grid of coordinates you create with meshgrid, ndgrid, or any way you prefer.

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by