Class: Yeah::Vector

Inherits:
Object
  • Object
show all
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.

Examples:

Basic vector math

V[5, 5] + V[10, 20]
# => Yeah::Vector[15, 25, 0]

V[3, 6, 9] / 3
# => Yeah::Vector[1, 2, 3]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*components) ⇒ Vector

Returns a new instance of Vector.

Parameters:

  • first (Numeric)

    component, defaults to 0

  • second (Numeric)

    component, defaults to 0

  • third (Numeric)

    component, defaults to 0



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.

Returns:

  • (Numeric)

    *n*th component value



33
# File 'lib/yeah/vector.rb', line 33

def_delegators :@components, :[]

#componentsArray

Returns vector components.

Returns:

  • (Array)

    vector components



29
30
31
# File 'lib/yeah/vector.rb', line 29

def components
  @components
end

#xNumeric

Returns first component value.

Parameters:

  • first (Numeric)

    component value

Returns:

  • (Numeric)

    first component value



# File 'lib/yeah/vector.rb', line 47

#yNumeric

Returns second component value.

Parameters:

  • second (Numeric)

    component value

Returns:

  • (Numeric)

    second component value



# File 'lib/yeah/vector.rb', line 51

#zNumeric

Returns third component value.

Parameters:

  • third (Numeric)

    component value

Returns:

  • (Numeric)

    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.

Parameters:

  • arguments

    catch-all

Returns:



23
24
25
# File 'lib/yeah/vector.rb', line 23

def [](*args)
  new(*args)
end

Instance Method Details

#*(numeric) ⇒ Vector

Returns vector product.

Parameters:

  • numeric (Numeric)

    to multiply vector by

Returns:



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.

Parameters:

Returns:



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.

Returns:

  • (Vector)

    identical vector



110
111
112
# File 'lib/yeah/vector.rb', line 110

def +@
  self.class.new(*@components)
end

#-(vector) ⇒ Vector

Returns vector difference.

Parameters:

  • vector (Vector)

    to subtract

Returns:

  • (Vector)

    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.

Returns:

  • (Vector)

    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.

Parameters:

  • numeric (Numeric)

    to divide vector by

Returns:

  • (Vector)

    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.

Parameters:

  • angle (Numeric)

    to move along, in radians

  • distance (Numeric)

    to move

Returns:

  • (Vector)

    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.

Parameters:

  • angle (Numeric)

    to move along, in radians

  • distance (Numeric)

    to move

Returns:

  • (Vector)

    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.

Parameters:

Returns:

  • (Numeric)

    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.

Parameters:

Returns:

  • (Numeric)

    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

#inspectString

Returns readable representation.

Returns:

  • (String)

    readable representation



43
44
45
# File 'lib/yeah/vector.rb', line 43

def inspect
  "#{self.class.name}#{@components.to_s}"
end

#lengthNumeric Also known as: magnitude

Returns vector’s length.

Returns:

  • (Numeric)

    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

#normalizeVector Also known as: unit

Returns vector of same direction whose length is 1.

Returns:

  • (Vector)

    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

TODO:

Make work in 3D.

Returns position moved toward other position for a distance in 2D.

Parameters:

  • position (Vector)

    to move to

  • distance (Vector)

    to move

Returns:

  • (Vector)

    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

TODO:

Make work in 3D.

Returns self after moving toward other position for a distance in 2D.

Parameters:

  • position (Vector)

    to move to

  • distance (Vector)

    to move

Returns:

  • (Vector)

    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