Clear Filters
Clear Filters

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

9 views (last 30 days)
How we can generate a union area of overlapping 2 circles, 3 circles, multiple n circles
  1 Comment
poonam tailor
poonam tailor on 29 May 2016
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.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 12 Feb 2016
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.

More Answers (2)

Steven Lord
Steven Lord on 7 Jun 2018
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 on 7 Jun 2018
Edited: KSSV on 7 Jun 2018
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,:))

Community Treasure Hunt

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

Start Hunting!