All files / packages/tools/src/utilities/math/point distanceToPointSquared.ts

87.5% Statements 7/8
75% Branches 3/4
100% Functions 1/1
87.5% Lines 7/8

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                        286x       286x 286x 286x 286x 286x       286x    
import type { Types } from '@cornerstonejs/core';
 
type Point = Types.Point2 | Types.Point3;
 
/**
 * Calculates the distance squared of a point to another point
 *
 * @param p1 - x,y or x,y,z of the point
 * @param p2 - x,y or x,y,z of the point
 * @returns distance
 */
export default function distanceToPointSquared(p1: Point, p2: Point): number {
  Iif (p1.length !== p2.length) {
    throw Error('Both points should have the same dimensionality');
  }
 
  const [x1, y1, z1 = 0] = p1;
  const [x2, y2, z2 = 0] = p2;
  const dx = x2 - x1;
  const dy = y2 - y1;
  const dz = z2 - z1;
 
  // Time to square 10M numbers:
  //   (n * n) = 161ms | (n ** 2) = 199ms | Math.pow(n, 2) = 29529ms
  return dx * dx + dy * dy + dz * dz;
}