Skip to content

Instantly share code, notes, and snippets.

@jernwerber
Last active March 28, 2024 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jernwerber/f2cb1fc89ed25fb34ef0663261be116e to your computer and use it in GitHub Desktop.
Save jernwerber/f2cb1fc89ed25fb34ef0663261be116e to your computer and use it in GitHub Desktop.
Get direction (unit vector tuple)
import math
def get_direction(startPos, endPos):
# returns a direction (unit vector tuple) pointing from startPos to endPos
# startPos and endPos are tuples of x,y positions
sx, sy = startPos
ex, ey = endPos
mag = math.sqrt((ex-sx)**2 + (ey-sy)**2)
return ((ex-sx)/mag, (ey-sy)/mag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment