function calculer_intersections_cercles(c1,c2) // c = [abscisse,ordonnée,rayon]
{
var Xc1 = c1[0]; // abscisse du centre du premier cercle
var Yc1 = c1[1]; // ordonnée du centre du premier cercle
var Rc1 = c1[2]; // rayon du premier cercle
var Xc2 = c2[0]; // abscisse du centre du second cercle
var Yc2 = c2[1]; // ordonnée du centre du second cercle
var Rc2 = c2[2]; // rayon du second cercle
if (Yc1==Yc2) // si les deux cercles sont sur la même abscisse, on utilise un simple Pythagore...
{
var a = Math.abs(Xc1-Xc2);
var XIa = XIb = (Math.pow(Rc2,2)-Math.pow(a,2)-Math.pow(Rc1,2))/(-2*a);
var YIa = Math.sqrt(Math.pow(Rc2,2)-Math.pow(a-XIa,2));
var YIb = -YIa;
}
else // ...sinon, un peu de trigo des familles !
{
var a = (-Math.pow(Xc1,2)-Math.pow(Yc1,2)+Math.pow(Xc2,2)+Math.pow(Yc2,2)+Math.pow(Rc1,2)-Math.pow(Rc2,2))/(2*(Yc2-Yc1));
var d = (Xc2-Xc1)/(Yc2-Yc1);
var A = Math.pow(d,2)+1;
var B = -2*Xc1+2*Yc1*d-2*a*d;
var C = Math.pow(Xc1,2)+Math.pow(Yc1,2)-2*Yc1*a+Math.pow(a,2)-Math.pow(Rc1,2);
var delta = Math.pow(B,2)-4*A*C;
var XIa = (-B+Math.sqrt(delta))/(2*A);
var XIb = (-B-Math.sqrt(delta))/(2*A);
var YIa = a-((-B+Math.sqrt(delta))/(2*A))*d;
var YIb = a-((-B-Math.sqrt(delta))/(2*A))*d;
}
if (isNaN(XIa) || isNaN(YIa) || isNaN(XIb) || isNaN(YIb))
return false; // si les cercles ne se touchent pas ou bien sont identiques
return [[XIa,YIa],[XIb,YIb]]; // coordonnées des deux points d'intersection [abscisse,ordonnée] (nb : seront identiques si les cercles ne se touchent qu'en un seul point)
}
// exemples :
console.log(calculer_intersections_cercles([0,50,100],[50,0,100])); // cercles qui se croisent
console.log(calculer_intersections_cercles([0,0,100],[0,75,100])); // cercles sur la même abscisse qui se croisent
console.log(calculer_intersections_cercles([0,0,100],[0,200,100])); // cercles sur la même abscisse qui ne se croisent qu'un en point
console.log(calculer_intersections_cercles([0,0,100],[0,300,100])); // cercles qui ne se croisent pas
console.log(calculer_intersections_cercles([0,0,100],[0,0,100])); // cercles identiques
// Sources :
// http://nains-games.over-blog.com/2014/12/intersection-de-deux-cercles.html
// http://nains-games.over-blog.com/2016/05/intersection-de-deux-cercles-simplifie-equation-de-soulgaal.html
Comments