Created
June 16, 2011 12:32
-
-
Save mrkn/1029140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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