If I have two 3d angles like [120 degrees, 40 degrees] and [70 degrees, 90 degrees], how would I calculate the scaler angle between them? All the related answers I see on here are about vectors with lengths, but I just have angles.


I wrote this program based on Lewis's answer, but I don't think its right.

var deg = Math.PI/180
var sin = Math.sin, cos = Math.cos, acos = Math.acos, pow = Math.pow

var cpointA = {r:1, a1: 120*deg, a2: 0*deg}
var cpointB = {r:1, a1: 90*deg, a2: 0*deg}

var pointA = cartesianFromSpherical(cpointA)
var pointB = cartesianFromSpherical(cpointB)

function angleBetween(pointA, pointB) {
  return acos(dot(pointA,pointB)/(mag(pointA)*mag(pointB)))

function dot(a,b) {
  return a.x*b.x + a.y*b.y + a.z*b.z

function mag(point) {
  var x = point.x, y = point.y, z = point.z
  return pow(pow(x,2)+pow(y,2)+pow(z,2), .5) 

function cartesianFromSpherical(sphericalPoint) {
  var r = sphericalPoint.r, a1 = sphericalPoint.a1, a2 = sphericalPoint.a2
  return {
    x: r * sin(a1)*cos(a2),
    y: r * sin(a1)*sin(a2),
    z: r * cos(a1)

function radiansFromDeg(xDegrees) {
  return xDegrees*deg
function degreesFromRad(radians) {
  return radians/deg

theta = angleBetween(pointA,pointB)

This gives the expected answer of 30 degrees. But if I switch a1 and a2 by changing my points to:

var cpointA = {r:1, a1: 0*deg, a2: 120*deg}
var cpointB = {r:1, a1: 0*deg, a2: 90*deg}

I get 0 degrees. I'd still expect 30 degrees. What am I doing wrong here?


Lewis helped me understand that my program above is written correctly and I was just misunderstanding the spherical coordinates.

  • $\begingroup$ Try using spherical coordinates. $\endgroup$ – user112358 Mar 8 '16 at 1:30
  • $\begingroup$ @Lewis That sounds like the right thing to do. Looking into that, the answers related to finding an angle aren't very clear. I found the top answer here which I don't fully understand: math.stackexchange.com/questions/231221/… . I calculate a 0 degree difference if theta1 and theta2 are 0, no matter what phi1 and phi2 are. I must be misunderstanding that somehow.. $\endgroup$ – B T Mar 8 '16 at 2:43

Your can consider those two points on a unit sphere in $\mathbb{R}^3$ and convert their coordinates to Cartesian using the the following:$$x=r\sin{\theta}\cos{\phi}, y=r\sin{\theta}\sin{\phi},z=r\cos{\theta}$$ and then use $v\cdot w=|v||w|\cos(\psi)$ to calculate the angle $\psi$ between them.

| cite | improve this answer | |
  • $\begingroup$ Thanks. This was helpful for me in doing that: wikihow.com/Find-the-Angle-Between-Two-Vectors $\endgroup$ – B T Mar 8 '16 at 6:53
  • $\begingroup$ Ugh, I'm still getting the same result with a completely different method. If I switch theta and phi I get different answers. Is that expected? $\endgroup$ – B T Mar 8 '16 at 7:21
  • $\begingroup$ I added the way I'm coming to that answer into my question. $\endgroup$ – B T Mar 8 '16 at 7:27
  • $\begingroup$ In your code, why did you put $0$*deg in the third coordinate? How did you define the angles in your original question? See en.wikipedia.org/wiki/Spherical_coordinate_system $\endgroup$ – user112358 Mar 8 '16 at 7:37
  • $\begingroup$ Is 0 not a valid angle? I'm essentially smoke testing this 3d equation by putting in two lines that are in the same place - and so are easy to verify with 2d analysis. $\endgroup$ – B T Mar 8 '16 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.