Module: Carbuncle::Vectorizable
- Includes:
- Enumerable
- Included in:
- Color, Matrix::Line, Point, Rect, Vector3, Vector4
- Defined in:
- gems/carbuncle-math/mrblib/000-vectorizable.rb,
gems/carbuncle-math/mrblib/rect.rb,
gems/carbuncle-math/mrblib/point.rb,
gems/carbuncle-math/mrblib/matrix.rb,
gems/carbuncle-math/mrblib/vector3.rb,
gems/carbuncle-math/mrblib/vector4.rb
Overview
Represent a vectorizable element, it’s any element than allows vector operations. All vectorizable elements are also Enumerable by default.
Defined Under Namespace
Modules: ClassMethods
Constant Summary
collapse
- CLASS =
Contains which class is a vectorizable of n-elements It is used for vector operations across classes. While Carbuncle::Color and Carbuncle::Rect are both Vectorizable objects of size 4, Vector4 is the more generic one.
{
2 => Carbuncle::Point,
3 => Carbuncle::Vector3,
4 => Carbuncle::Vector4
}.freeze
Constants included
from Enumerable
Enumerable::NONE
Vector Operations
collapse
Instance Method Summary
collapse
Methods included from Enumerable
__update_hash, #all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip
Instance Method Details
153
154
155
156
157
158
159
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 153
def %(other)
dup.tap do |vector|
size.times do |i|
vector[i] %= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
119
120
121
122
123
124
125
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 119
def *(other)
dup.tap do |vector|
size.times do |i|
vector[i] *= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
170
171
172
173
174
175
176
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 170
def **(other)
dup.tap do |vector|
size.times do |i|
vector[i] **= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
85
86
87
88
89
90
91
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 85
def +(other)
dup.tap do |vector|
size.times do |i|
vector[i] += other.is_a?(Numeric) ? other : other[i]
end
end
end
|
102
103
104
105
106
107
108
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 102
def -(other)
dup.tap do |vector|
size.times do |i|
vector[i] -= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
Returns a new vector, with each of it’s values negated.
217
218
219
220
221
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 217
def -@
dup.tap do |vector|
size.times { |i| vector[i] = -vector[i] }
end
end
|
136
137
138
139
140
141
142
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 136
def /(other)
dup.tap do |vector|
size.times do |i|
vector[i] /= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
187
188
189
190
191
192
193
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 187
def <<(other)
dup.tap do |vector|
size.times do |i|
vector[i] <<= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
204
205
206
207
208
209
210
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 204
def >>(other)
dup.tap do |vector|
size.times do |i|
vector[i] >>= other.is_a?(Numeric) ? other : other[i]
end
end
end
|
Returns the n-th element of a vectorizable object
48
49
50
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 48
def [](index)
to_a[index]
end
|
#[]=(index, value) ⇒ self
Sets the n-th element of the devcotirzable object
56
57
58
59
60
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 56
def []=(index, value)
result = to_a
result[index] = value
set(*result)
end
|
#each(&block) ⇒ Object
Iterates over each element on the vectorizable. If no block is given an iterator will be returned.
41
42
43
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 41
def each(&block)
to_a.each(&block)
end
|
62
63
64
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 62
def length
size
end
|
66
67
68
|
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 66
def to_s
inspect
end
|