summaryrefslogtreecommitdiff
path: root/lib/matrix.rb
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-19 17:32:58 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-19 17:32:58 +0000
commit4da89e192a757ee06e00bea15c6fe659d424ca32 (patch)
treef2d7d73dafd99f77013796fd879a987fe0ebdca0 /lib/matrix.rb
parent098127dc2bc417419490a83b94b3e179389828b0 (diff)
* lib/matrix.rb: Add Vector#angle_with
Patch by Egunov Dmitriy [#10442] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48505 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index f7d75b3..4d211dd 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1699,6 +1699,7 @@ end
# * #-@
#
# Vector functions:
+# * #angle_with(v)
# * #inner_product(v), dot(v)
# * #cross_product(v), cross(v)
# * #collect
@@ -2038,6 +2039,20 @@ class Vector
self / n
end
+ #
+ # Returns an angle with another vector. Result is within the [0...Math::PI].
+ # Vector[1,0].angle_with(Vector[0,1])
+ # # => Math::PI / 2
+ #
+ def angle_with(v)
+ raise TypeError, "Expected a Vector, got a #{v.class}" unless v.is_a?(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 )
+ end
+
#--
# CONVERTING
#++