twgl/v3

Vec3 math math functions.

Almost all functions take an optional dst argument. If it is not passed in the
functions will create a new Vec3. In other words you can do this

var v = v3.cross(v1, v2);  // Creates a new Vec3 with the cross product of v1 x v2.

or

var v = v3.create();
v3.cross(v1, v2, v);  // Puts the cross product of v1 x v2 in v

The first style is often easier but depending on where it's used it generates garbage where
as there is almost never allocation with the second style.

It is always save to pass any vector as the destination. So for example

v3.cross(v1, v2, v1);  // Puts the cross product of v1 x v2 in v1

Methods

(static) add(a, b, dstopt) → {module:twgl/v3.Vec3}

Adds two vectors; assumes a and b have the same dimension.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

A vector tha tis the sum of a and b.

(static) copy(v, dstopt) → {module:twgl/v3.Vec3}

Copies a vector.

Parameters:
Name Type Attributes Description
v module:twgl/v3.Vec3

The vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

A copy of v.

(static) create(xopt, yopt, zopt) → {module:twgl/v3.Vec3}

Creates a vec3; may be called with x, y, z to set initial values.

Parameters:
Name Type Attributes Description
x number <optional>

Initial x value.

y number <optional>

Initial y value.

z number <optional>

Initial z value.

Returns:
Type:
module:twgl/v3.Vec3

the created vector

(static) cross(a, b, dstopt) → {module:twgl/v3.Vec3}

Computes the cross product of two vectors; assumes both vectors have
three entries.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The vector of a cross b.

(static) distance(a, b) → {number}

Computes the distance between 2 points

Parameters:
Name Type Description
a module:twgl/v3.Vec3

vector.

b module:twgl/v3.Vec3

vector.

Returns:
Type:
number

distance between a and b

(static) distanceSq(a, b) → {number}

Computes the square of the distance between 2 points

Parameters:
Name Type Description
a module:twgl/v3.Vec3

vector.

b module:twgl/v3.Vec3

vector.

Returns:
Type:
number

square of the distance between a and b

(static) divide(a, b, dstopt) → {module:twgl/v3.Vec3}

Divides a vector by another vector (component-wise); assumes a and
b have the same length.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The vector of quotients of entries of a and
b.

(static) divScalar(v, k, dstopt) → {module:twgl/v3.Vec3}

Divides a vector by a scalar.

Parameters:
Name Type Attributes Description
v module:twgl/v3.Vec3

The vector.

k number

The scalar.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The scaled vector.

(static) dot(a, b) → {number}

Computes the dot product of two vectors; assumes both vectors have
three entries.

Parameters:
Name Type Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

Returns:
Type:
number

dot product

(static) length(v) → {number}

Computes the length of vector

Parameters:
Name Type Description
v module:twgl/v3.Vec3

vector.

Returns:
Type:
number

length of vector.

(static) lengthSq(v) → {number}

Computes the square of the length of vector

Parameters:
Name Type Description
v module:twgl/v3.Vec3

vector.

Returns:
Type:
number

square of the length of vector.

(static) lerp(a, b, t, dstopt) → {module:twgl/v3.Vec3}

Performs linear interpolation on two vectors.
Given vectors a and b and interpolation coefficient t, returns
a + t * (b - a).

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

t number

Interpolation coefficient.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The linear interpolated result.

(static) lerpV(a, b, t, dstopt) → {module:twgl/v3.Vec3}

Performs linear interpolation on two vectors.
Given vectors a and b and interpolation coefficient vector t, returns
a + t * (b - a).

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

t module:twgl/v3.Vec3

Interpolation coefficients vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

the linear interpolated result.

(static) max(a, b, dstopt) → {module:twgl/v3.Vec3}

Return max values of two vectors.
Given vectors a and b returns
[max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])].

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The max components vector.

(static) min(a, b, dstopt) → {module:twgl/v3.Vec3}

Return min values of two vectors.
Given vectors a and b returns
[min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])].

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The min components vector.

(static) mulScalar(v, k, dstopt) → {module:twgl/v3.Vec3}

Multiplies a vector by a scalar.

Parameters:
Name Type Attributes Description
v module:twgl/v3.Vec3

The vector.

k number

The scalar.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The scaled vector.

(static) multiply(a, b, dstopt) → {module:twgl/v3.Vec3}

Multiplies a vector by another vector (component-wise); assumes a and
b have the same length.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The vector of products of entries of a and
b.

(static) negate(v, dstopt) → {module:twgl/v3.Vec3}

Negates a vector.

Parameters:
Name Type Attributes Description
v module:twgl/v3.Vec3

The vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

-v.

(static) normalize(a, dstopt) → {module:twgl/v3.Vec3}

Divides a vector by its Euclidean length and returns the quotient.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

The vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

The normalized vector.

(static) setDefaultType(ctor) → {constructor}

Sets the type this library creates for a Vec3

Parameters:
Name Type Description
ctor constructor

the constructor for the type. Either Float32Array or Array

Returns:
Type:
constructor

previous constructor for Vec3

(static) subtract(a, b, dstopt) → {module:twgl/v3.Vec3}

Subtracts two vectors.

Parameters:
Name Type Attributes Description
a module:twgl/v3.Vec3

Operand vector.

b module:twgl/v3.Vec3

Operand vector.

dst module:twgl/v3.Vec3 <optional>

vector to hold result. If not new one is created.

Returns:
Type:
module:twgl/v3.Vec3

A vector that is the difference of a and b.

Type Definitions

Vec3

A JavaScript array with 3 values or a Float32Array with 3 values.
When created by the library will create the default type which is Float32Array
but can be set by calling module:twgl/v3.setDefaultType.

Type:
  • Array.<number> | Float32Array