Class: Kawaii::Vector2
- Inherits:
-
Object
- Object
- Kawaii::Vector2
- Defined in:
- lib/kawaii/vector2.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #direction ⇒ Object
-
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
- #length ⇒ Object
- #normalize ⇒ Object
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector2
Returns a new instance of Vector2.
6 7 8 |
# File 'lib/kawaii/vector2.rb', line 6 def initialize x = 0, y = 0 @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/kawaii/vector2.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/kawaii/vector2.rb', line 4 def y @y end |
Instance Method Details
#direction ⇒ Object
22 23 24 |
# File 'lib/kawaii/vector2.rb', line 22 def direction Math::atan2(y, x) end |
#length ⇒ Object
18 19 20 |
# File 'lib/kawaii/vector2.rb', line 18 def length Math::sqrt((@x * @x) + (@y * @y)) end |
#normalize ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/kawaii/vector2.rb', line 10 def normalize length = Math::sqrt((@x * @x) + (@y * @y)) if (length != 0) @x = @x / length @y = @y / length end end |