Module: Vector2d::Transformations
- Included in:
- Vector2d
- Defined in:
- lib/vector2d/transformations.rb
Instance Method Summary collapse
-
#ceil ⇒ Object
Rounds vector to up nearest integer.
-
#floor ⇒ Object
Rounds vector to up nearest integer.
-
#normalize ⇒ Object
Normalizes the vector.
-
#perpendicular ⇒ Object
Returns a perpendicular vector.
-
#resize(new_length) ⇒ Object
Changes magnitude of vector.
-
#reverse ⇒ Object
Reverses the vector.
-
#round ⇒ Object
Rounds vector to nearest integer.
-
#truncate(max) ⇒ Object
Truncates to max length if vector is longer than max.
Instance Method Details
#ceil ⇒ Object
Rounds vector to up nearest integer.
Vector2d(2.4, 3.6).ceil # => Vector2d(3,4)
9 10 11 |
# File 'lib/vector2d/transformations.rb', line 9 def ceil self.class.new(x.ceil, y.ceil) end |
#floor ⇒ Object
Rounds vector to up nearest integer.
Vector2d(2.4, 3.6).floor # => Vector2d(2,3)
17 18 19 |
# File 'lib/vector2d/transformations.rb', line 17 def floor self.class.new(x.floor, y.floor) end |
#normalize ⇒ Object
Normalizes the vector.
vector = Vector2d(2, 3)
vector.normalize # => Vector2d(0.5547.., 0.8320..)
vector.normalize.length # => 1.0
27 28 29 |
# File 'lib/vector2d/transformations.rb', line 27 def normalize resize(1.0) end |
#perpendicular ⇒ Object
Returns a perpendicular vector.
Vector2d(2, 3).perpendicular # => Vector2d(-3,2)
35 36 37 |
# File 'lib/vector2d/transformations.rb', line 35 def perpendicular Vector2d.new(-y, x) end |
#resize(new_length) ⇒ Object
Changes magnitude of vector.
Vector2d(2, 3).resize(1.0) # => Vector2d(0.5547.., 0.8320..)
43 44 45 |
# File 'lib/vector2d/transformations.rb', line 43 def resize(new_length) self * (new_length / self.length) end |
#reverse ⇒ Object
Reverses the vector.
Vector2d(2, 3).reverse # => Vector2d(-2,-3)
51 52 53 |
# File 'lib/vector2d/transformations.rb', line 51 def reverse self.class.new(-x, -y) end |
#round ⇒ Object
Rounds vector to nearest integer.
Vector2d(2.4, 3.6).round # => Vector2d(2,4)
59 60 61 |
# File 'lib/vector2d/transformations.rb', line 59 def round self.class.new(x.round, y.round) end |
#truncate(max) ⇒ Object
Truncates to max length if vector is longer than max.
vector = Vector2d(2.0, 3.0)
vector.truncate(5.0) # => Vector2d(2.0, 3.0)
vector.truncate(1.0) # => Vector2d(0.5547.., 0.8320..)
69 70 71 |
# File 'lib/vector2d/transformations.rb', line 69 def truncate(max) resize([max, self.length].min) end |