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[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:

Summary
function getCircles(point1, point2, radius) {
  // Exceptional cases
  if (radius === 0) {
    return "Radius Zero";
  }
  if (point1[0] === point2[0] && point1[1] === point2[1]) {
    return "Coincident point. Infinite solutions";
  }

  // Find midpoint
  const 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. */

  const slope = Math.abs(point1[1] - point2[1]) / Math.abs(point1[0] - point2[0]);
  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[0] - point1[0], 2) + Math.pow(midpoint[1] - point1[1], 2));

  if (distance === radius) {
    // Points are on the diameter
    return midpoint;
  }
  if (distance > radius) {
    // No intersection
    return "No intersection. Points further apart than circle diameter";
  }

  const l = Math.sqrt((radius * radius) - (distance * distance))
  const dx = (l / Math.sqrt(1 + (slopePerpendicular * slopePerpendicular)));
  const dy = slopePerpendicular * dx;
  const a = [parseFloat((midpoint[0] + dx).toFixed(4)),
             parseFloat((midpoint[1] + dy).toFixed(4))];
  const b = [parseFloat((midpoint[0] - dx).toFixed(4)),
             parseFloat((midpoint[1] - dy).toFixed(4))];
  return [a, b];
}

Small updates for convention and style.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.