如果你只想绘制一组圆,你可以使用长方形 https://it.mathworks.com/help/matlab/ref/rectangle.html循环内的函数
如果在通话中rectangle
你设置了Curvature
财产给1
它将被绘制为圆形(请参阅文档)。
在循环中,您必须通过定义其来正确设置每个矩形(圆形)的位置lower left
坐标及其width
and height
.
定义为n
圆圈的数量和d
它们的直径,你可以计算出lower left
坐标为:
px=linspace(0,d*(nc-1),nc)
py=linspace(0,d*(nr-1),nr)
黑色背景可以通过设置轴的颜色或绘制另一个矩形来获得。
然后你可以设置xlim
and ylim
以适应外部矩形。
您还可以通过设置 ○Visible 使 axex 不可见property to
off`.
Edit #1
更新代码以允许绘制用户定义数量的圆圈(设置行数和列数)
% Define the number of circles
% Number of rows
nr=8
% NUmber of column
nc=8
% Define the diameter of the circle
d=6
% Create an axex and set hold to on
ax=axes
hold on
% Evalaute the lower left position of each circle
px=linspace(0,d*(nc-1),nc)
py=linspace(0,d*(nr-1),nr)
% Plot the background rectangle
rectangle('Position',[px(1),py(1),d*nc,d*nr],'Curvature',[0 0],'facecolor','k')
% Plot all the circles
for i=1:length(px)
for j=1:length(py)
rectangle('Position',[px(i) py(j) d d],'Curvature',1,'facecolor','w')
end
end
% Set the aspect ratio of the axes
daspect([1 1 1])
% Set the XLim and YLim
xlim([px(1) d*nc])
ylim([py(1) d*nr])
% Make the axes invisible
ax.Visible='off'
Edit #2
解决OP评论的替代解决方案:
如果我想让轴固定,假设我想要一个 1024 x 1024 的网格,(图像大小与圆半径无关)如何将其合并到代码中?
如果你想使用固定的(1024 x 1024)
绘制圆圈的掩码,从您在问题中发布的 cde 开始,您可以简单地执行以下操作:
- 定义要在 a 上绘制的圆圈数
(1024 x 1024)
他们可以是面具2, 4, 8, ...
- 定义一个仅包含一个圆的基本蒙版
- 识别该圆内的点并将它们设置为
1
- 复制(使用函数repmat http://it.mathworks.com/help/matlab/ref/repmat.html根据要绘制的圆圈数量的基本掩模
根据您的代码,实现可能是:
% Define the number of circles to be plotted
% on a (1024 x 1024) mask they can be 2, 4, 8, ...
n_circles=4
% Define the size of the basic mask
n0 = (2^10)/n_circles;
% Initialize the basic mask
M0 = zeros(n0);
% Define the x and y coordinates of the basic mask
I = 1:n0;
x = I - n0/2;
y = n0/2 - I;
% Create the mask
[X,Y] = meshgrid(x,y);
% Define the radius of the basic circle
R = n0/2;
% Get the indices of the points insiede the basic circle
A = (X.^2 + Y.^2 <= R^2);
% Set basic mask
M0(A) = 1;
% Open a FIgure
figure
% Replicate the basic mask accoding to the numner of circles to be plotted
M=repmat(M0,n_circles,n_circles);
% Display the mask
imagesc(M)
% Set the axes aspect ratio
daspect([1 1 1])