Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot an NxN array of circles?

Tags:

arrays

matlab

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

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.

like image 850
Viron Gil A. Estrada Avatar asked Feb 11 '18 08:02

Viron Gil A. Estrada


1 Answers

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

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

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.

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.

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

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

Edit #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'

enter image description here

Edit #2

Aternative solution to address the OP comment:

If I want to make the axes fixed, say I want a meshgrid of 1024 by 1024, (the image size is independent of the circle radius) how do I incorporate it in the code?

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])
like image 139
il_raffa Avatar answered Nov 08 '22 06:11

il_raffa