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 ⇒ Object
X coordinate of this vector.
-
#y ⇒ Object
Y coordinate of this vector.
Instance Method Summary collapse
-
#!=(vector) ⇒ Boolean
Returns whether two
Vector2are not equal within tolerance. -
#+(vector) ⇒ Vector2
Add another
Vector2to this one. -
#-(vector) ⇒ Vector2
Subtract another
Vector2from this one. -
#==(vector) ⇒ Boolean
Returns whether two
Vector2are equal within tolerance. -
#clone ⇒ Vector2
Makes a copy of this
Vector2. -
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
-
#length ⇒ Float
Returns the length of this
Vector2. -
#to_a ⇒ Array(Float, Float)
Retrieves the coordinates in an Array.
-
#to_s ⇒ String
Returns a string representation of this
Vector2.
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector2
Returns a new instance of Vector2.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/osb/vector2.rb', line 15 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) raise InvalidValueError, "Must be an Array of 2 Numeric values." if x.size != 2 @x = x[0] @y = x[1] else @x = x @y = y end end |
Instance Attribute Details
#x ⇒ Object
Returns x coordinate of this vector.
7 8 9 |
# File 'lib/osb/vector2.rb', line 7 def x @x end |
#y ⇒ Object
Returns y coordinate of this vector.
7 8 9 |
# File 'lib/osb/vector2.rb', line 7 def y @y end |
Instance Method Details
#!=(vector) ⇒ Boolean
Returns whether two Vector2 are not equal within tolerance
56 57 58 |
# File 'lib/osb/vector2.rb', line 56 def !=(vector) !(self == vector) end |
#+(vector) ⇒ Vector2
Add another Vector2 to this one.
32 33 34 35 |
# File 'lib/osb/vector2.rb', line 32 def +(vector) Internal.assert_type!(vector, Vector2, "vector") Vector2.new(self.x + vector.x, self.y + vector.y) end |
#-(vector) ⇒ Vector2
Subtract another Vector2 from this one.
40 41 42 43 |
# File 'lib/osb/vector2.rb', line 40 def -(vector) Internal.assert_type!(vector, Vector2, "vector") Vector2.new(self.x - vector.x, self.y - vector.y) end |
#==(vector) ⇒ Boolean
Returns whether two Vector2 are equal within tolerance
48 49 50 51 |
# File 'lib/osb/vector2.rb', line 48 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 Vector2.
62 63 64 |
# File 'lib/osb/vector2.rb', line 62 def clone Vector2.new(self.x, self.y) end |
#length ⇒ Float
Returns the length of this Vector2.
80 81 82 |
# File 'lib/osb/vector2.rb', line 80 def length Math.sqrt(self.x**2 + self.y**2) end |
#to_a ⇒ Array(Float, Float)
Retrieves the coordinates in an Array.
68 69 70 |
# File 'lib/osb/vector2.rb', line 68 def to_a [self.x, self.y] end |
#to_s ⇒ String
Returns a string representation of this Vector2.
74 75 76 |
# File 'lib/osb/vector2.rb', line 74 def to_s self.to_a.to_s end |