Class: Yeah::Vector
- Inherits:
-
Object
- Object
- Yeah::Vector
- Extended by:
- Forwardable
- Defined in:
- lib/yeah/vector.rb
Overview
The Vector represents a mathematical vector. It can be used to describe a position, velocity or direction.
V is an alias for Vector.
Instance Attribute Summary collapse
-
#[] ⇒ Numeric
*n*th component value.
-
#components ⇒ Array
Vector components.
-
#x ⇒ Numeric
First component value.
-
#y ⇒ Numeric
Second component value.
-
#z ⇒ Numeric
Third component value.
Class Method Summary collapse
-
.[](*args) ⇒ Vector
Alias for ::new.
Instance Method Summary collapse
-
#*(numeric) ⇒ Vector
(also: #multiply)
Vector product.
-
#+(vector) ⇒ Vector
(also: #add)
Vector sum.
-
#-(vector) ⇒ Vector
(also: #subtract)
Vector difference.
-
#-@ ⇒ Vector
Negative vector.
-
#/(numeric) ⇒ Vector
(also: #divide)
Vector quotient.
-
#==(other) ⇒ Boolean
Whether self equals vector.
-
#add!(vector) ⇒ Vector
Self after adding vector.
-
#along(angle, distance) ⇒ Vector
Position moved along an angle for a distance in 2D.
-
#along!(angle, distance) ⇒ Vector
Self after moving along an angle for a distance in 2D.
-
#angle_to(position) ⇒ Numeric
Angle to 2D position, in radians.
-
#distance_to(position) ⇒ Numeric
Distance to a position.
-
#divide!(numeric) ⇒ Vector
Self after dividing by numeric.
-
#initialize(*components) ⇒ Vector
constructor
A new instance of Vector.
-
#inspect ⇒ String
Readable representation.
-
#length ⇒ Numeric
(also: #magnitude)
Vector’s length.
-
#multiply!(numeric) ⇒ Vector
Self after multiplying by numeric.
-
#normalize ⇒ Vector
(also: #unit)
Vector of same direction whose length is 1.
-
#subtract!(vector) ⇒ Vector
Self after subtracting vector.
-
#toward(position, distance) ⇒ Vector
Position moved toward other position for a distance.
-
#toward!(position, distance) ⇒ Vector
Self after moving toward other position for a distance.
Constructor Details
#initialize(*components) ⇒ Vector
Returns a new instance of Vector.
38 39 40 |
# File 'lib/yeah/vector.rb', line 38 def initialize(*components) @components = components + [0] * (3 - components.size) end |
Instance Attribute Details
#[] ⇒ Numeric
Returns *n*th component value.
33 |
# File 'lib/yeah/vector.rb', line 33 def_delegators :@components, :[] |
#components ⇒ Array
Returns vector components.
29 30 31 |
# File 'lib/yeah/vector.rb', line 29 def components @components end |
#x ⇒ Numeric
Returns first component value.
|
|
# File 'lib/yeah/vector.rb', line 47
|
#y ⇒ Numeric
Returns second component value.
|
|
# File 'lib/yeah/vector.rb', line 51
|
#z ⇒ Numeric
Returns third component value.
58 59 60 61 |
# File 'lib/yeah/vector.rb', line 58 %i[x y z].each_with_index do |component, i| define_method(component) { @components[i] } define_method("#{component}=") { |v| @components[i] = v } end |
Class Method Details
.[](*args) ⇒ Vector
Alias for ::new.
23 24 25 |
# File 'lib/yeah/vector.rb', line 23 def [](*args) new(*args) end |
Instance Method Details
#*(numeric) ⇒ Vector Also known as: multiply
Returns vector product.
101 102 103 104 105 |
# File 'lib/yeah/vector.rb', line 101 def *(numeric) self.class.new(@components[0] * numeric, @components[1] * numeric, @components[2] * numeric) end |
#+(vector) ⇒ Vector Also known as: add
Returns vector sum.
85 86 87 88 89 |
# File 'lib/yeah/vector.rb', line 85 def +(vector) self.class.new(@components[0] + vector.components[0], @components[1] + vector.components[1], @components[2] + vector.components[2]) end |
#-(vector) ⇒ Vector Also known as: subtract
Returns vector difference.
93 94 95 96 97 |
# File 'lib/yeah/vector.rb', line 93 def -(vector) self.class.new(@components[0] - vector.components[0], @components[1] - vector.components[1], @components[2] - vector.components[2]) end |
#-@ ⇒ Vector
Returns negative vector.
166 167 168 169 170 |
# File 'lib/yeah/vector.rb', line 166 def -@ self.class.new(-@components[0], -@components[1], -@components[2]) end |
#/(numeric) ⇒ Vector Also known as: divide
Returns vector quotient.
109 110 111 112 113 |
# File 'lib/yeah/vector.rb', line 109 def /(numeric) self.class.new(@components[0] / numeric, @components[1] / numeric, @components[2] / numeric) end |
#==(other) ⇒ Boolean
Returns whether self equals vector.
79 80 81 |
# File 'lib/yeah/vector.rb', line 79 def ==(other) @components == other.components end |
#add!(vector) ⇒ Vector
Returns self after adding vector.
125 126 127 128 129 130 131 |
# File 'lib/yeah/vector.rb', line 125 def add!(vector) @components[0] += vector.components[0] @components[1] += vector.components[1] @components[2] += vector.components[2] self end |
#along(angle, distance) ⇒ Vector
Returns position moved along an angle for a distance in 2D.
190 191 192 193 194 |
# File 'lib/yeah/vector.rb', line 190 def along(angle, distance) self.class.new(@components[0] + Math.cos(angle) * distance, @components[1] + Math.sin(angle) * distance, @components[2]) end |
#along!(angle, distance) ⇒ Vector
Returns self after moving along an angle for a distance in 2D.
198 199 200 201 202 203 |
# File 'lib/yeah/vector.rb', line 198 def along!(angle, distance) @components[0] += Math.cos(angle) * distance @components[1] += Math.sin(angle) * distance self end |
#angle_to(position) ⇒ Numeric
Returns angle to 2D position, in radians.
182 183 184 185 |
# File 'lib/yeah/vector.rb', line 182 def angle_to(position) diff = position - self Math.atan2(diff.y, diff.x) end |
#distance_to(position) ⇒ Numeric
Returns distance to a position.
174 175 176 177 178 |
# File 'lib/yeah/vector.rb', line 174 def distance_to(position) Math.sqrt((@components[0] - position.x) ** 2 + (@components[1] - position.y) ** 2 + (@components[2] - position.z) ** 2) end |
#divide!(numeric) ⇒ Vector
Returns self after dividing by numeric.
155 156 157 158 159 160 161 |
# File 'lib/yeah/vector.rb', line 155 def divide!(numeric) @components[0] /= numeric @components[1] /= numeric @components[2] /= numeric self end |
#inspect ⇒ String
Returns readable representation.
43 44 45 |
# File 'lib/yeah/vector.rb', line 43 def inspect "#{self.class.name}#{@components.to_s}" end |
#length ⇒ Numeric Also known as: magnitude
Returns vector’s length.
64 65 66 |
# File 'lib/yeah/vector.rb', line 64 def length Math.sqrt(@components[0] ** 2 + @components[1] ** 2 + @components[2] ** 2) end |
#multiply!(numeric) ⇒ Vector
Returns self after multiplying by numeric.
145 146 147 148 149 150 151 |
# File 'lib/yeah/vector.rb', line 145 def multiply!(numeric) @components[0] *= numeric @components[1] *= numeric @components[2] *= numeric self end |
#normalize ⇒ Vector Also known as: unit
Returns vector of same direction whose length is 1.
71 72 73 |
# File 'lib/yeah/vector.rb', line 71 def normalize self / length end |
#subtract!(vector) ⇒ Vector
Returns self after subtracting vector.
135 136 137 138 139 140 141 |
# File 'lib/yeah/vector.rb', line 135 def subtract!(vector) @components[0] -= vector.components[0] @components[1] -= vector.components[1] @components[2] -= vector.components[2] self end |
#toward(position, distance) ⇒ Vector
Returns position moved toward other position for a distance.
208 209 210 |
# File 'lib/yeah/vector.rb', line 208 def toward(position, distance) self + (position - self).unit * distance end |
#toward!(position, distance) ⇒ Vector
Returns self after moving toward other position for a distance.
214 215 216 |
# File 'lib/yeah/vector.rb', line 214 def toward!(position, distance) self.add! (position - self).unit * distance end |