All files / utils svg-math.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 6/6
100% Lines 12/12

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                            928x 928x                                         174x 17x               157x 157x 157x 174x                   179x                 69x                                     160x 160x    
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
/* eslint-disable prefer-arrow/prefer-arrow-functions */
 
/**
 * Cartesian coordinate
 */
export interface Coordinate {
  x: number;
  y: number;
}
 
/**
 * Converts a polar coordinate to a cartesian
 * @param center - Center point of circle in cartesian coordinate
 * @param radius - Radius of circle
 * @param angleDeg - Angle in degrees
 * @returns cartesian coordinate
 */
export function polarToCartesian(center: Coordinate, radius: number, angleDeg: number): Coordinate {
  const angleRad = ((angleDeg - 90) * Math.PI) / 180;
  return {
    x: center.x + radius * Math.cos(angleRad),
    y: center.y + radius * Math.sin(angleRad)
  };
}
 
/**
 * Creates a SVG path representing an arc
 * @param center - Center of circle in cartesian coordinate
 * @param radius - Radius of circle
 * @param startAngle - Start angel in degrees
 * @param endAngle - End angel in degrees
 * @returns SVG path string
 */
export function makeArc(
  center: Coordinate,
  radius: number,
  startAngle: number,
  endAngle: number
): string {
  // special case for full circle
  if ((endAngle - startAngle) % 360 === 0) {
    return (
      `M ${center.x} ${center.y - radius} ` +
      `A ${radius} ${radius} 0 1 0 ${center.x} ${center.y + radius} ` +
      `A ${radius} ${radius} 0 1 0 ${center.x} ${center.y - radius} z`
    );
  }
 
  // normal arc
  const start = polarToCartesian(center, radius, endAngle);
  const end = polarToCartesian(center, radius, startAngle);
  const largeArc = endAngle - startAngle <= 180 ? '0' : '1';
  return `M ${start.x} ${start.y} A ${radius} ${radius} 0 ${largeArc} 0 ${end.x} ${end.y}`;
}
 
/**
 * Creates a SVG path representing a line
 * @param start - Start coordinate
 * @param end - End coordinate
 * @returns SVG path string
 */
export function makeLine(start: Coordinate, end: Coordinate): string {
  return `M ${start.x} ${start.y} L ${end.x} ${end.y}`;
}
 
/**
 * Creates a SVG path representing a poly line
 * @param points - Object array with x y coordinates
 * @returns SVG path string for a polyline
 */
export function makePolyline(points: Coordinate[]): string {
  return points.map((point, index) => `${index === 0 ? 'M' : 'L'} ${point.x} ${point.y}`).join(' ');
}
 
/**
 * Calculates the relative angle for a given value
 * @param minAngle - min/start angle in degrees
 * @param maxAngle - max/end angle in degrees
 * @param min - Min value
 * @param max - Max value
 * @param value - Value
 * @returns Relative (from minAngle) angle in degrees
 */
export function valueToRelativeAngle(
  minAngle: number,
  maxAngle: number,
  min: number,
  max: number,
  value: number
): number {
  value = Math.max(min, Math.min(max, value));
  return (value * (maxAngle - minAngle)) / (max - min);
}