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.
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
採用された回答
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
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))
0 件のコメント
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,:))
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!