Class: Osb::Vector2
- Inherits:
-
Object
- Object
- Osb::Vector2
- Defined in:
- lib/osb/vector2.rb
Overview
Represents a 2d point or vector.
Instance Attribute Summary collapse
-
#x ⇒ Numeric
X coordinate of this vector.
-
#y ⇒ Numeric
Y coordinate of this vector.
Instance Method Summary collapse
-
#!=(vector) ⇒ Boolean
Returns whether two 2d vector are not equal within tolerance.
-
#+(vector) ⇒ Vector2
Add another 2d vector to this one.
-
#-(vector) ⇒ Vector2
Subtract another 2d vector from this one.
-
#==(vector) ⇒ Boolean
Returns whether two 2d vector are equal within tolerance.
-
#clone ⇒ Vector2
Makes a copy of this 2d vector.
-
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
-
#length ⇒ Float
Returns the length of this 2d vector.
-
#to_a ⇒ Array(Float, Float)
Retrieves the coordinates in an Array.
-
#to_s ⇒ String
Returns a string representation of this 2d vector.
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector2
Returns a new instance of Vector2.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/osb/vector2.rb', line 14 def initialize(x = 0, y = 0) Internal.assert_type!(x, [Numeric, Internal::T[Array][Numeric]], "x") Internal.assert_type!(y, Numeric, "y") if x.is_a?(Array) if x.size != 2 raise InvalidValueError, "Must be an Array of 2 Numeric values." end @x = x[0] @y = x[1] else @x = x @y = y end end |
Instance Attribute Details
#x ⇒ Numeric
Returns x coordinate of this vector.
7 8 9 |
# File 'lib/osb/vector2.rb', line 7 def x @x end |
#y ⇒ Numeric
Returns y coordinate of this vector.
9 10 11 |
# File 'lib/osb/vector2.rb', line 9 def y @y end |
Instance Method Details
#!=(vector) ⇒ Boolean
Returns whether two 2d vector are not equal within tolerance
57 58 59 |
# File 'lib/osb/vector2.rb', line 57 def !=(vector) !(self == vector) end |
#+(vector) ⇒ Vector2
Add another 2d vector to this one.
33 34 35 36 |
# File 'lib/osb/vector2.rb', line 33 def +(vector) Internal.assert_type!(vector, Vector2, "vector") Vector2.new(self.x + vector.x, self.y + vector.y) end |
#-(vector) ⇒ Vector2
Subtract another 2d vector from this one.
41 42 43 44 |
# File 'lib/osb/vector2.rb', line 41 def -(vector) Internal.assert_type!(vector, Vector2, "vector") Vector2.new(self.x - vector.x, self.y - vector.y) end |
#==(vector) ⇒ Boolean
Returns whether two 2d vector are equal within tolerance
49 50 51 52 |
# File 'lib/osb/vector2.rb', line 49 def ==(vector) Internal.assert_type!(vector, Vector2, "vector") Math.fuzzy_equal(self.x, vector.x) && Math.fuzzy_equal(self.y, vector.y) end |
#clone ⇒ Vector2
Makes a copy of this 2d vector.
63 64 65 |
# File 'lib/osb/vector2.rb', line 63 def clone Vector2.new(self.x, self.y) end |
#length ⇒ Float
Returns the length of this 2d vector.
81 82 83 |
# File 'lib/osb/vector2.rb', line 81 def length Math.sqrt(self.x**2 + self.y**2) end |
#to_a ⇒ Array(Float, Float)
Retrieves the coordinates in an Array.
69 70 71 |
# File 'lib/osb/vector2.rb', line 69 def to_a [self.x, self.y] end |
#to_s ⇒ String
Returns a string representation of this 2d vector.
75 76 77 |
# File 'lib/osb/vector2.rb', line 75 def to_s self.to_a.to_s end |