Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created October 13, 2020 10:48
Show Gist options
  • Save steveruizok/56004f2204898c8582b921435a655441 to your computer and use it in GitHub Desktop.
Save steveruizok/56004f2204898c8582b921435a655441 to your computer and use it in GitHub Desktop.
Get arc for circle.
/**
* Get an arc (circle segment) between two points.
* @param x0 The x-coordinate of the arc's starting point.
* @param y0 The y-coordinate of the arc's starting point.
* @param a0 The angle in radians of the arc's starting point.
* @param x1 The x-coordinate of the arc's ending point.
* @param y1 The y-coordinate of the arc's ending point.
* @param a1 The angle in radians of the arc's ending point.
* @param r The radius of the circle.
* @param flip Whether to flip the segment.
*/
function getArcPath(
x0: number,
y0: number,
a0: number,
x1: number,
y1: number,
a1: number,
r: number,
flip: boolean
) {
if (a1 < a0) a1 += Math.PI * 2
let largeArc = Math.abs(a1 - a0) > Math.PI ? 1 : 0
if (flip) largeArc = Math.abs(largeArc - 1)
return `M ${x0},${y0} A ${r},${r} 0 ${largeArc} ${
flip ? 0 : 1
} ${x1} ${y1}`
}
// Based on http://madebyevan.com/fsm/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment