Created
August 10, 2016 20:20
-
-
Save badcc/f9411b7054447b6c64ea585e31b9153e to your computer and use it in GitHub Desktop.
Three different ways to calculate angle between two vectors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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