Skip to main content

Vector4

Defines a 4D vector (x, y, z, w).

Constructors

Vector4.new

Creates a new Vector4 with the given x, y and z values.

Example

-- Creates a new Vector4 with x=1, y=2, z=3, and assigns the vector to variable "a"
local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new()

print(a) -- Prints '(1, 2, 3, 4)'
print(b) -- Prints '(0, 0, 0, 0)'

Properties

x

The x component of the Vector4.

Example

local a = Vector4.new(1, 2, 3, 4)

print(a.x) -- Prints '1'

y

The y component of the Vector4.

Example

local a = Vector4.new(1, 2, 3, 4)

print(a.y) -- Prints '2'

z

The z component of the Vector4.

Example

local a = Vector4.new(1, 2, 3, 4)

print(a.z) -- Prints '3'

w

The w component of the Vector4.

Example

local a = Vector4.new(1, 2, 3, 4)

print(a.w) -- Prints '4'

Functions

angle(a, b)

Returns the angle (in degrees) between the two vectors.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(Vector4.angle(a, b)) -- Prints '14.335'

dot(a, b)

Returns the dot product of the two vectors.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(Vector4.dot(a, b)) -- Prints '70'

distance(a, b)

Returns the distance between the two vectors.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(Vector4.distance(a, b)) -- Prints '8'

lerp(from, to, t)

Returns a Vector4 where the value of each component is linearly interpolated from the input vectors, by the value specified in "t" (which should be between 0 and 1).

Example

local a = Vector4.new(1, 1, 1, 1)
local b = Vector4.new(2, 2, 2, 2)

print(Vector4.lerp(a, b, 0.5)) -- Prints '(1.5, 1.5, 1.5, 1.5)'

magnitude(a)

Returns the length of the input vector.

Example

local a = Vector4.new(1, 1, 1, 1)

print(Vector4.magnitude(a)) -- Prints '2'

max(a, b)

Returns a Vector4 where the value of each component is the maximum value from the respective component in each of the input vectors.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(4, 3, 2, 1)

print(Vector4.max(a, b)) -- Prints '(4, 3, 3, 4)'

min(a, b)

Returns a Vector4 where the value of each component is the minimum value from the respective component in each of the input vectors.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(4, 3, 2, 1)

print(Vector4.min(a, b)) -- Prints '(1, 2, 2, 1)'

normalize(a)

Returns a Vector4 pointing in the same direction as the input vector, but with a length of 1.

Example

local a = Vector4.new(1, 1, 1, 1)

print(Vector4.normalize(a)) -- Prints '(0.5, 0.5, 0.5, 0.5)'

sqrMagnitude(a)

Returns the squared length of the input vector.

Example

local a = Vector4.new(1, 1, 1, 1)

print(Vector4.sqrMagnitude(a)) -- Prints '4'

Math Operations

Vector4 + Vector4

Adds each of the components of the vector.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(a + b) -- Prints '(6, 8, 10, 12)'

Vector4 - Vector4

Subtracts each of the components of the vector.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(a - b) -- Prints '(-4, -4, -4, -4)'

Vector4 * number

Multiplies each of the components of the vector by the number.

Example

local a = Vector4.new(1, 2, 3, 4)

print(a * 3) -- Prints '(3, 6, 9, 12)'

Vector4 * Vector4

Multiplies each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector4.new(1, 2, 3, 4)
local b = Vector4.new(5, 6, 7, 8)

print(a * b) -- Prints '(5, 12, 21, 32)'

Vector4 / number

Divides each of the components of the vector by the number.

Example

local a = Vector4.new(3, 12, 15, 18)

print(a / 3) -- Prints '(1, 4, 5, 6)'

Vector4 / Vector4

Divides each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector4.new(3, 12, 15, 21)
local b = Vector4.new(5, 6, 7, 3)

print(a / b) -- Prints '(1, 3, 3, 7)'

-Vector4

Returns the negative of the vector.

Example

local a = Vector4.new(1, -2, 3, -4)

print(-a) -- Prints '(-1, 2, -3, 4)'

#Vector4

Returns the length of the vector.

Example

local a = Vector4.new(1, 2, 3, 4)

print(#a) -- Prints '5.477'

Constants

Vector4.zero

Returns a Vector4 where the value of each component is 0.

Example

print(Vector4.zero)    -- Prints '(0, 0, 0, 0)'

Vector4.one

Returns a Vector4 where the value of each component is 1.

Example

print(Vector4.one)    -- Prints '(1, 1, 1, 1)'