# Solution to Rosetta Code Challenge - Circles of given radius through two points

What is your hint or solution suggestion?
Solution :

``````function getCircles(...args) {

const point1 = args;
const point2 = args;

let a = [],
b = [];

// exceptional cases
}
if (point1 == point2 && point1 == point2) {
return "Coincident point. Infinite solutions";
}

//find midpoint
let midpoint = [(point1 + point2) / 2, (point1 + point2) / 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 - point2) / Math.abs(point1 - point2);
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 - point1, 2) + Math.pow(midpoint - point1, 2));

//points are on the diameter
return midpoint;
}
//No intersection
return "No intersection. Points further apart than circle diameter";

}

let dx = (l / Math.sqrt(1 + (m * m)));
let dy = m * dx;
a = parseFloat((midpoint + dx).toFixed(4));
a = parseFloat((midpoint + dy).toFixed(4));
b = parseFloat((midpoint - dx).toFixed(4));
b = parseFloat((midpoint - 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

Summary
``````function getCircles(point1, point2, radius) {
// Exceptional cases
}
if (point1 === point2 && point1 === point2) {
return "Coincident point. Infinite solutions";
}

// Find midpoint
const midpoint = [(point1 + point2) / 2, (point1 + point2) / 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. */

const slope = Math.abs(point1 - point2) / Math.abs(point1 - point2);
const slopePerpendicular = 1 / slope;

/* Now find the distance between the centres and the midpoint (l).
The two centres will be equidistant from the midpoint */
/* Distance is the distance of midpoint from point1*/
const distance = Math.sqrt(Math.pow(midpoint - point1, 2) + Math.pow(midpoint - point1, 2));

// Points are on the diameter
return midpoint;
}
// No intersection
return "No intersection. Points further apart than circle diameter";
}