Intersection points of two circles in MATLAB
I need to find out the intersecting points of two circles. I have the center points and the radius of each cir开发者_如何转开发cle. I need to do it in MATLAB. Any help will be appreciated.
Assume a triangle ABC, where A and B are the centers of the circle, and C is one or the other intersection point. a, b, and c are the sides opposite the corresponding corners. alpha, beta, and gamma are the angles associated with A, B, and C, respectively.
Then, b^2+c^2 - 2*bccos(alpha) = a^2. Knowing alpha (or its cosine), you can find the location of C.
A = [0 0]; %# center of the first circle
B = [1 0]; %# center of the second circle
a = 0.7; %# radius of the SECOND circle
b = 0.9; %# radius of the FIRST circle
c = norm(A-B); %# distance between circles
cosAlpha = (b^2+c^2-a^2)/(2*b*c);
u_AB = (B - A)/c; %# unit vector from first to second center
pu_AB = [u_AB(2), -u_AB(1)]; %# perpendicular vector to unit vector
%# use the cosine of alpha to calculate the length of the
%# vector along and perpendicular to AB that leads to the
%# intersection point
intersect_1 = A + u_AB * (b*cosAlpha) + pu_AB * (b*sqrt(1-cosAlpha^2));
intersect_2 = A + u_AB * (b*cosAlpha) - pu_AB * (b*sqrt(1-cosAlpha^2));
intersect_1 =
0.66 -0.61188
intersect_2 =
0.66 0.61188
Find the equations of the circles. Make sure to account for the negative of the square root or else you will just have a semi circle.
Set the equations of the two circles equal to eachother.
Here is a simple code using two File Exchange submissions: first - to draw circles, second - to find intersections (links below).
clf
N=30; % circle resolution as the number of points
hold on
% draw 1st circle at (0,0) radius 5 and get X and Y data
H1=circle([0 0],5,N);
X1=get(H1,'XData');
Y1=get(H1,'YData');
% draw 2nd circle at (2,5) radius 3 and get X and Y data
H2=circle([2 5],3,N);
X2=get(H2,'XData');
Y2=get(H2,'YData');
% find intersection points
[x,y]=intersections(X1,Y1,X2,Y2,0);
% and plot them as red o's
plot(x,y,'ro')
hold off
axis equal
- CIRCLE
- Fast and Robust Curve Intersections
Function CIRCCIRC
does this for you.
[xout,yout] = circcirc(x1,y1,r1,x2,y2,r2)
This will give you the two intersection points.
http://www.mathworks.nl/help/map/ref/circcirc.html
I am pasting Roger Stafford's answer here. (See link) This also prints if there is no intersection between circles. I think you can figure it the geometrical relations in the code.
% P1 and P2 are column vectors r1 and r2 are their respective radius. % P1 = [x1;y1]; P2 = [x2;y2];
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;
end
Pint = [Pa, Pb];
精彩评论