Class: EhbGameLib::Math::Vector
- Inherits:
-
Object
- Object
- EhbGameLib::Math::Vector
- Defined in:
- lib/ehb_game_lib/math/vector.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #==(other) ⇒ Object
- #dot(other) ⇒ Object
- #equal?(other) ⇒ Boolean
-
#initialize(x, y) ⇒ Vector
constructor
A new instance of Vector.
- #length ⇒ Object
- #normalize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Vector
Returns a new instance of Vector.
8 9 10 11 |
# File 'lib/ehb_game_lib/math/vector.rb', line 8 def initialize(x, y) @x = x @y = y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
6 7 8 |
# File 'lib/ehb_game_lib/math/vector.rb', line 6 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
6 7 8 |
# File 'lib/ehb_game_lib/math/vector.rb', line 6 def y @y end |
Instance Method Details
#*(other) ⇒ Object
33 34 35 |
# File 'lib/ehb_game_lib/math/vector.rb', line 33 def *(other) self.class.new(x * other, y * other) end |
#+(other) ⇒ Object
25 26 27 |
# File 'lib/ehb_game_lib/math/vector.rb', line 25 def +(other) self.class.new(x + other.x, y + other.y) end |
#-(other) ⇒ Object
29 30 31 |
# File 'lib/ehb_game_lib/math/vector.rb', line 29 def -(other) self.class.new(x - other.x, y - other.y) end |
#/(other) ⇒ Object
37 38 39 |
# File 'lib/ehb_game_lib/math/vector.rb', line 37 def /(other) self.class.new(x / other, y / other) end |
#==(other) ⇒ Object
21 22 23 |
# File 'lib/ehb_game_lib/math/vector.rb', line 21 def ==(other) equal?(other) end |
#dot(other) ⇒ Object
41 42 43 |
# File 'lib/ehb_game_lib/math/vector.rb', line 41 def dot(other) x * other.x + y * other.y end |
#equal?(other) ⇒ Boolean
17 18 19 |
# File 'lib/ehb_game_lib/math/vector.rb', line 17 def equal?(other) x == other.x && y == other.y end |
#length ⇒ Object
45 46 47 |
# File 'lib/ehb_game_lib/math/vector.rb', line 45 def length @length ||= ::Math.sqrt(x * x + y * y) end |
#normalize ⇒ Object
49 50 51 |
# File 'lib/ehb_game_lib/math/vector.rb', line 49 def normalize self / length end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/ehb_game_lib/math/vector.rb', line 13 def to_s "#{x}, #{y}" end |