Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created June 16, 2011 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkn/1029140 to your computer and use it in GitHub Desktop.
Save mrkn/1029140 to your computer and use it in GitHub Desktop.
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