Class: EhbGameLib::Math::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/ehb_game_lib/math/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector

Returns a new instance of Vector.



6
7
8
9
# File 'lib/ehb_game_lib/math/vector.rb', line 6

def initialize(x, y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/ehb_game_lib/math/vector.rb', line 4

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/ehb_game_lib/math/vector.rb', line 4

def y
  @y
end

Instance Method Details

#*(other) ⇒ Object



31
32
33
# File 'lib/ehb_game_lib/math/vector.rb', line 31

def *(other)
  self.class.new(x * other, y * other)
end

#+(other) ⇒ Object



23
24
25
# File 'lib/ehb_game_lib/math/vector.rb', line 23

def +(other)
  self.class.new(x + other.x, y + other.y)
end

#-(other) ⇒ Object



27
28
29
# File 'lib/ehb_game_lib/math/vector.rb', line 27

def -(other)
  self.class.new(x - other.x, y - other.y)
end

#/(other) ⇒ Object



35
36
37
# File 'lib/ehb_game_lib/math/vector.rb', line 35

def /(other)
  self.class.new(x / other, y / other)
end

#==(other) ⇒ Object



19
20
21
# File 'lib/ehb_game_lib/math/vector.rb', line 19

def ==(other)
  equal?(other)
end

#dot(other) ⇒ Object



39
40
41
# File 'lib/ehb_game_lib/math/vector.rb', line 39

def dot(other)
  x * other.x + y * other.y
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ehb_game_lib/math/vector.rb', line 15

def equal?(other)
  x == other.x && y == other.y
end

#lengthObject



43
44
45
# File 'lib/ehb_game_lib/math/vector.rb', line 43

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

#normalizeObject



47
48
49
# File 'lib/ehb_game_lib/math/vector.rb', line 47

def normalize
  self / length
end

#to_sObject



11
12
13
# File 'lib/ehb_game_lib/math/vector.rb', line 11

def to_s
  "#{x}, #{y}"
end