angle_between +/- results

Been a while since I’ve been in a math course but this struck me an not quite correct - and my program does not work correctly. So:
Consider 3 vectors
1, 0, 0 is the reference - the x axis, X
0,0,1 is vector A, the z axis (up)
0,0,-1 is vector B, the -z axis (down)

Now, the angle from A to X is 1.57 radians. So, also, says angle_between.
But, angle_between says B to X is ALSO 1.57 radians.

This cannot be right. B is opposite A. B to X must be 6.123 radians.

OK, some code:
vRef = Geom::Vector3d 1,0,0
vA = Geom::Vector3d 0,0,1
vB = Geom::Vector3d 0,0,-1
puts "vRef to vA is #{vRef.angle_between vA} "
puts “vRef to vB is #{vRef.angle_between vB}”

Results:
vRef to vA is 1.5707963267948966
vRef to vB is 1.5707963267948966
NOT!
You might ask - what is the reference? The right-hand rule via the cross product of the two vectors. Note also the normal of a face - it must be + or - depending on the order of the points that define the face - again by the right-hand rule.

angle_between always returns a positive. It may not be what you expect, but it is not technically incorrect. If you need angles greater than 180 or direction in the form of +/- then you need to do the math yourself. The Ruby API does not have those methods.

Ah ha - then the answer is simple. Use the angle provided by angle_between, Then use the sum of the product of the coefficients to determine the sign: i.e.
a1b1 + a2b2 + a3*b3
The sign of this is the sign of the angle. Simple and cheap to calculate. Right?

1 Like