Here are some functions I have written for working with 3D points, vectors and angles. If you spot any issues, please let me know.

/* Copyright (c) 2011, 2012 Julian Richardson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ spherical_cartesian( { Phi, Theta, R } ) -> { R*sin(Phi)*cos(Theta), R*cos(Phi), R*sin(Phi)*sin(Theta) }. cartesian_spherical( { X, Y, Z } ) -> R = math:sqrt(X*X+Y*Y+Z*Z), S = math:sqrt(X*X+Z*Z), AZS = if S > 0.0 -> math:asin(Z/S) ; true -> 0.0 end, { math:acos(Y/R), if 0.0 =< X -> AZS ; true -> math:pi() - AZS end, R }. rotate_about_Z(Phi, V) -> RotationByPhiAboutZ = { { cos(Phi), sin(Phi), 0.0 }, % was -sin(Phi) { -sin(Phi), cos(Phi), 0.0 }, % was sin(Phi) { 0.0, 0.0, 1.0 } }, mvmul3(RotationByPhiAboutZ, V). rotate_about_Y(Theta,V) -> RotationByThetaAboutY = { { cos(Theta), 0.0, -sin(Theta) }, { 0.0, 1.0, 0.0 }, { sin(Theta), 0.0, cos(Theta) } }, mvmul3(RotationByThetaAboutY, V). mvmul3( { { A, B, C }, { D, E, F }, { H, I, J } }, { K, L, M } ) -> { A*K+B*L+C*M, D*K+E*L+F*M, H*K+I*L+J*M }. mmmul3( { { A, B, C }, { D, E, F }, { H, I, J } }, { { K, L, M }, { N, O, P }, { Q, R, S} } ) -> { { A*K+B*N+C*Q, A*L+B*O+C*R, A*M+B*P+C*S }, { D*K+E*N+F*Q, D*L+E*O+F*R, D*M+E*P+F*S }, { H*K+I*N+J*Q, H*L+I*O+J*R, H*M+I*P+J*S } }. smmul3(X, { A, B, C }) -> { svmul3(X, A), svmul3(X, B), svmul3(X,C) }. mdet3( { { A, B, C }, { D, E, F }, { G, H, I } } ) -> A*E*I+B*F*G+C*D*H-A*F*H-B*D*I-C*E*G. svmul2(S, {X,Y}) -> {S*X,S*Y}. vvplus2({A,B},{C,D}) -> {A+C,B+D}. vvminus2({A,B},{C,D}) -> {A-C,B-D}. vvplus3({A,B,C},{D,E,F}) -> {A+D,B+E,C+F}. vvminus3({A,B,C},{D,E,F}) -> {A-D,B-E,C-F}. mvmul2({{A,B},{C,D}},{X,Y})-> {A*X+B*Y,C*X+D*Y}. svmul3(S,{X,Y,Z}) -> {S*X, S*Y, S*Z}. normv3(V) -> svmul3(1/len(V),V). len({X,Y,Z}) -> math:sqrt(X*X+Y*Y+Z*Z). vdot2({A,B},{C,D}) -> A*C+B*D. vdot3({A1,A2,A3},{B1,B2,B3}) -> A1*B1+A2*B2+A3*B3. mid3() -> { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }. crossProduct3( { A1, A2, A3 }, { B1, B2, B3 } ) -> { A2*B3-A3*B2, A3*B1-A1*B3, A1*B2-A2*B1 }. vlen3(V) -> math:sqrt(distancev3(cartesian, V, { 0.0, 0.0, 0.0 } )). vlen4({ X, Y, Z, W }) -> math:sqrt(X*X+Y*Y+Z*Z+W*W). qlen(Q) -> vlen4(Q). qnorm(Q) -> S = qlen(Q), { X, Y, Z, W } = Q, { X/S, Y/S, Z/S, W/S }. angle_between(V1,V2) -> math:acos(vdot3(normv3(V1),normv3(V2))). axis_angle_to_quaternion(V,Angle) -> { Ax, Ay, Az } = normv3(V), SinA = math:sin(Angle/2.0), CosA = math:cos(Angle/2.0), qnorm({ Ax*SinA, Ay*SinA, Az*SinA, CosA }). quaternion_to_matrix(Q) -> { X, Y, Z, W } = Q, { { 1 - 2 * (Y*Y+Z*Z), 2 * (X*Y + Z*W), 2 * (X*Z - Y*W) }, { 2 * (X*Y - Z*W), 1 - 2 * (X*X+Z*Z), 2 * (Y*Z + X*W) }, { 2 * (X*Z + Y*W), 2 * (Y*Z-X*W), 1 - 2 * (X*X+Y*Y) } }.