ruby-changes:36424
From: marcandre <ko1@a...>
Date: Thu, 20 Nov 2014 02:33:05 +0900 (JST)
Subject: [ruby-changes:36424] marcandRe: r48505 (trunk): * lib/matrix.rb: Add Vector#angle_with
marcandre 2014-11-20 02:32:58 +0900 (Thu, 20 Nov 2014) New Revision: 48505 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48505 Log: * lib/matrix.rb: Add Vector#angle_with Patch by Egunov Dmitriy [#10442] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/matrix.rb trunk/test/matrix/test_vector.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48504) +++ ChangeLog (revision 48505) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Nov 20 02:32:34 2014 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Add Vector#angle_with + Patch by Egunov Dmitriy [#10442] + Thu Nov 20 02:10:31 2014 Nobuyoshi Nakada <nobu@r...> * parse.y (ripper_flush_string_content, parser_parse_string): Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 48504) +++ lib/matrix.rb (revision 48505) @@ -1699,6 +1699,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1699 # * #-@ # # Vector functions: +# * #angle_with(v) # * #inner_product(v), dot(v) # * #cross_product(v), cross(v) # * #collect @@ -2038,6 +2039,20 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L2039 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 #++ Index: NEWS =================================================================== --- NEWS (revision 48504) +++ NEWS (revision 48505) @@ -172,6 +172,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L172 * 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#+. Index: test/matrix/test_vector.rb =================================================================== --- test/matrix/test_vector.rb (revision 48504) +++ test/matrix/test_vector.rb (revision 48505) @@ -182,4 +182,15 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L182 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/