All files / core/domain/utils/geometry getCircleIntersection.ts

5.88% Statements 1/17
100% Branches 0/0
0% Functions 0/1
5.88% Lines 1/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57    4x                                                                                                            
/* eslint-disable camelcase, @typescript-eslint/camelcase */
 
export default function getCircleIntersection(
  x0: number,
  y0: number,
  r0: number,
  x1: number,
  y1: number,
  r1: number
) {
  // http://stackoverflow.com/a/12219802
 
  // dx and dy are the vertical and horizontal distances between the circle
  // centers:
  const dx = x1 - x0;
  const dy = y1 - y0;
 
  // Determine the straight-line distance between the centers:
  const d = Math.sqrt(dy * dy + dx * dx);
 
  // Check for solvability:
  if (d > r0 + r1) {
    // No solution. circles do not intersect.
    return false;
  }
  if (d < Math.abs(r0 - r1)) {
    // No solution. one circle is contained in the other.
    return false;
  }
 
  // 'point 2' is the point where the line through the circle intersection
  // points crosses the line between the circle centers.
 
  // Determine the distance from point 0 to point 2:
  const a = (r0 * r0 - r1 * r1 + d * d) / (2.0 * d);
 
  // Determine the coordinates of point 2:
  const x2 = x0 + (dx * a) / d;
  const y2 = y0 + (dy * a) / d;
 
  // Determine the distance from point 2 to either of the intersection
  // points:
  const h = Math.sqrt(r0 * r0 - a * a);
 
  // Determine the offsets of the intersection points from point 2:
  const rx = -dy * (h / d);
  const ry = dx * (h / d);
 
  // Determine the absolute intersection points:
  const xi = x2 + rx;
  const xi_prime = x2 - rx;
  const yi = y2 + ry;
  const yi_prime = y2 - ry;
 
  return { xi, xi_prime, yi, yi_prime };
}