Skip to content

Instantly share code, notes, and snippets.

@badcc
Created August 10, 2016 20:20
Show Gist options
  • Select an option

  • Save badcc/f9411b7054447b6c64ea585e31b9153e to your computer and use it in GitHub Desktop.

Select an option

Save badcc/f9411b7054447b6c64ea585e31b9153e to your computer and use it in GitHub Desktop.
Three different ways to calculate angle between two vectors.
local acos = math.acos
local asin = math.asin
local sqrt = math.sqrt
-- Define unit vectors
local mx = sqrt(1*1+2*2+3*3)
local x,y,z = 1/mx,2/mx,3/mx
local ma = sqrt(4*4+5*5+6*6)
local a,b,c = 4/ma,5/ma,6/ma
-- Dot product
print(acos(x*a+y*b+z*c))
-- Cross product
local cx = y*c-z*b
local cy = z*a-x*c
local cz = x*b-y*a
print(asin((cx*cx+cy*cy+cz*cz)^0.5))
-- Cross product magnitude squared generalization
print(asin(((x*x+y*y+z*z)*(a*a+b*b+c*c)-(x*a+y*b+z*c)^2)^0.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment