aboutsummaryrefslogtreecommitdiff
path: root/src/arc.ts
blob: d8e1e7d3e655346ff68d479fd5332c6c70b336d0 (plain)
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
function polarToCartesian(
  centerX: number,
  centerY: number,
  radius: number,
  degreesAngle: number
): number[] {
  const radianAngle = (degreesAngle - 90) * Math.PI / 180
  return [
    centerX + radius * Math.cos(radianAngle),
    centerY + radius * Math.sin(radianAngle)
  ]
}

export function describe(
  x: number,
  y: number,
  radius: number,
  startAngle: number,
  endAngle: number
): string {
  const [startX, startY] = polarToCartesian(x, y, radius, endAngle)
  const [endX, endY] = polarToCartesian(x, y, radius, startAngle)
  const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1"
  return [
    "M",
    startX.toString(),
    startY,
    "A",
    radius.toString(),
    radius.toString(),
    "0",
    largeArcFlag,
    "0",
    endX.toString(),
    endY.toString(),
  ].join(" ")
}