Class: RubyGL::Vec3
- Inherits:
-
Object
- Object
- RubyGL::Vec3
- Defined in:
- lib/rubygl/math.rb
Instance Method Summary collapse
- #+(other_vector) ⇒ Object
- #-(other_vector) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #cross(other_vector) ⇒ Object
-
#initialize(array = nil) ⇒ Vec3
constructor
A new instance of Vec3.
- #len ⇒ Object
- #norm ⇒ Object
- #norm! ⇒ Object
- #to_a ⇒ Object
- #to_ary ⇒ Object
Constructor Details
#initialize(array = nil) ⇒ Vec3
Returns a new instance of Vec3.
18 19 20 21 22 23 24 25 26 |
# File 'lib/rubygl/math.rb', line 18 def initialize(array = nil) @data = Array.new(3, 0) if array then for i in 0...@data.size @data[i] = array[i] end end end |
Instance Method Details
#+(other_vector) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/rubygl/math.rb', line 28 def +(other_vector) new_vector = Vec3.new() for i in 0...@data.size new_vector[i] = @data[i] + other_vector[i] end new_vector end |
#-(other_vector) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/rubygl/math.rb', line 38 def -(other_vector) new_vector = Vec3.new() for i in 0...@data.size new_vector[i] = @data[i] - other_vector[i] end new_vector end |
#[](index) ⇒ Object
87 88 89 |
# File 'lib/rubygl/math.rb', line 87 def [](index) @data[index] end |
#[]=(index, value) ⇒ Object
91 92 93 |
# File 'lib/rubygl/math.rb', line 91 def []=(index, value) @data[index] = value end |
#cross(other_vector) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/rubygl/math.rb', line 48 def cross(other_vector) new_vector = Vec3.new() new_vector[0] = (@data[1] * other_vector[2]) - (@data[2] * other_vector[1]) new_vector[1] = (@data[0] * other_vector[2]) - (@data[2] * other_vector[0]) new_vector[2] = (@data[0] * other_vector[1]) - (@data[1] * other_vector[0]) new_vector end |
#len ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/rubygl/math.rb', line 77 def len() sum = 0 for i in 0...@data.size sum += @data[i] * @data[i] end Math::sqrt(sum) end |
#norm ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubygl/math.rb', line 66 def norm() new_vector = Vec2.new() for i in 0...@data.size new_vector[i] = @data[i] end new_vector.norm! new_vector end |
#norm! ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/rubygl/math.rb', line 58 def norm!() curr_len = self.len().abs() for i in 0...@data.size @data[i] /= curr_len end end |
#to_a ⇒ Object
99 100 101 |
# File 'lib/rubygl/math.rb', line 99 def to_a() self.to_ary end |
#to_ary ⇒ Object
95 96 97 |
# File 'lib/rubygl/math.rb', line 95 def to_ary() Array.new(@data) end |