0

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

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

theta = angleBetween(pointA,pointB)
degreesFromRad(theta)

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.

B T
  • 135
  • Try using spherical coordinates. – user112358 Mar 08 '16 at 01: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: http://math.stackexchange.com/questions/231221/great-arc-distance-between-two-points-on-a-unit-sphere . 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 08 '16 at 02:43

1 Answers1

2

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.

user112358
  • 1,783
  • Thanks. This was helpful for me in doing that: http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors – B T Mar 08 '16 at 06:53
  • 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? – B T Mar 08 '16 at 07:21
  • I added the way I'm coming to that answer into my question. – B T Mar 08 '16 at 07:27
  • In your code, why did you put $0$*deg in the third coordinate? How did you define the angles in your original question? See https://en.wikipedia.org/wiki/Spherical_coordinate_system – user112358 Mar 08 '16 at 07:37
  • 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. – B T Mar 08 '16 at 07:59
  • Yes, the results given by your code completely make sense. Think about what $\theta$ and $\phi$ mean: they are the angles between the vector and the z-axis and x-axis respectively. – user112358 Mar 08 '16 at 17:35
  • Because the z and x axes are perpendicular, I would think the situation is symmetric. Knowing that theta is angle from x and phi is angle from z aren't helping me understand what's going on. Why would the angle between my second two vectors be 0 when they have different a2 (phi) angles? It still doesn't make sense. – B T Mar 08 '16 at 23:05
  • If your angle between the vector and the z-axis is zero, that means your vector is restricted to the z-axis. In that situation it does not matter what your angle between the vector and x-axis is. (Draw a graph you will see this very clearly.) – user112358 Mar 08 '16 at 23:11
  • Ok, looking again at the spherical coordinate wikipedia article helps. I understand you now. That does make sense. Thanks! I'll accept your answer if you add a little more details as to how to use the dot product to calculate the angle - for future readers. – B T Mar 08 '16 at 23:27