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

0% Statements 0/7
0% Branches 0/2
0% Functions 0/1
0% Lines 0/7

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                                               
import { glMatrix } from 'gl-matrix';
import type { Types } from '@cornerstonejs/core';
import { distanceToPointSquared } from '../point';
 
/**
 * A polyline is considered closed if the start and end points are at the same position
 *
 * @param polyline - Polyline points (2D)
 * @returns True if the polyline is already closed or false otherwise
 */
export default function isClosed(polyline: Types.Point2[]): boolean {
  if (polyline.length < 3) {
    return false;
  }
 
  const numPolylinePoints = polyline.length;
 
  const firstPoint = polyline[0];
  const lastPoint = polyline[numPolylinePoints - 1];
  const distFirstToLastPoints = distanceToPointSquared(firstPoint, lastPoint);
 
  return glMatrix.equals(0, distFirstToLastPoints);
}