Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 3fda08b..d33b3df 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1479,6 +1479,13 @@ class Vector
end
#
+ # Creates a normalized vector.
+ #
+ def normalize
+ self / self.r
+ end
+
+ #
# Returns the elements of the vector in an array.
#
def to_a
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb
index 582509f..821d692 100644
--- a/test/matrix/test_vector.rb
+++ b/test/matrix/test_vector.rb
@@ -131,4 +131,8 @@ class TestVector < Test::Unit::TestCase
assert_equal("Vector[1, 2, 3]", @v1.inspect)
end
+ def test_normalize
+ assert_equal(Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732],
+ Vector[1, 2, 3].normalize)
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.