Module: Yeah::Vec2

Defined in:
lib/yeah/vec2.rb

Overview

Vec2 provides methods for manipulating 2-dimensional vectors.

A vector is represented by a sequence of Numeric component values.

Class Method Summary collapse

Class Method Details

.add(ax, ay, bx, by) ⇒ (Numeric, Numeric)

Returns vector sum

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric))

    vector sum


47
48
49
# File 'lib/yeah/vec2.rb', line 47

def add(ax, ay, bx, by)
  [ax + bx, ay + by]
end

.direction(x, y) ⇒ Numeric

Returns angle of vector in radians

Parameters:

  • x (Numeric)

    component

  • y (Numeric)

    component

Returns:

  • (Numeric)

    angle of vector in radians


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

def direction(x, y)
  Math.atan2(y, x)
end

.direction_to(ax, ay, bx, by) ⇒ Numeric

Returns angle from first vector to second vector in radians

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • (Numeric)

    angle from first vector to second vector in radians


100
101
102
# File 'lib/yeah/vec2.rb', line 100

def direction_to(ax, ay, bx, by)
  Math.atan2(by - ay, bx - by)
end

.distance_to(ax, ay, bx, by) ⇒ Numeric

Returns distance from first vector to second vector

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • (Numeric)

    distance from first vector to second vector


91
92
93
# File 'lib/yeah/vec2.rb', line 91

def distance_to(ax, ay, bx, by)
  Math.sqrt((ax - bx) ** 2 + (ay - by) ** 2)
end

.divide(ax, ay, b) ⇒ (Numeric, Numeric)

Returns vector quotient

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • scalar (Numeric)

    value to multiply by

Returns:

  • ((Numeric, Numeric))

    vector quotient


73
74
75
# File 'lib/yeah/vec2.rb', line 73

def divide(ax, ay, b)
  [ax / b, ay / b]
end

.dot_product(ax, ay, bx, by) ⇒ (Numeric, Numeric)

Returns vector dot product

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric))

    vector dot product


82
83
84
# File 'lib/yeah/vec2.rb', line 82

def dot_product(ax, ay, bx, by)
  ax * bx + ay * by
end

.equal?(ax, ay, bx, by) ⇒ true, false

Returns whether vectors are equal

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • (true, false)

    whether vectors are equal


13
14
15
# File 'lib/yeah/vec2.rb', line 13

def equal?(ax, ay, bx, by)
  ax == bx && ay == by
end

.magnitude(x, y) ⇒ Numeric Also known as: length

Returns magnitude of vector

Parameters:

  • x (Numeric)

    component

  • y (Numeric)

    component

Returns:

  • (Numeric)

    magnitude of vector


20
21
22
# File 'lib/yeah/vec2.rb', line 20

def magnitude(x, y)
  Math.sqrt(x ** 2 + y ** 2)
end

.multiply(ax, ay, b) ⇒ (Numeric, Numeric)

Returns vector product

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric))

    vector product


65
66
67
# File 'lib/yeah/vec2.rb', line 65

def multiply(ax, ay, b)
  [ax * b, ay * b]
end

.normalize(x, y) ⇒ (Numeric, Numeric)

Returns vector of same direction with a magnitude of 1

Parameters:

  • x (Numeric)

    component

  • y (Numeric)

    component

Returns:

  • ((Numeric, Numeric))

    vector of same direction with a magnitude of 1


37
38
39
40
# File 'lib/yeah/vec2.rb', line 37

def normalize(x, y)
  mag = magnitude(x, y)
  [x / mag, y / mag]
end

.subtract(ax, ay, bx, by) ⇒ (Numeric, Numeric)

Returns vector difference

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric))

    vector difference


56
57
58
# File 'lib/yeah/vec2.rb', line 56

def subtract(ax, ay, bx, by)
  [ax - bx, ay - by]
end