All files / tools/src/utilities/math/polyline getFirstLineSegmentIntersectionIndexes.ts

0% Statements 0/12
0% Branches 0/5
0% Functions 0/1
0% Lines 0/11

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                                                                                     
import type { Types } from '@cornerstonejs/core';
import areLineSegmentsIntersecting from './areLineSegmentsIntersecting';
 
/**
 * Checks whether the line (`p1`,`q1`) intersects any of the other lines in the
 * `points`, and returns the first value.
 *
 * @param points - Polyline points
 * @param p1 - First point of the line segment that is being tested
 * @param q1 - Second point of the line segment that is being tested
 * @param closed - Test the intersection with the line segment that connects
 *   the last and first points of the polyline
 * @returns Indexes of the line segment points from the polyline that intersects [p1, q1]
 */
export default function getFirstLineSegmentIntersectionIndexes(
  points: Types.Point2[],
  p1: Types.Point2,
  q1: Types.Point2,
  closed = true
): Types.Point2 | undefined {
  let initialI;
  let j;
 
  if (closed) {
    j = points.length - 1;
    initialI = 0;
  } else {
    j = 0;
    initialI = 1;
  }
 
  for (let i = initialI; i < points.length; i++) {
    const p2 = points[j];
    const q2 = points[i];
 
    if (areLineSegmentsIntersecting(p1, q1, p2, q2)) {
      return [j, i];
    }
 
    j = i;
  }
}