All files / packages/tools/src/utilities/math/angle angleBetweenLines.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 1/1
100% Lines 10/10

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                            7x 7x   7x 7x   7x   7x 7x   7x   7x   7x    
import type { Types } from '@cornerstonejs/core';
import { vec3 } from 'gl-matrix';
 
type Line = [Types.Point3, Types.Point3];
 
/**
 * It returns the angle between two lines in degrees.
 * The angle measured is that between the vectors
 * line1[1]->line1[0] AND line2[0]->line2[1].
 * @param line1 - Line = [p1, p2]
 * @param line2 - Line = [p3, p4]
 * @returns The angle between two lines in degrees.
 */
export default function angleBetweenLines(line1: Line, line2: Line): number {
  const [p1, p2] = line1;
  const [p3, p4] = line2;
 
  const v1 = vec3.sub(vec3.create(), p2, p1);
  const v2 = vec3.sub(vec3.create(), p3, p4);
 
  const dot = vec3.dot(v1, v2);
 
  const v1Length = vec3.length(v1);
  const v2Length = vec3.length(v2);
 
  const cos = dot / (v1Length * v2Length);
 
  const radian = Math.acos(cos);
 
  return (radian * 180) / Math.PI;
}