制作一个二维数组的圆,每个圆心的半径为2 [英] Make a 2D array of circles with a distance of 2 radius from each centers
问题描述
我目前正在制作一个NxN的圆数组,其中N> 0的圆半径在1024 x 1024网格内. n_circles定义数组中的圆数,R为半径.我使用了repmat函数来复制数组,具体取决于我选择的圆圈数.这是我的以下代码:
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'])
问题是圆圈之间没有相互接触,即当我复制它们时,它们之间的距离很远.我需要生成彼此接触的2D圆数组,并且将半径设置为较低的值.我在下面附上一张图片以显示我的担忧.
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.
因此,在图片中,圆与圆心的距离为2半径,圆的半径非常小,使得圆的整个2D数组小于整个网格.有人可以帮忙吗?谢谢.
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.
推荐答案
- 步骤1 :我们创建一个遮罩"并获取其点的索引.请注意,我们通过调用
ind2sub
将其另存为[xx,yy]
,因此[x0+xx-R,y0+yy-R]
将是中心为[x0,y0]
的圆的索引. - 第2步:找出一个圆的中心,我们可以调用
sub2ind(size(M),MidX+xx-R,MidY+yy-R)
以获得它的真实索引.以获得更好的答案.) - 第3步:使用
for
循环生成M
中的所有圆. - Step 1: we create a "mask" and get the indexes of the point of it. Note that we save it as
[xx,yy]
by callingind2sub
, 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 theM
.
请注意,如果n_circle
是 odd (奇数),我们应该做一些数学运算并将圆心稍微移动一点,以使所有圆都位于图片的中间.
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)
输出图像为:
希望有帮助!
这篇关于制作一个二维数组的圆,每个圆心的半径为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!