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:
Type:
module:twgl/v3.Vec3

A copy of v.

(static) create() → {Vec3}

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

Returns:
Type:
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 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

dst.

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

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:
Type:
module:twgl/v3.Vec3

dst.

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

(inner) 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

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