Viron Gil A. Estrada
Viron Gil A. Estrada

Reputation: 89

Make a 2D array of circles with a distance of 2 radius from each centers

I am currently working on making an NxN arrays of circles where N>0 with varying circle radius inside a 1024 by 1024 meshgrid. The n_circles defines the number of circles in an array, and R is the radius. I used a repmat function to replicate the array depending on the number of circles I choose. Here's my code below:

n_circles = 4                    % Define the number of circles to be plotted                               
n0 = round(1024/n_circles); % Define the size of the basic mask
M0 = zeros(n0);             % Initialize the basic mask
I = 1:n0;             % Define the x and y coordinates of the basic mask
x = I - n0/2;
y = n0/2 - I;
[X,Y] = meshgrid(x,y);      % Create the mask
R = 4;                      % Define the radius of the basic circle

% Get the indices of the points inside the basic circle
A = (X.^2 + Y.^2 <= R^2);   
M0(A) = 1;                           % Set basic mask
% Replicate basic mask according to the number of circles to be plotted
M=repmat(M0,n_circles,n_circles); 
M(2^10,2^10) = 0;                    % zerofill the rest of the matrix

figure(1)
imagesc(M)
daspect([1 1 1])

imwrite(M,[num2str(n_circles),'array.bmp'])

The problem is that the circles are not touching each other, i.e. when I replicate them, they are far away from each other. I need to generate a 2D array of circles that are touching from each other, with the radius set at low values. I attached a picture below to visualize my concern.

enter image description here

So in the picture, the circles have a distance of 2 radii from their centers, and the radius of the circle is very small in such a way that the whole 2D array of circles is smaller than the whole meshgrid. Can anyone help? Thanks.

Upvotes: 1

Views: 466

Answers (1)

Hunter Jiang
Hunter Jiang

Reputation: 1300

  • Step 1: we create a "mask" and get the indexes of the point of it. Note that we save it as [xx,yy] by calling ind2sub, so [x0+xx-R,y0+yy-R] will be the indexes of a circle with the center [x0,y0].
  • Step 2: Figuring out the center of a circle, we could call sub2ind(size(M),MidX+xx-R,MidY+yy-R) to get real indexes of it.(It seems pretty strange but I only got this way, waiting for better answers.)
  • Step 3: Using a for loop to generate all the circles in the M.

Note that if n_circle is odd, we should do some math and move the centers of the circle a little to keep all the circles in the middle of the picture.


clc; clear;
n_circles = 4;                    % Define the number of circles to be plotted
R = 4;                      % Define the radius of the basic circle
Len=400;
M=zeros(Len);               % Create the hole mask

% Get the indices of the points inside the basic circle
M0 = zeros(2*R+1);             % Initialize the basic mask
I = 1:(2*R+1);             % Define the x and y coordinates of the basic mask
x = (I - R)-1;
y = (R - I)+1;
[X,Y] = meshgrid(x,y);      % Create the mask
A = (X.^2 + Y.^2 <= R^2);   
[xx,yy]=ind2sub(size(M0),find(A == true)); 


%plot
for ii=1:n_circles
    for jj=1:n_circles
      MidX=Len/2+(ii-n_circles/2-0.5)*(2*R);
      MidY=Len/2+(jj-n_circles/2-0.5)*(2*R);
%       [MidX MidY]
      M(sub2ind(size(M),MidX+xx-R-1,MidY+yy-R-1))=1;
    end
end
figure(1)
imshow(M)

The output image is:

enter image description here

hope it helps!

Upvotes: 3

Related Questions