필터 지우기
필터 지우기

How can I draw many circles without overlapping?

조회 수: 5 (최근 30일)
H Y
H Y 2018년 1월 12일
댓글: Manisha Kumari 2021년 10월 26일
The following code allows overlapping. How can I draw circles without overlapping?
t = linspace(0,2*pi,100);
figure
cx = 100 + (rand(20, 1) .* 1000); %center
cy = 100 + (rand(20, 1) .* 1000); % center
r = 50; % radius
plot(r*sin(t)+cx,r*cos(t)+cy)
y = r*cos(t)+cy
x = r*sin(t)+cx
for i =0:20
fill(x',y','k')
%fill(r*sin(t)+cx,r*cos(t)+cy, 'k')
r = r+1;
end
i=i+1;
axis([0,1200,0,1200])
axis square

채택된 답변

michio
michio 2018년 1월 12일
Interesting problem. One way is to keep generating a set of centers until every center is separated by more than the fixed radius from any other centers. Simple logic. Here's an example function that does just that. Inputs are the number of centers required and the radius.
function centers = getCentersWithoutOverlaps(numCenters, radius)
%
% these are parameters you can set
a = 0;
b = 10;
%
radius2 = radius^2;
centers = zeros(numCenters,2);
centers(1,:) = a + rand(1,2).*b; % first center
for ii=2:numCenters
tmp = a + rand(1,2).*b;
% keep generating a random number until
% the new center is separated by "radius" from any other center points.
while any(sum((centers(1:ii-1,:)-tmp).^2,2) < radius2)
tmp = a + rand(1,2).*b;
end
% if the condition is met, the new center is added to "centers"
centers(ii,:) = tmp;
end
%
end
Please save above as a function and the following is the example script that you can try.
t = linspace(0,2*pi,100);
figure
r = 1; % radius
centers = getCentersWithoutOverlaps(10, 2);
cx = centers(:,1);
cy = centers(:,2);
plot(r*sin(t)+cx,r*cos(t)+cy);
y = r*cos(t)+cy;
x = r*sin(t)+cx;
fill(x',y','k')
axis square
Note that you need to specify reasonable values for numCenters and radius. Otherwise the function will not find the non overlapping set of centers.

추가 답변 (2개)

H Y
H Y 2018년 1월 13일
Amazing! Thank you for answering my question. On this script, where can I set the definition of the field?
axis([0,1200,0,1200])
  댓글 수: 2
michio
michio 2018년 1월 14일
I think after the fill function would work out just fine?
Manisha Kumari
Manisha Kumari 2021년 10월 26일
Hi ,
in the below code when i am putting axis([0,100,0,100]) then the above code is not working fine. could you please resolve this issue.
clc
close all
clear all
t = linspace(0,2*pi,100);
r = 1; % radius
centers = getCentersWithoutOverlaps(10,2);
cx = centers(:,1);
cy = centers(:,2);
plot(r*sin(t)+cx,r*cos(t)+cy);
y = r*cos(t)+cy;
x = r*sin(t)+cx;
%axis([0,100,0,100])
fill(x',y','r')
axis([0,100,0,100])
Thank you

댓글을 달려면 로그인하십시오.


MFB
MFB 2019년 4월 20일
@michio
you put different radius in function calling i.e. radius 2 in the function as input argument and in the script you put r = 1. Can you explain why???
  댓글 수: 1
michio
michio 2019년 4월 22일
the r=1 in the script is just for visualization purpose. It can be any value. If you set r to be smaller than 2, you can draw circles without overlapping.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!