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

#%(number) ⇒ Vectorizable #%(vector) ⇒ Vectorizable

Overloads:

  • #%(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the modulo of each value by this number.

    Returns:

  • #%(vector) ⇒ Vectorizable

    Iterates over itself and the vector, returning a new vector with the multiplied values.

    Returns:

Returns:



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

#*(number) ⇒ Vectorizable #*(vector) ⇒ Vectorizable

Overloads:

  • #*(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the multiplication of each value and this number.

    Returns:

  • #*(vector) ⇒ Vectorizable

    iterates over itself and the vector, returning a new vector with the multiplied values.

    Returns:

Returns:



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

#**(number) ⇒ Vectorizable #**(vector) ⇒ Vectorizable

Overloads:

  • #**(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the power of each value by this number.

    Returns:

  • #**(vector) ⇒ Vectorizable

    Iterates over itself and the vector, returning a new vector with the power between values.

    Returns:

Returns:



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

#+(number) ⇒ Vectorizable #+(vector) ⇒ Vectorizable

Overloads:

  • #+(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the addition of each value and this number.

    Examples:

    Point.new(1, 2) + 2 # => Point(3, 4)

    Returns:

  • #+(vector) ⇒ Vectorizable

    iterates over itself and the vector,, returning a new vector with the added values.

    Examples:

    Point.new(1, 2) + Point.new(3, 4) # => Point(4, 5)

    Returns:

Returns:



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

#-(number) ⇒ Vectorizable #-(vector) ⇒ Vectorizable

Overloads:

  • #-(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the substraction of each value by this number.

    Returns:

  • #-(vector) ⇒ Vectorizable

    iterates over itself and the vector,, returning a new vector with the substracted values.

    Returns:

Returns:



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

#-@Vectorizable

Returns a new vector, with each of it’s values negated.

Examples:

-Point.new(1, 2) => Point(-1, -2)

Returns:



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

#/(number) ⇒ Vectorizable #/(vector) ⇒ Vectorizable

Overloads:

  • #/(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the division of each value by this number.

    Returns:

  • #/(vector) ⇒ Vectorizable

    Iterates over itself and the vector, returning a new vector with the divided values.

    Returns:

Returns:



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

#<<(number) ⇒ Vectorizable #<<(vector) ⇒ Vectorizable

Overloads:

  • #<<(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the left shift of each value by this number.

    Returns:

  • #<<(vector) ⇒ Vectorizable

    Iterates over itself and the vector, returning a new vector with the shifted values.

    Returns:

Returns:



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

#>>(number) ⇒ Vectorizable #>>(vector) ⇒ Vectorizable

Overloads:

  • #>>(number) ⇒ Vectorizable

    Iterates over this vector’s values to create a new one with the right shift of each value by this number.

    Returns:

  • #>>(vector) ⇒ Vectorizable

    Iterates over itself and the vector, returning a new vector with the shifted values.

    Returns:

Returns:



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

#[](index) ⇒ Numeric

Returns the n-th element of a vectorizable object

Returns:



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

Returns:

  • (self)


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

#lengthObject



62
63
64
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 62

def length
  size
end

#to_sObject



66
67
68
# File 'gems/carbuncle-math/mrblib/000-vectorizable.rb', line 66

def to_s
  inspect
end