Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Last active July 20, 2016 04:22
Show Gist options
  • Save nicksheffield/11204385 to your computer and use it in GitHub Desktop.
Save nicksheffield/11204385 to your computer and use it in GitHub Desktop.
Work out angles and distances between objects that have x and y properties.
// es6
const at = {
angle: (a,b)=>Math.atan2(b.y-a.y,b.x-a.x)/Math.PI*180,
dist: (a,b)=>Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)),
step: (a,s)=>({x:s*Math.cos(a*Math.PI/180),y:s*Math.sin(a*Math.PI/180)})
}
// es5
var at = {
angle: function(a,b){return Math.atan2(b.y-a.y,b.x-a.x)/Math.PI*180},
dist: function(a,b){return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))},
step: function(a,s){return {x:s*Math.cos(a*Math.PI/180),y:s*Math.sin(a*Math.PI/180)}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment