如何绘制一个NxN的圆数组? [英] How to plot an NxN array of circles?

查看:147
本文介绍了如何绘制一个NxN的圆数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个NxN的圆形数组.为了形象化,我附上了我想要实现的图像.我是MatlLab的新手,所以我尝试首先绘制一个圆形,这是下面的示例代码:

I want to plot an NxN array of circles. Just to visualize, I attached an image of what I want to achieve. I'm new in MatlLab so I tried to plot a single circle first, and here is the sample code below:

n = 2^10;                   % size of mask
M = zeros(n);
I = 1:n;
x = I - n/2;                % mask x - coordinates
y = n/2 - I;                % mask y - coordinates
[X,Y] = meshgrid(x,y);      % create 2-D mask grid
R = 200;                     % aperture radius
A = (X.^2 + Y.^2 <= R^2);   % Circular aperture of radius R
M(A) = 1;                   % set mask elements inside aperture to 1
imagesc(M)                  % plot mask
axis image

我真的不知道如何绘制2D圆阵列.两个圆之间的距离是两个半径.我的研究需要这个.希望任何人都能提供帮助. 4 x 4的圆形阵列.

I really don't have any idea on how to plot a 2D-array of circles. The distance between two circles is two radii. I need this for my research. Hoping anyone can help.A 4 x 4 array of circles.

推荐答案

如果只想绘制一组圆,则可以使用

If you just want to plot a set of circles, you can use the rectangle function within a loop

如果在对rectangle的调用中将Curvature属性设置为1,它将被绘制为圆形(请参阅文档).

If in the call to rectangleyou set the Curvature property to 1 it will be drawn as circle (ref. to the documentation).

在循环中,您需要通过定义其lower left坐标以及其widthheight来正确设置每个矩形(圆形)的位置.

In the loop you hav to properly set the position of each rectangle (circle) by defining its lower left coordinates along with its width and height.

n的圆数和直径的d定义,您可以将lower left坐标集计算为:

Having defined with n the number of circles and with d their diameter, you can compute the set of lower left coordinates as:

px=linspace(0,d*(nc-1),nc)
py=linspace(0,d*(nr-1),nr)

可以通过设置轴的颜色或绘制另一个矩形来获得黑色背景.

The black background can be either obtained by setting the color of the axes or by plotting another rectangle.

然后,您可以设置xlimylim使其适合外部矩形.

Then you can set the xlim and ylim to fit with the external rectangle.

您还可以通过将其○Visible property to off`设置为不可见来使axex不可见.

You can also make the axex invisible, by setting its ○Visibleproperty tooff`.

编辑#1

代码已更新,可以绘制用户定义的圆数(设置行数和列数)

Code updated to allow drawing a user-defined number of circles (set the number of rows and the number of columns)

% 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'

编辑#2

解决OP注释的替代解决方案:

Aternative solution to address the OP comment:

如果要固定轴,说要1024 x 1024的网格,(图像大小与圆半径无关)如何将其合并到代码中?

如果要使用固定的(1024 x 1024)面具在其上绘制圆,请从问题中发布的cde开始,只需执行以下操作:

If you want to use a fixed (1024 x 1024) mask on which to plot the circles, starting from the cde you've posted in the question, you can simply do the following:

  • define the number of circles you want to plot, on a (1024 x 1024) mask they can be 2, 4, 8, ...
  • define a basic mask holding just one circle
  • identify the points inside that circle and set them to 1
  • Replicate (using the function repmat the basic mask accoding to the numner of circles to be plotted

根据您的代码,这种隐含可能是:

The implemntation, based on your code could be:

% 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])

这篇关于如何绘制一个NxN的圆数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆