I have a question related to the intersection of circle. I've spent some time on Google already but none of the solution seems to work for now.
I need to get the intersection of three circle. Each of them has a center given in latitude and longitude, and their radius in Meter.
I tried to follow this post Find intersecting point of three circles programmatically and translate the code to javascript :
function calculateThreeCircleIntersection(x0, y0, r0,
x1, y1, r1,
x2, y2, r2){
var a, horizontal_d_1_2_circle, horizontal_d_1_2_circle, distance_1_2_circle, h, rx, ry;
const EPSILON = 0.0000001;
horizontal_d_1_2_circle = x1 - x0;
vertical_d_1_2_circle = y1 - y0;
distance_1_2_circle = Math.sqrt((vertical_d_1_2_circle*vertical_d_1_2_circle) + (horizontal_d_1_2_circle*horizontal_d_1_2_circle));
if (distance_1_2_circle > (r0 + r1))
{
return false;
}
if (distance_1_2_circle < Math.abs(r0 - r1))
{
return false;
}
a = ((r0*r0) - (r1*r1) + (distance_1_2_circle*distance_1_2_circle)) / (2.0 * distance_1_2_circle) ;
point2_x = x0 + (horizontal_d_1_2_circle * a/distance_1_2_circle);
point2_y = y0 + (vertical_d_1_2_circle * a/distance_1_2_circle);
h = Math.sqrt((r0*r0) - (a*a));
rx = -vertical_d_1_2_circle * (h/distance_1_2_circle);
ry = horizontal_d_1_2_circle * (h/distance_1_2_circle);
intersectionPoint1_x = point2_x + rx;
intersectionPoint2_x = point2_x - rx;
intersectionPoint1_y = point2_y + ry;
intersectionPoint2_y = point2_y - ry;
console.log("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," + intersectionPoint2_y + ")");
horizontal_d_1_2_circle = intersectionPoint1_x - x2;
vertical_d_1_2_circle = intersectionPoint1_y - y2;
d1 = Math.sqrt((vertical_d_1_2_circle*vertical_d_1_2_circle) + (horizontal_d_1_2_circle*horizontal_d_1_2_circle));
horizontal_d_1_2_circle = intersectionPoint2_x - x2;
vertical_d_1_2_circle = intersectionPoint2_y - y2;
d2 = Math.sqrt((vertical_d_1_2_circle*vertical_d_1_2_circle) + (horizontal_d_1_2_circle*horizontal_d_1_2_circle));
if(Math.abs(d1 - r2) < EPSILON) {
console.log("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")");
}
else if(Math.abs(d2 - r2) < EPSILON) {
console.log("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint2_x + "," + intersectionPoint2_y + ")"); //here was an error
}
else {
console.log("INTERSECTION Circle1 AND Circle2 AND Circle3:", "NONE");
}
return true;
}
I tried with some dummy value and it worked
But in my case, the center of the circles are in Lat/Lng, which this code doesn't support, so i tried to convert Lat/Lng to Cartesian Coordinate following this post Converting from longitude\latitude to Cartesian coordinates.
function get_cartesian(lat,lon){
const earthRadius = 6367000;
lat = lat * (Math.PI/180);
lon = lon * (Math.PI/180);
var x = earthRadius * Math.cos(lat)*Math.cos(lon);
var y = earthRadius * Math.cos(lat)*Math.sin(lon);
return [x, y];
}
However, when I try to combine the two codes with data that should contains intersecting circles, calculateThreeCircleIntersection return false
calculateThreeCircleIntersection(get_cartesian(50, 4.5)[0],get_cartesian(50, 4.5)[1] , 1000,
get_cartesian(50, 4.501)[0],get_cartesian(50, 4.501)[0] , 1000,
get_cartesian(50.001, 4.501)[0],get_cartesian(50.001, 4.501)[1] , 1000)
It should however return intersections since I tested the values on google map. I know it is a problem with the conversion from Lat/Lng to cartesian, but since I've tested 4 or 5 different way to do the conversion, i'm out of idea.
Does anyone have an idea what should I do ?
To be precise i think i have to find yet another way to convert Lat/Lng to cartesian coordinate, but because i don't even know what they should look like i'm completely lost