What is your hint or solution suggestion?
Solution :
function getCircles(...args) {
const point1 = args[0];
const point2 = args[1];
const radius = args[2];
let a = [],
b = [];
// exceptional cases
if (radius == 0) {
return "Radius Zero";
}
if (point1[0] == point2[0] && point1[1] == point2[1]) {
return "Coincident point. Infinite solutions";
}
//find midpoint
let midpoint = [(point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2];
/* find the slope of the line connecting point1 and point2
the line connecting two centres will be perpendicular to the above line
and the slope of this line (m) will be reciprocal of the slope */
let slope = Math.abs(point1[1] - point2[1]) / Math.abs(point1[0] - point2[0]);
let m = 1 / slope;
/* now find the distance between the centres and the midpoint (l). the
two centres will be equidistant from the midpoint */
/* here, dist is the distance of midpoint from point1*/
let dist = Math.sqrt(Math.pow(midpoint[0] - point1[0], 2) + Math.pow(midpoint[1] - point1[1], 2));
if (dist == radius) {
//points are on the diameter
return midpoint;
}
if (dist > radius) {
//No intersection
return "No intersection. Points further apart than circle diameter";
}
let l = Math.sqrt((radius * radius) - (dist * dist))
let dx = (l / Math.sqrt(1 + (m * m)));
let dy = m * dx;
a[0] = parseFloat((midpoint[0] + dx).toFixed(4));
a[1] = parseFloat((midpoint[1] + dy).toFixed(4));
b[0] = parseFloat((midpoint[0] - dx).toFixed(4));
b[1] = parseFloat((midpoint[1] - dy).toFixed(4));
return [a, b];
}
The solution is based on the principle that the line connecting the two centers will always be perpendicular to the line connecting the two random points. These lines will intersect at their midpoints. This is explained with the help of the below diagram -
Once the slope of this line c1c2 is found, the two endpoints of this line (c1 and c2) can be found using the formula explained in the below article.
Challenge: Circles of given radius through two points
Link to the challenge: