Class: Kawaii::Vector2

Inherits:
Object
  • Object
show all
Defined in:
lib/kawaii/vector2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/kawaii/vector2.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/kawaii/vector2.rb', line 4

def y
  @y
end

Instance Method Details

#directionObject



22
23
24
# File 'lib/kawaii/vector2.rb', line 22

def direction
  Math::atan2(y, x)
end

#lengthObject



18
19
20
# File 'lib/kawaii/vector2.rb', line 18

def length
  Math::sqrt((@x * @x) + (@y * @y))
end

#normalizeObject



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