Class: RubyGL::Vec3

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

Instance Method Summary collapse

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

#lenObject



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

#normObject



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_aObject



99
100
101
# File 'lib/rubygl/math.rb', line 99

def to_a()
    self.to_ary
end

#to_aryObject



95
96
97
# File 'lib/rubygl/math.rb', line 95

def to_ary()
    Array.new(@data)
end