Class: Mittsu::Vector

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

Direct Known Subclasses

Vector2, Vector3, Vector4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Vector

Returns a new instance of Vector.



5
6
7
# File 'lib/mittsu/math/vector.rb', line 5

def initialize(elements)
  @elements = elements
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



3
4
5
# File 'lib/mittsu/math/vector.rb', line 3

def elements
  @elements
end

#indexObject

Returns the value of attribute index.



3
4
5
# File 'lib/mittsu/math/vector.rb', line 3

def index
  @index
end

#uvObject

Returns the value of attribute uv.



3
4
5
# File 'lib/mittsu/math/vector.rb', line 3

def uv
  @uv
end

Instance Method Details

#==(v) ⇒ Object



221
222
223
224
225
226
# File 'lib/mittsu/math/vector.rb', line 221

def ==(v)
  each_dimension do |i|
    return false if @elements[i] != v.elements[i]
  end
  true
end

#[](index) ⇒ Object

Raises:

  • (IndexError)


26
27
28
29
30
# File 'lib/mittsu/math/vector.rb', line 26

def [](index)
  index = self.class::ELEMENTS[index] if index.is_a?(Symbol)
  raise IndexError if index.nil? || index < 0 || index >= self.class::DIMENSIONS
  @elements[index]
end

#[]=(index, value) ⇒ Object

Raises:

  • (IndexError)


20
21
22
23
24
# File 'lib/mittsu/math/vector.rb', line 20

def []=(index, value)
  index = self.class::ELEMENTS[index] if index.is_a?(Symbol)
  raise IndexError if index.nil? || index < 0 || index >= self.class::DIMENSIONS
  @elements[index] = value
end

#add(v) ⇒ Object



37
38
39
40
41
42
# File 'lib/mittsu/math/vector.rb', line 37

def add(v)
  each_dimension do |i|
    @elements[i] = @elements[i] + v.elements[i]
  end
  self
end

#add_scalar(s) ⇒ Object



44
45
46
47
# File 'lib/mittsu/math/vector.rb', line 44

def add_scalar(s)
  @elements.map!{ |e| e + s }
  self
end

#add_vectors(a, b) ⇒ Object



49
50
51
52
53
54
# File 'lib/mittsu/math/vector.rb', line 49

def add_vectors(a, b)
  each_dimension do |i|
    @elements[i] = a.elements[i] + b.elements[i]
  end
  self
end

#angle_to(v) ⇒ Object



210
211
212
213
214
215
# File 'lib/mittsu/math/vector.rb', line 210

def angle_to(v)
  theta = self.dot(v) / (self.length * v.length)

  # clamp, to handle numerical problems
  ::Math.acos(Math.clamp(theta, -1.0, 1.0))
end

#ceilObject



138
139
140
141
# File 'lib/mittsu/math/vector.rb', line 138

def ceil
  @elements.map!{ |e| e.ceil.to_f }
  self
end

#clamp(min, max) ⇒ Object



121
122
123
124
125
126
# File 'lib/mittsu/math/vector.rb', line 121

def clamp(min, max)
  each_dimension do |i|
    @elements[i] = Math.clamp(@elements[i], min.elements[i], max.elements[i])
  end
  self
end

#clamp_scalar(min, max) ⇒ Object



128
129
130
131
# File 'lib/mittsu/math/vector.rb', line 128

def clamp_scalar(min, max)
  @elements.map!{ |e| Math.clamp(e, min, max) }
  self
end

#cloneObject



243
244
245
# File 'lib/mittsu/math/vector.rb', line 243

def clone
  self.class.new(*@elements)
end

#copy(v) ⇒ Object



32
33
34
35
# File 'lib/mittsu/math/vector.rb', line 32

def copy(v)
  @elements = v.elements.dup
  self
end

#distance_to(v) ⇒ Object



217
218
219
# File 'lib/mittsu/math/vector.rb', line 217

def distance_to(v)
  ::Math.sqrt(self.distance_to_squared(v))
end

#divide(v) ⇒ Object



94
95
96
97
98
99
# File 'lib/mittsu/math/vector.rb', line 94

def divide(v)
  each_dimension do |i|
    @elements[i] = @elements[i] / v.elements[i]
  end
  self
end

#divide_scalar(s) ⇒ Object



101
102
103
104
105
# File 'lib/mittsu/math/vector.rb', line 101

def divide_scalar(s)
  inv_scalar = s == 0 ? 0 : 1.0 / s
  @elements.map!{ |e| e * inv_scalar }
  self
end

#each_dimensionObject



14
15
16
17
18
# File 'lib/mittsu/math/vector.rb', line 14

def each_dimension
  self.class::DIMENSIONS.times do |i|
    yield i
  end
end

#floorObject



133
134
135
136
# File 'lib/mittsu/math/vector.rb', line 133

def floor
  @elements.map!{ |e| e.floor.to_f }
  self
end

#from_array(array, offset = 0) ⇒ Object



228
229
230
231
232
233
# File 'lib/mittsu/math/vector.rb', line 228

def from_array(array, offset = 0)
  each_dimension do |i|
    @elements[i] = array[offset + i]
  end
  self
end

#lengthObject



162
163
164
# File 'lib/mittsu/math/vector.rb', line 162

def length
  ::Math.sqrt(length_sq)
end

#length_sqObject



158
159
160
# File 'lib/mittsu/math/vector.rb', line 158

def length_sq
  self.dot(self)
end

#lerp(v, alpha) ⇒ Object



178
179
180
181
182
183
# File 'lib/mittsu/math/vector.rb', line 178

def lerp(v, alpha)
  each_dimension do |i|
    @elements[i] += (v.elements[i] - @elements[i]) * alpha
  end
  self
end

#lerp_vectors(v1, v2, alpha) ⇒ Object



185
186
187
188
# File 'lib/mittsu/math/vector.rb', line 185

def lerp_vectors(v1, v2, alpha)
  self.sub_vectors(v2, v1).multiply_scalar(alpha).add(v1)
  self
end

#max(v) ⇒ Object



114
115
116
117
118
119
# File 'lib/mittsu/math/vector.rb', line 114

def max(v)
  each_dimension do |i|
    @elements[i] = v.elements[i] if @elements[i] < v.elements[i]
  end
  self
end

#min(v) ⇒ Object



107
108
109
110
111
112
# File 'lib/mittsu/math/vector.rb', line 107

def min(v)
  each_dimension do |i|
    @elements[i] = v.elements[i] if @elements[i] > v.elements[i]
  end
  self
end

#multiply(v) ⇒ Object



75
76
77
78
79
80
# File 'lib/mittsu/math/vector.rb', line 75

def multiply(v)
  each_dimension do |i|
    @elements[i] = @elements[i] * v.elements[i]
  end
  self
end

#multiply_scalar(s) ⇒ Object



82
83
84
85
# File 'lib/mittsu/math/vector.rb', line 82

def multiply_scalar(s)
  @elements.map!{ |e| e * s }
  self
end

#multiply_vectors(a, b) ⇒ Object



87
88
89
90
91
92
# File 'lib/mittsu/math/vector.rb', line 87

def multiply_vectors(a, b)
  each_dimension do |i|
    @elements[i] = a.elements[i] * b.elements[i]
  end
  self
end

#negateObject



153
154
155
156
# File 'lib/mittsu/math/vector.rb', line 153

def negate
  @elements.map!{ |e| -e }
  self
end

#normalizeObject



166
167
168
# File 'lib/mittsu/math/vector.rb', line 166

def normalize
  self.divide_scalar(self.length)
end

#project_on_plane(plane_normal) ⇒ Object



197
198
199
200
201
# File 'lib/mittsu/math/vector.rb', line 197

def project_on_plane(plane_normal)
  v1 = self.class.new
  v1.copy(self).project_on_vector(plane_normal)
  self.sub(v1)
end

#project_on_vector(vector) ⇒ Object



190
191
192
193
194
195
# File 'lib/mittsu/math/vector.rb', line 190

def project_on_vector(vector)
  v1 = self.class.new
  v1.copy(vector).normalize
  dot = self.dot(v1)
  self.copy(v1).multiply_scalar(dot)
end

#reflect(normal) ⇒ Object



203
204
205
206
207
208
# File 'lib/mittsu/math/vector.rb', line 203

def reflect(normal)
  # reflect incident vector off plane orthogonal to normal
  # normal is assumed to have unit length
  v1 = self.class.new
  self.sub(v1.copy(normal).multiply_scalar(2.0 * self.dot(normal)))
end

#roundObject



143
144
145
146
# File 'lib/mittsu/math/vector.rb', line 143

def round
  @elements.map!{ |e| e.round.to_f }
  self
end

#round_to_zeroObject



148
149
150
151
# File 'lib/mittsu/math/vector.rb', line 148

def round_to_zero
  @elements.map!{ |e| (e < 0) ? e.ceil.to_f : e.floor.to_f }
  self
end

#set(elements) ⇒ Object



9
10
11
12
# File 'lib/mittsu/math/vector.rb', line 9

def set(elements)
  @elements = elements
  self
end

#set_length(l) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/mittsu/math/vector.rb', line 170

def set_length(l)
  old_length = self.length
  if old_length != 0 && l != old_length
    self.multiply_scalar(l / old_length)
  end
  self
end

#sub(v) ⇒ Object



56
57
58
59
60
61
# File 'lib/mittsu/math/vector.rb', line 56

def sub(v)
  each_dimension do |i|
    @elements[i] = @elements[i] - v.elements[i]
  end
  self
end

#sub_scalar(s) ⇒ Object



63
64
65
66
# File 'lib/mittsu/math/vector.rb', line 63

def sub_scalar(s)
  @elements.map!{ |e| e - s }
  self
end

#sub_vectors(a, b) ⇒ Object



68
69
70
71
72
73
# File 'lib/mittsu/math/vector.rb', line 68

def sub_vectors(a, b)
  each_dimension do |i|
    @elements[i] = a.elements[i] - b.elements[i]
  end
  self
end

#to_array(array = [], offset = 0) ⇒ Object Also known as: to_a



235
236
237
238
239
240
# File 'lib/mittsu/math/vector.rb', line 235

def to_array(array = [], offset = 0)
  each_dimension do |i|
    array[offset + i] = @elements[i]
  end
  array
end

#to_sObject



247
248
249
# File 'lib/mittsu/math/vector.rb', line 247

def to_s
  "[#{@elements.join(', ')}]"
end