Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Last active November 21, 2025 17:08
Show Gist options
  • Select an option

  • Save nicksheffield/493f82af9f0a466d4e1339a0f7a27cc0 to your computer and use it in GitHub Desktop.

Select an option

Save nicksheffield/493f82af9f0a466d4e1339a0f7a27cc0 to your computer and use it in GitHub Desktop.
Angle Tools
const angleTools = {
/**
* Calculate the angle between two points in degrees.
*/
angle: (a: { x: number; y: number }, b: { x: number; y: number }) => {
return (Math.atan2(b.y - a.y, b.x - a.x) / Math.PI) * 180
},
/**
* Calculate the distance between two points in a straight line in pixels.
*/
dist: (a: { x: number; y: number }, b: { x: number; y: number }) => {
return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))
},
/**
* Calculate the x and y changes needed to move in a straight line on a specific angle
*/
step: (angle: number, distance: number) => {
return {
x: distance * Math.cos((angle * Math.PI) / 180),
y: distance * Math.sin((angle * Math.PI) / 180),
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment