All files / packages/tools/src/utilities/contours calculatePerimeter.ts

0% Statements 0/11
0% Branches 0/2
0% Functions 0/1
0% Lines 0/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                                                               
/**
 * Calculates the perimeter of a polyline.
 *
 * @param polyline - The polyline represented as an array of points.
 * @param closed - Indicates whether the polyline is closed or not.
 * @returns The perimeter of the polyline.
 */
function calculatePerimeter(polyline: number[][], closed: boolean): number {
  let perimeter = 0;
 
  for (let i = 0; i < polyline.length - 1; i++) {
    const point1 = polyline[i];
    const point2 = polyline[i + 1];
    perimeter += Math.sqrt(
      Math.pow(point2[0] - point1[0], 2) + Math.pow(point2[1] - point1[1], 2)
    );
  }
 
  if (closed) {
    const firstPoint = polyline[0];
    const lastPoint = polyline[polyline.length - 1];
    perimeter += Math.sqrt(
      Math.pow(lastPoint[0] - firstPoint[0], 2) +
        Math.pow(lastPoint[1] - firstPoint[1], 2)
    );
  }
 
  return perimeter;
}
 
export default calculatePerimeter;