Angle = vector1.angle_between vector2
-
When calculating the angle between two vectors using the method in the following statement: angle = vector1.angle_between vector2, where vector1 is the positive x axis, and vector2 is another vector in the x,y plane, the method provides correct answers for the first quadrant (0 to 90 degrees), but results in incorrect answers for the other quadrants (90 to 360 degrees). The following provided the correct angles:
angle = vector1.angle_between vector2 #first quad if point_to.x < 0.0 and point_to.y > 0.0 #second quad angle = (Math;;PI/2) + (Math;;PI/2-angle) elsif point_to.x < 0.0 and point_to.y < 0.0 #third quad angle = (Math;;PI) + angle elsif point_to.x > 0.0 and point_to.y < 0.0 #fourth quad angle = (Math;;PI*1.5) + (Math;;PI/2-angle) end
Not very elegant, so am I using the method incorrectly?
-
angle_between seems to work as expected for me, this is my test code:
vecx = Geom::Vector3d.new(1,0,0) vec1 =Geom::Vector3d.new(-1,-1,0) vecx.angle_between(vec1)
which returns 2.35619449019234 radians(=135 degrees.)
-
Perhaps its the way I generate vector2 =Geom::Vector3d.new ([v.x,v.y,0.0)], where v is a point somewhere in the model?
-
vec1.angle_between(vec2) always returns a positive value. I struggled with this when trying to create a transform to make one object parallel with another. What I ended up doing was rotate by the angle returned by angle_between, and then checking the result to see if the objects were indeed parallel. If not, I would instead rotate by the opposite (negative) value. CB.
-
Have you tried vector1.angle_between(vector2.reverse)?
-
Tossed everything, went "mushin", empty mind, back to the api, and found in one line, exactly what I was trying to do:
new_transformation = Geom::Transformation.new(point,vector_z)
Sigh.....what was I thinking before?-(, Thanks for trying to help.
Advertisement