# How can I get the angle between two 2-component 3d angles?

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.

UPDATE:

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)
}
}

return xDegrees*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?

UPDATE:

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

• Try using spherical coordinates. – user112358 Mar 8 '16 at 1:30
• @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.. – 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.
• 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 – user112358 Mar 8 '16 at 7:37