For intersecting circles, how to remove overlap and have chord visible? Also, how to randomly generate circle position in design space?
Show older comments
Accepted Answer
More Answers (2)
Roger Stafford
on 9 Jun 2014
Let P1 = [x1;y1] and P2 = [x2;y2] be column vectors for the coordinates of the two centers of the circles and let r1 and r2 be their respective radii.
d2 = sum((P2-P1).^2);
P0 = (P1+P2)/2+(r1^2-r2^2)/d2/2*(P2-P1);
t = ((r1+r2)^2-d2)*(d2-(r2-r1)^2);
if t <= 0
fprintf('The circles don''t intersect.\n')
else
T = sqrt(t)/d2/2*[0 -1;1 0]*(P2-P1);
Pa = P0 + T; % Pa and Pb are circles' intersection points
Pb = P0 - T;
a = atan2(P1(2)-P2(2),P1(1)-P2(1));
b1 = atan2(abs(det([Pa-P1,P1-P2])),dot(Pa-P1,P1-P2));
b2 = atan2(abs(det([Pb-P2,P2-P1])),dot(Pb-P2,P2-P1));
t1 = linspace(a-b1,a+b1);
t2 = linspace(a+pi-b2,a+pi+b2);
Q1 = bsxfun(@plus,P1,r1*[cos(t1);sin(t1)]);
Q2 = bsxfun(@plus,P2,r2*[cos(t2);sin(t2)]);
plot(Q1(1,:),Q1(2,:),'y-',Q2(1,:),Q2(2,:),'y-',...
[Pa(1),Pb(1)],[Pa(2),Pb(2)],'y-')
axis equal
end
Kelly Kearney
on 9 Jun 2014
0 votes
If you have the mapping toolbox, try polybool. If you don't, try this option, which appears to do provide a wrapper around GPC, which is the same thing polybool does.
Categories
Find more on Computational Geometry in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!