Module: Yeah::Vec3

Defined in:
lib/yeah/vec3.rb

Overview

Vec3 provides methods for manipulating 3-dimensional vectors.

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

Class Method Summary collapse

Class Method Details

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

Returns vector sum

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric, Numeric))

    vector sum


46
47
48
# File 'lib/yeah/vec3.rb', line 46

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

.cross_product(ax, ay, az, bx, by, bz) ⇒ (Numeric, Numeric, Numeric)

Returns vector dot product

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric, Numeric))

    vector dot product


99
100
101
# File 'lib/yeah/vec3.rb', line 99

def cross_product(ax, ay, az, bx, by, bz)
  [ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx]
end

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

Returns distance from first vector to second vector

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • (Numeric)

    distance from first vector to second vector


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

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

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

Returns vector quotient

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • scalar (Numeric)

    value to multiply by

Returns:

  • ((Numeric, Numeric, Numeric))

    vector quotient


77
78
79
# File 'lib/yeah/vec3.rb', line 77

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

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

Returns vector dot product

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • (Numeric)

    vector dot product


88
89
90
# File 'lib/yeah/vec3.rb', line 88

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

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

Returns whether vectors are equal

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • (true, false)

    whether vectors are equal


15
16
17
# File 'lib/yeah/vec3.rb', line 15

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

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

Returns magnitude of vector

Parameters:

  • x (Numeric)

    component

  • y (Numeric)

    component

  • z (Numeric)

    component

Returns:

  • (Numeric)

    magnitude of vector


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

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

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

Returns vector product

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric, Numeric))

    vector product


68
69
70
# File 'lib/yeah/vec3.rb', line 68

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

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

Returns vector of same direction with a magnitude of 1

Parameters:

  • x (Numeric)

    component

  • y (Numeric)

    component

  • z (Numeric)

    component

Returns:

  • ((Numeric, Numeric, Numeric))

    vector of same direction with a magnitude of 1


34
35
36
37
# File 'lib/yeah/vec3.rb', line 34

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

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

Returns vector difference

Parameters:

  • x (Numeric)

    component of first vector

  • y (Numeric)

    component of first vector

  • z (Numeric)

    component of first vector

  • x (Numeric)

    component of second vector

  • y (Numeric)

    component of second vector

  • z (Numeric)

    component of second vector

Returns:

  • ((Numeric, Numeric, Numeric))

    vector difference


57
58
59
# File 'lib/yeah/vec3.rb', line 57

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