diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-11-19 17:32:58 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-11-19 17:32:58 +0000 |
commit | 4da89e192a757ee06e00bea15c6fe659d424ca32 (patch) | |
tree | f2d7d73dafd99f77013796fd879a987fe0ebdca0 | |
parent | 098127dc2bc417419490a83b94b3e179389828b0 (diff) | |
download | ruby-4da89e192a757ee06e00bea15c6fe659d424ca32.tar.gz |
* 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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | lib/matrix.rb | 15 | ||||
-rw-r--r-- | test/matrix/test_vector.rb | 11 |
4 files changed, 32 insertions, 0 deletions
@@ -1,3 +1,8 @@ +Thu Nov 20 02:32:34 2014 Marc-Andre Lafortune <ruby-core@marc-andre.ca> + + * lib/matrix.rb: Add Vector#angle_with + Patch by Egunov Dmitriy [#10442] + Thu Nov 20 02:10:31 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> * parse.y (ripper_flush_string_content, parser_parse_string): @@ -172,6 +172,7 @@ with all sufficient information, see the ChangeLog file. * Unary - and + added for Vector and Matrix. * Vector#cross_product generalized to arbitrary dimensions. * Vector#dot and #cross are aliases for #inner_product and #cross_product. + * Vector#angle_with returns the angle with its argument * Pathname * Pathname#/ is aliased to Pathname#+. diff --git a/lib/matrix.rb b/lib/matrix.rb index f7d75b328f..4d211ddd78 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 #++ diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb index a063d2ca12..9607755feb 100644 --- a/test/matrix/test_vector.rb +++ b/test/matrix/test_vector.rb @@ -182,4 +182,15 @@ class TestVector < Test::Unit::TestCase assert_raise(ArgumentError) { Vector[1, 2].cross_product(Vector[2, -1]) } assert_raise(Vector::ErrOperationNotDefined) { Vector[1].cross_product } end + + def test_angle_with + assert_in_epsilon(Math::PI, Vector[1, 0].angle_with(Vector[-1, 0])) + 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_raise(Vector::ZeroVectorError) { Vector[1, 1].angle_with(Vector[0, 0]) } + assert_raise(Vector::ZeroVectorError) { Vector[0, 0].angle_with(Vector[1, 1]) } + assert_raise(Matrix::ErrDimensionMismatch) { Vector[1, 2, 3].angle_with(Vector[0, 1]) } + end end |