Class: Vector2

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector2

Returns a new instance of Vector2.



6
7
8
9
# File 'lib/vector2.rb', line 6

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

Instance Attribute Details

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Object



58
59
60
# File 'lib/vector2.rb', line 58

def self.[](x, y)
  Vector2.new(x, y)
end

Instance Method Details

#*(scalar) ⇒ Object



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

def *(scalar)
  Vector2[@x * scalar, @y * scalar]
end

#+(other) ⇒ Object



11
12
13
# File 'lib/vector2.rb', line 11

def +(other)
  Vector2[@x + other[0], @y + other[1]]
end

#-(other) ⇒ Object



15
16
17
# File 'lib/vector2.rb', line 15

def -(other)
  Vector2[@x - other[0], @y - other[1]]
end

#/(scalar) ⇒ Object



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

def /(scalar)
  Vector2[@x / scalar, @y / scalar]
end

#==(other) ⇒ Object



27
28
29
# File 'lib/vector2.rb', line 27

def ==(other)
  @x == other[0] && @y == other[1]
end

#[](index) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/vector2.rb', line 31

def [](index)
  case index
  when 0 then @x
  when 1 then @y
  else nil
  end
end

#[]=(index, value) ⇒ Object



39
40
41
42
43
44
# File 'lib/vector2.rb', line 39

def []=(index, value)
  case index
  when 0 then @x = value
  when 1 then @y = value
  end
end

#sumObject



54
55
56
# File 'lib/vector2.rb', line 54

def sum
  @x + @y
end

#to_aObject



50
51
52
# File 'lib/vector2.rb', line 50

def to_a
  [@x, @y]
end

#to_sObject



46
47
48
# File 'lib/vector2.rb', line 46

def to_s
  self.to_a.to_s
end