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 v3 = 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)

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..

(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:

A copy of v.

Type
module:twgl/v3.Vec3

(static) create() → {Vec3}

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

Returns:

the created vector

Type
Vec3

(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:

The vector a cross b.

Type
module:twgl/v3.Vec3

(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:

distance between a and b

Type
number

(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:

square of the distance between a and b

Type
number

(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:

The vector of quotients of entries of a and
b.

Type
module:twgl/v3.Vec3

(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:

dst.

Type
module:twgl/v3.Vec3

(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:

dot product

Type
number

(static) length(v) → {number}

Computes the length of vector

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

vector.

Returns:

length of vector.

Type
number

(static) lengthSq(v) → {number}

Computes the square of the length of vector

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

vector.

Returns:

square of the length of vector.

Type
number

(static) lerp(a, b, t, dstopt)

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

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..

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

Mutiplies 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:

dst.

Type
module:twgl/v3.Vec3

(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:

The vector of products of entries of a and
b.

Type
module:twgl/v3.Vec3

(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:

-v.

Type
module:twgl/v3.Vec3

(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:

The normalized vector.

Type
module:twgl/v3.Vec3

(static) subtract(a, b, dstopt)

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..

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