필터 지우기
필터 지우기

How we can generate a union area of overlapping 2 circles, 3 circles, multiple n circles

조회 수: 7 (최근 30일)
How we can generate a union area of overlapping 2 circles, 3 circles, multiple n circles
  댓글 수: 1
poonam tailor
poonam tailor 2016년 5월 29일
Star my question was about the area i want to found that is covered by n union circles, the tree view of the circle may be useful but i couldn got the code.

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

채택된 답변

Star Strider
Star Strider 2016년 2월 12일
Here it is for two circles:
t = linspace(0, 2*pi, 100);
cir = @(r,ctr) [r*cos(t)+ctr(1); r*sin(t)+ctr(2)]; % Circle Function
c1 = cir(1.0, [0; 0]);
c2 = cir(1.5, [1; 1]);
in1 = find(inpolygon(c1(1,:), c1(2,:), c2(1,:), c2(2,:))); % Circle #1 Points Inside Circle #2
in2 = find(inpolygon(c2(1,:), c2(2,:), c1(1,:), c1(2,:))); % Circle #2 Points Inside Circle #1
[fillx,ix] = sort([c1(1,in1) c2(1,in2)]); % Sort Points
filly = [c1(2,in1) (c2(2,in2))];
filly = filly(ix);
figure(1)
plot(c1(1,:), c1(2,:))
hold on
plot(c2(1,:), c2(2,:))
fill([fillx fliplr(fillx)], [filly fliplr(filly)], 'g', 'EdgeColor','none')
hold off
axis square
I will let you adapt it for more circles. That will require doing the find(inpolygon( ...)), sort, plot, and fill calls for each two-circle intersection. This would become complicated for more than two intersecting circles in the same area, but is probably possible.

추가 답변 (2개)

Steven Lord
Steven Lord 2018년 6월 7일
If you're okay with approximations to circles and you're using release R2017b or later:
% Generate and plot the first 'circle'
% A 1000-sided polygon is probably a close enough approximation for most purposes
n = nsidedpoly(1000, 'Center', rand(1, 2), 'Radius', rand);
h = plot(n);
% Set up the axes
title(sprintf('Area of original circle is %.2f', area(n)))
axis([-1 2 -1 2]);
axis square
% Pause for a second to let you see the plot with just one circle
pause(1)
% Let's add 5 more circles for a total of 6
for k = 2:6
% Generate a new circle
newcircle = nsidedpoly(1000, 'Center', rand(1, 2), 'Radius', rand);
% Take the union of the existing area with the new circle
n = union(n, newcircle);
% Update the plot
h.Shape = n;
title(sprintf('Area of union of %d circles is %.2f', k, area(n)))
% You may want to replace this with a drawnow call
% The pause is to let you see that the new circle has been added
pause(1)
end
Alternately, if you wanted to plot the edges of each individual circle (even if they overlap) store them in an array and plot the array.
clear v
for k = 1:6
v(k) = nsidedpoly(1000, 'Center', rand(1, 2), 'Radius', rand);
end
plot(v)
axis([-1 2 -1 2]);
axis square
A = area(union(v));
title(sprintf('The area of the union of the circles is %.2f', A))

KSSV
KSSV 2018년 6월 7일
편집: KSSV 2018년 6월 7일
You have that region points in your hand......get the points arrange them in anticlockwise order and use polyarea.
t = linspace(0, 2*pi, 100);
cir = @(r,ctr) [r*cos(t)+ctr(1); r*sin(t)+ctr(2)]; % Circle Function
c1 = cir(1.0, [0; 0]);
c2 = cir(1.5, [1; 1]);
in1 = find(inpolygon(c1(1,:), c1(2,:), c2(1,:), c2(2,:))); % Circle #1 Points Inside Circle #2
in2 = find(inpolygon(c2(1,:), c2(2,:), c1(1,:), c1(2,:))); % Circle #2 Points Inside Circle #1
[fillx,ix] = sort([c1(1,in1) c2(1,in2)]); % Sort Points
filly = [c1(2,in1) (c2(2,in2))];
filly = filly(ix);
figure(1)
plot(c1(1,:), c1(2,:))
hold on
plot(c2(1,:), c2(2,:))
% get coordinates
x = [fillx fliplr(fillx)] ;
y = [filly fliplr(filly)] ;
P = [x; y]; % coordinates / points
c = mean(P,2); % mean/ central point
d = P-c ; % vectors connecting the central point and the given points
th = atan2(d(2,:),d(1,:)); % angle above x axis
[th, idx] = sort(th); % sorting the angles
P = P(:,idx); % sorting the given points
P = [P P(:,1)]; % add the first at the end to close the polygon
plot( P(1,:), P(2,:), '.-r');
fill(P(1,:),P(2,:), 'g', 'EdgeColor','none')
hold off
axis square
area = polyarea(P(1,:),P(2,:))

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by