Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created January 3, 2024 12:10
Show Gist options
  • Save steveruizok/23dcab3594cd5e2e5c006866dadaeb68 to your computer and use it in GitHub Desktop.
Save steveruizok/23dcab3594cd5e2e5c006866dadaeb68 to your computer and use it in GitHub Desktop.
/**
* Get a point along an arc.
*
* @param center - The arc's center.
* @param radius - The arc's radius.
* @param startAngle - The start point of the arc.
* @param size - The size of the arc.
* @param t - The point along the arc to get.
*/
export function getPointOnArc(
center: VecLike,
radius: number,
startAngle: number,
size: number,
t: number
) {
const angle = startAngle + size * t
return new Vec(center.x + radius * Math.cos(angle), center.y + radius * Math.sin(angle))
}
/**
* Get a bounding box for an arc.
*
* @param center - The arc's center.
* @param radius - The arc's radius.
* @param start - The start point of the arc.
* @param size - The size of the arc.
*/
export function getArcBoundingBox(center: VecLike, radius: number, start: VecLike, size: number) {
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
let maxY = -Infinity
const startAngle = Vec.Angle(center, start)
// Test 20 points along the arc
for (let i = 0; i < 20; i++) {
const angle = startAngle + size * (i / 19)
const x = center.x + radius * Math.cos(angle)
const y = center.y + radius * Math.sin(angle)
minX = Math.min(x, minX)
minY = Math.min(y, minY)
maxX = Math.max(x, maxX)
maxY = Math.max(y, maxY)
}
return new Box(minX, minY, maxX - minX, maxY - minY)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment