Cette page rpésente la fonction Matlab circlesIntersectionPoint().
Cette fonction calcule
les coordonnées des points d'intersection de deux cercles. la fonction retourne les points
d'intersection I1 et I2 après avoir vérifié que l'intersetion des cercles existe.
[I1, I2, status] = circlesIntersectionPoints(P1, r1, P2, r2)
Le code ci-dessous implémente le calcul présenté sur cette page : Comment calculer les points d'intersection de deux cercles ?. here is the source code of the function:
function [I1, I2, status] = circlesIntersectionPoints(P1, r1, P2, r2)
I1=0;
I2=0;
%% Distance between centers
d = sqrt( (P2(1)-P1(1))*(P2(1)-P1(1)) + (P2(2)-P1(2))*(P2(2)-P1(2)) );
%% Check cases
if (d>r1+r2)
% Circles are too far apart and do not intersect;
status = -1;
return
end
if (d<abs(r1-r2))
% One circle is inside the other and do not intersect;
status = -2;
return
end
if (d==0 && r1==r2)
% Circles are merged
status = -3;
return
end
if (d==r1+r2)
% Single intersection point;
status = 1;
end
if (d<r1+r2)
% Two intersection points.
status = 1;
end
%% a and b
a = (r1*r1 - r2*r2 + d*d) / (2*d);
b = (r2*r2 - r1*r1 + d*d) / (2*d);
%% h
h=sqrt(r1*r1 - a*a);
%% P5
P5 = [ P1(1) + (a/d)*(P2(1)-P1(1)) , P1(2) + (a/d)*(P2(2)-P1(2)) ];
%% Intersection points
I1 = [ P5(1) - h*(P2(2)-P1(2))/d ; P5(2) + h*(P2(1)-P1(1))/d];
I2 = [ P5(1) + h*(P2(2)-P1(2))/d ; P5(2) - h*(P2(1)-P1(1))/d];
end
Voici un exemple qui calcule les points d'intersection entre deux cercles :
%% Intersection entre deux cercles
%% More details at https://lucidar.me/fr/mathematics/how-to-calculate-the-intersection-points-of-two-circles
close all;
clear all;
clc;
%% Paramètre des cercles
P1=[3;4]; r1=3.4;
P2=[-1;2]; r2=2.6;
%% Dessin les cercles
drawCircle = @(C,R,color) plot ( C(1) + R*cos([0:0.01:2*pi]) , C(2) + R*sin([0:0.01:2*pi]), color );
figure;
hold on;
grid on;
axis square equal;
drawCircle(P1, r1, 'r');
drawCircle(P2, r2, 'g');
%% Points d'intersection
[I1, I2] = circlesIntersectionPoints(P1, r1, P2, r2);
plot (I1(1), I1(2), 'o');
plot (I2(1), I2(2), 'o');
Le code ci-dessus affiche la figure suivante:
Voici l'aide de la fonction :
>> help circlesIntersectionPoints
circlesIntersectionPoints Compute the intersection points of two circle
[I1, I2, status] = circlesIntersectionPoints(P1, r1, P2, r2)
Input parameters
P1 is the center of circle 1
r1 is the radius of circle 1
P2 is the center of circle 2
r2 is the radius of circle 1
Output parameters
I1 and I2 are the coordinates of intersection points
status is the status of the intersection:
* -1 : Circles are too far apart and do not intersect;
* -2 : One circle is inside the other and do not intersect;
* -3 : Circles are merged , there are an infinite number of points
* 1 : Single intersection point
* 2 : two intersection points
Usage example:
[i1, i2, s] = circlesIntersectionPoints([3,4], 3.4, [-1,2], 2.6)