A set of functions that perform mathematical operations on 2D Vectors.

- member of jStage

Though create(x, y) initializes a vector as a 2-element Float32Array, any array object with two elements may be used with these functions. See tojicode.com for more information about this implementation.

- add(vec1, vec2, out)
- addValues(vec, x, y, out)
- clone(vec)
- copy(vec, out)
- create(x, y)
- difference(vec1, vec2)
- distance(vec1, vec2)
- distanceSquared(vec1, vec2)
- distanceValues(vec, vx, vy)
- divide(vec1, vec2, out)
- dot(vec1, vec2)
- isZero(vec)
- length(vec)
- lengthSquared(vec)
- max(vec1, out)
- min(vec1, out)
- multiply(vec1, vec2, out)
- multiplyValues(vec, x, y, out)
- normalize(vec, out)
- scale(vec, scalar, out)
- scaleAdd(vec1, vec2, scalar, out)
- scaleAddAdd(vec1, vec2, vec3, scalar, out)
- scaleAddValues(vec, x, y, scalar, out)
- set(x, y, out)
- subtract(vec1, vec2, out)
- subtractFromValues(vec, x, y, out)
- subtractValues(vec, x, y, out)
- toString(vec)
- transform(vector, matrix, out)
- zero(vec)

Adds the X and Y values of two vectors together.

vec1:A vector object with values you'd like to add to another vector.

vec2:A vector object with values you'd like to add to another vector.

out:The vector object into which the result of the addition will be stored.

The vector object passed as the out parameter.

Adds arbitrary X and Y values to the X and Y values of a vector.

vec:The vector object to which the arbitrary X and Y values will be added.

x:An arbitrary numeric value to add to the vector's X value.

y:An arbitrary numeric value to add to the vector's Y value.

out:The vector object into which the result of the addition will be stored.

The vector object passed as the out parameter.

Initializes a new Float32Array with two elements for use as a 2D vector, taking it's initial values from another vector object.

vec:A compatible array-vector object to clone.

A new Float32Array with the same initial values as the input.

Sets the values of the given vector to the values of another vector.

vec:The vector object from which you're copying values.

out:The vector object to which you're assigning values.

The vector object passed as the out parameter.

Initializes a new Float32Array with two elements for use as a 2D vector.

x:The initial value for the X component.

y:The initial value for the Y component.

A new Float32Array with two elements representing x and y.

A shortcut function for providing a few different measures of difference between two vector objects.

vec1:A vector object with which you'd like to measure the difference with another vector.

vec2:A vector object with which you'd like to measure the difference with another vector.

An object with three properties: `{ `

x: vec2.x - vec1.x,

y: vec2.y - vec1.y,

distance: distance(vec1,vec2)

}

Determines the distance between two points.

vec1:A vector object representing a point in 2D space.

vec2:A vector object representing a point in 2D space.

The length of a line directly connecting the two points.

This function requires the use of Math.sqrt(); since this can be expensive if called many times per second, in instances where you're checking against a known distance threshold it may be faster instead to determine what your trigger value is when squared and compare that to distanceSquared(vec1, vec2) (which does not use Math.sqrt()).

Determines the square of the distance between two points.

vec1:A vector object representing a point in 2D space.

vec2:A vector object representing a point in 2D space.

The square of the length of a line directly connecting the two points.

Determines the distance between two points.

vec:A vector object representing a point in 2D space.

vx:The X component of an arbitrary point in 2D space.

vy:The Y component of an arbitrary point in 2D space.

The length of a line directly connecting the two points.

Divides the X and Y values of one vector by the X and Y values of another.

vec1:A vector object with values acting as numerators in a division with another vector.

vec2:A vector object with values acting as denominators in a division with another vector.

out:The vector object into which the result of the division will be stored.

The vector object passed as the out parameter.

Determines the dot product of two vectors.

vec1:A vector object to use in a dot product calculation.

vec2:A vector object to use in a dot product calculation.

The dot product of the two vector objects.

Quickly check if the values of this vector are small enough to consider the length of the vector as being zero.

vec:A vector object to check for zero-ness.

`true`

if both components of the vector object were within `+/- 1E-06`

of `0.0`

, `false`

otherwise.

Gets the length of a vector (i.e. the distance from (0,0) if the vector represented coordinates in 2D space).

vec:The vector object from which to calculate length.

The length of a line directly connecting the vector and the origin.

This function requires the use of Math.sqrt(); since this can be expensive if called many times per second, in instances where you're checking against a known length threshold it may be faster instead to determine what your trigger value is when squared and compare that to lengthSquared(vec) (which does not use Math.sqrt()).

Determines the square of the length of a vector.

vec:The vector object from which to calculate length.

The square of the length of a line directly connecting the vector and the origin.

Compares the X and Y values of two vectors, returning a vector with the max of each.

vec1:A vector object to compare with another.

out:The vector object into which the result of the comparison will be stored.

The vector object passed as the out parameter.

Compares the X and Y values of two vectors, returning a vector with the minimum of each.

vec1:A vector object to compare with another.

out:The vector object into which the result of the comparison will be stored.

The vector object passed as the out parameter.

Multiplies the X and Y values of two vectors together.

vec1:A vector object with values you'd like to multiply with another vector.

vec2:A vector object with values you'd like to multiply with another vector.

out:The vector object into which the result of the multiplication will be stored.

The vector object passed as the out parameter.

Multiplies arbitrary X and Y values with the X and Y values of a vector.

vec:The vector object with which the arbitrary X and Y values will be multiplied.

x:An arbitrary numeric value to multiply with the vector's X value.

y:An arbitrary numeric value to multiply with the vector's Y value.

out:The vector object into which the result of the multiplication will be stored.

The vector object passed as the out parameter.

Normalizes the X and Y values of a vector such that the length of the vector will equal 1.

vec:The vector to normalize.

out:The vector object into which the result of the normalization will be stored.

The vector object passed as the out parameter.

Multiplies the X and Y values of a vector by a scalar value.

vec:A vector object with values you'd like to multiply by a given scalar.

scalar:A scalar (numeric value) by which you'd like to multiply the given vector.

out:The vector object into which the result of the multiplication will be stored.

The vector object passed as the out parameter.

Multiplies the X and Y values of a vector by a scalar value, then adds the result to another vector.

vec1:A vector object with values you'd like to add to the result of the scale operation.

vec2:A vector object with values you'd like to multiply by a given scalar.

scalar:A scalar (numeric value) by which you'd like to multiply the given vector.

out:The vector object into which the result of the operations will be stored.

The vector object passed as the out parameter.

This function can be represented algebraically as **out = vec1 + (vec2 * scalar)**.

Adds two vectors together, multiplies the result by a scalar value, then adds the result to a third vector.

vec1:The outermost vector to which the result of all other operations will be added.

vec2:One of the two inner vectors which will be added together.

vec3:The other of the inner vectors which will be added together.

scalar:A scalar (numeric value) by which you'd like to multiply the result of the inner vector addition.

out:The vector object into which the result of the operations will be stored.

The vector object passed as the out parameter.

This function can be represented algebraically as **out = vec1 + ((vec2 + vec3) * scalar)**.

Multiplies arbitrary X and Y values by a scalar value, then adds the result to a vector.

vec:A vector object with values you'd like to add to the result of the scale operation.

x:An arbitrary numeric value to multiply with the given scalar.

y:An arbitrary numeric value to multiply with the given scalar.

scalar:A scalar (numeric value) by which you'd like to multiply the given X and Y values.

out:The vector object into which the result of the operations will be stored.

The vector object passed as the out parameter.

Functionally this is the same as scaleAdd(vec1, vec2, scalar, out), but with the X and Y parameters taking place of the vec2 parameter.

Sets the values of the given vector to the passed parameters.

x:The value to apply for the X component.

y:The value to apply for the Y component.

out:The vector object to which you're assigning values.

The vector object passed as the out parameter.

Subtracts the X and Y values of one vector from those of another.

vec1:The vector object from which the subtraction will occur.

vec2:The vector object with values that will be subtracted from the other.

out:The vector object into which the result of the subtraction will be stored.

The vector object passed as the out parameter.

Subtracts the X and Y values of a vector from arbitrary X and Y values.

vec:The vector object with values that will be subtracted from the arbitary X and Y values.

x:An arbitrary numeric value from which to subtract from the vector's X value.

y:An arbitrary numeric value from which to subtract from the vector's Y value.

out:The vector object into which the result of the subtraction will be stored.

The vector object passed as the out parameter.

Functionally this is the same as subtractValues(vec, x, y, out), but with the operands switched around to opposite sides of the subtraction.

Subtracts arbitrary X and Y values from the X and Y values of a vector.

vec:The vector object from which the arbitrary X and Y values will be subtracted.

x:An arbitrary numeric value to subtract from the vector's X value.

y:An arbitrary numeric value to subtract from the vector's Y value.

out:The vector object into which the result of the subtraction will be stored.

The vector object passed as the out parameter.

Generates a human-readable coordinate pair string from a vector.

vec:The vector object from which you're creating a string.

A string containing an XY coordinate pair.

Transforms a vector to another coordinate system.

vector:A vector object to to transform to another coordinate system.

matrix:A 2x2 Matrix object representing the two axes of the new coordinate system.

out:The vector object into which the result of the transformation will be stored.

The vector object passed as the out parameter.

Resets the X and Y values of a vector to 0.0

vec:The vector object to reset to 0.0

The vector object passed as the vec parameter.