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.



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

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (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

Returns:

  • (Boolean)


17
18
19
# File 'lib/ehb_game_lib/math/vector.rb', line 17

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

#lengthObject



45
46
47
# File 'lib/ehb_game_lib/math/vector.rb', line 45

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

#normalizeObject



49
50
51
# File 'lib/ehb_game_lib/math/vector.rb', line 49

def normalize
  self / length
end

#to_sObject



13
14
15
# File 'lib/ehb_game_lib/math/vector.rb', line 13

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