summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/matrix.rb10
-rw-r--r--test/matrix/test_vector.rb2
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 5cc9697714..7639b1de6d 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -2080,7 +2080,7 @@ class Vector
end
#
- # Returns an angle with another vector. Result is within the [0...Math::PI].
+ # Returns an angle with another vector. Result is within the [0..Math::PI].
# Vector[1,0].angle_with(Vector[0,1])
# # => Math::PI / 2
#
@@ -2089,8 +2089,12 @@ class Vector
Vector.Raise ErrDimensionMismatch if size != v.size
prod = magnitude * v.magnitude
raise ZeroVectorError, "Can't get angle of zero vector" if prod == 0
-
- Math.acos( inner_product(v) / prod )
+ dot = inner_product(v)
+ if dot.abs >= prod
+ dot.positive? ? 0 : Math::PI
+ else
+ Math.acos(dot / prod)
+ end
end
#--
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb
index 52785120b1..631b4d809a 100644
--- a/test/matrix/test_vector.rb
+++ b/test/matrix/test_vector.rb
@@ -223,6 +223,8 @@ class TestVector < Test::Unit::TestCase
assert_in_epsilon(Math::PI/2, Vector[1, 0].angle_with(Vector[0, -1]))
assert_in_epsilon(Math::PI/4, Vector[2, 2].angle_with(Vector[0, 1]))
assert_in_delta(0.0, Vector[1, 1].angle_with(Vector[1, 1]), 0.00001)
+ assert_equal(Vector[6, 6].angle_with(Vector[7, 7]), 0.0)
+ assert_equal(Vector[6, 6].angle_with(Vector[-7, -7]), Math::PI)
assert_raise(Vector::ZeroVectorError) { Vector[1, 1].angle_with(Vector[0, 0]) }
assert_raise(Vector::ZeroVectorError) { Vector[0, 0].angle_with(Vector[1, 1]) }