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
Vector product.
-
#+(vector) ⇒ Vector
Vector sum.
-
#+@ ⇒ Vector
Identical vector.
-
#-(vector) ⇒ Vector
Vector difference.
-
#-@ ⇒ Vector
Negative vector.
-
#/(numeric) ⇒ Vector
Vector quotient.
-
#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.
-
#initialize(*components) ⇒ Vector
constructor
A new instance of Vector.
-
#inspect ⇒ String
Readable representation.
-
#length ⇒ Numeric
(also: #magnitude)
Vector’s length.
-
#normalize ⇒ Vector
(also: #unit)
Vector of same direction whose length is 1.
-
#toward(position, distance) ⇒ Vector
Position moved toward other position for a distance in 2D.
-
#toward!(position, amount) ⇒ Vector
Self after moving toward other position for a distance in 2D.
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
Returns vector product.
95 96 97 98 99 |
# File 'lib/yeah/vector.rb', line 95 def *(numeric) self.class.new(@components[0] * numeric, @components[1] * numeric, @components[2] * numeric) end |
#+(vector) ⇒ Vector
Returns vector sum.
79 80 81 82 83 |
# File 'lib/yeah/vector.rb', line 79 def +(vector) self.class.new(@components[0] + vector.components[0], @components[1] + vector.components[1], @components[2] + vector.components[2]) end |
#+@ ⇒ Vector
Returns identical vector.
110 111 112 |
# File 'lib/yeah/vector.rb', line 110 def +@ self.class.new(*@components) end |
#-(vector) ⇒ Vector
Returns vector difference.
87 88 89 90 91 |
# File 'lib/yeah/vector.rb', line 87 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.
115 116 117 118 119 |
# File 'lib/yeah/vector.rb', line 115 def -@ self.class.new(-@components[0], -@components[1], -@components[2]) end |
#/(numeric) ⇒ Vector
Returns vector quotient.
103 104 105 106 107 |
# File 'lib/yeah/vector.rb', line 103 def /(numeric) self.class.new(@components[0] / numeric, @components[1] / numeric, @components[2] / numeric) end |
#along(angle, distance) ⇒ Vector
Returns position moved along an angle for a distance in 2D.
139 140 141 142 |
# File 'lib/yeah/vector.rb', line 139 def along(angle, distance) self.class.new(@components[0] + Math.cos(angle) * distance, @components[1] + Math.sin(angle) * distance) end |
#along!(angle, distance) ⇒ Vector
Returns self after moving along an angle for a distance in 2D.
146 147 148 149 150 151 |
# File 'lib/yeah/vector.rb', line 146 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.
131 132 133 134 |
# File 'lib/yeah/vector.rb', line 131 def angle_to(position) diff = position - self Math.atan2(diff.y, diff.x) end |
#distance_to(position) ⇒ Numeric
Returns distance to a position.
123 124 125 126 127 |
# File 'lib/yeah/vector.rb', line 123 def distance_to(position) Math.sqrt((@components[0] - position.x) ** 2 + (@components[1] - position.y) ** 2 + (@components[2] - position.z) ** 2) 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 |
#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 |
#toward(position, distance) ⇒ Vector
Make work in 3D.
Returns position moved toward other position for a distance in 2D.
157 158 159 |
# File 'lib/yeah/vector.rb', line 157 def toward(position, distance) along angle_to(position), amount end |
#toward!(position, amount) ⇒ Vector
Make work in 3D.
Returns self after moving toward other position for a distance in 2D.
165 166 167 |
# File 'lib/yeah/vector.rb', line 165 def toward!(position, amount) along! angle_to(position), amount end |