Class: Vector
- Inherits:
-
Object
- Object
- Vector
- Defined in:
- lib/rtl/vector.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](idx) ⇒ Object
- #[]=(idx, val) ⇒ Object
- #first ⇒ Object
-
#initialize(x = nil, y = nil) ⇒ Vector
constructor
A new instance of Vector.
- #last ⇒ Object
- #scale(int) ⇒ Object
- #squared ⇒ Object
- #to_s ⇒ Object
- #x ⇒ Object
- #x=(v) ⇒ Object
- #y ⇒ Object
- #y=(v) ⇒ Object
Constructor Details
#initialize(x = nil, y = nil) ⇒ Vector
Returns a new instance of Vector.
2 3 4 |
# File 'lib/rtl/vector.rb', line 2 def initialize x=nil,y=nil @array=[x,y] end |
Instance Method Details
#+(other) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rtl/vector.rb', line 38 def +(other) res=Vector.new @array.each_with_index do |e,i| res[i]=e + other[i] end return res end |
#[](idx) ⇒ Object
30 31 32 |
# File 'lib/rtl/vector.rb', line 30 def [](idx) @array[idx] end |
#[]=(idx, val) ⇒ Object
34 35 36 |
# File 'lib/rtl/vector.rb', line 34 def []=(idx,val) @array[idx]=val end |
#first ⇒ Object
22 23 24 |
# File 'lib/rtl/vector.rb', line 22 def first @array.first end |
#last ⇒ Object
26 27 28 |
# File 'lib/rtl/vector.rb', line 26 def last @array.last end |
#scale(int) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/rtl/vector.rb', line 46 def scale int res=Vector.new @array.each_with_index do |e,i| res[i]=e*int end return res end |
#squared ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rtl/vector.rb', line 54 def squared res=0 @array.each do |e| res+=e*e end return res end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/rtl/vector.rb', line 62 def to_s "[#{@array.join(',')}]" end |
#x ⇒ Object
6 7 8 |
# File 'lib/rtl/vector.rb', line 6 def x return @array[0] end |
#x=(v) ⇒ Object
14 15 16 |
# File 'lib/rtl/vector.rb', line 14 def x=(v) @array[0]=v end |
#y ⇒ Object
10 11 12 |
# File 'lib/rtl/vector.rb', line 10 def y return @array[1] end |
#y=(v) ⇒ Object
18 19 20 |
# File 'lib/rtl/vector.rb', line 18 def y=(v) @array[1]=v end |