Class: Vector2
- Inherits:
-
Object
- Object
- Vector2
- Defined in:
- lib/vector2.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(scalar) ⇒ Object
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
-
#initialize(x, y) ⇒ Vector2
constructor
A new instance of Vector2.
- #sum ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
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
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/vector2.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/vector2.rb', line 4 def y @y end |
Class Method Details
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 |
#sum ⇒ Object
54 55 56 |
# File 'lib/vector2.rb', line 54 def sum @x + @y end |
#to_a ⇒ Object
50 51 52 |
# File 'lib/vector2.rb', line 50 def to_a [@x, @y] end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/vector2.rb', line 46 def to_s self.to_a.to_s end |