vec

A 2d vector

Constructor

new vec(x, yopt)

Creates a new vector

Parameters:
NameTypeAttributesDescription
xnumber | Array | object

x coordinate, array [x, y], or object { x: x, y: y }

ynumber<optional>

y coordinate

Members

length

Finds the length of this

length

Sets the length of this, keeping its direction the same

Example
let v = new vec(1, 1);
v.length = 10; // v = { x: 7.07, y: 7.07 }

angle

Finds the angle of this

area

Finds area of the rectangle created by this

Methods

add(vec2) → {vec}

Adds vec2 to this, returning a new vector

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
vec

sub(vec2) → {vec}

Subtracts vec2 from this, returning a new vector

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
vec

mult(vec2) → {vec}

Multiplies this by vec2, returning a new vector

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
vec

div(vec2) → {vec}

Divides this by vec2, returning a new vector

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
vec

add2(vec2) → {vec}

Adds vec2 to this in place, returning this

Parameters:
NameTypeDescription
vec2vec | number
Returns:

this

Type: 
vec

sub2(vec2) → {vec}

Subtracts vec2 from this in place, returning this

Parameters:
NameTypeDescription
vec2vec | number
Returns:

this

Type: 
vec

mult2(vec2) → {vec}

Multiplies this by vec2 in place, returning this

Parameters:
NameTypeDescription
vec2vec | number
Returns:

this

Type: 
vec

div2(vec2) → {vec}

Divides this by vec2 in place, returning this

Parameters:
NameTypeDescription
vec2vec | number
Returns:

this

Type: 
vec

pow(vec2) → {vec}

Raises this to the power of vec2

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
vec

pow2(vec2) → {vec}

Raises this to the power of vec2 in place

Parameters:
NameTypeDescription
vec2vec | number
Returns:

this

Type: 
vec

sign() → {vec}

Finds the signed values of x and y

Returns:

New vector

Type: 
vec
Example
let signed = new vec(4, -2).sign(); // signed = { x: 1, y: -1 }

sign2() → {vec}

Finds the signed values of x and y in place

Returns:

this

Type: 
vec
Example
let signed = new vec(0, -4);
signed.sign2(); // signed = { x: 0, y: -1 }

mod(vec2) → {vec}

Finds the modulus of this and vec2

Parameters:
NameTypeDescription
vec2vec
Returns:

New vector

Type: 
vec
Example
let mod = new vec(14, 4).mod(new vec(2, 3)); // mod = { x: 0, y: 1 }

mod2(vec2) → {vec}

Finds the modulus of this and vec2 in place

Parameters:
NameTypeDescription
vec2vec
Returns:

this

Type: 
vec
Example
let mod = new vec(-2, 6);
mod.mod2(new vec(3, 4)); // mod = { x: -2, y: 2 }

dot(vec2) → {number}

Finds dot product of this and vec2

Parameters:
NameTypeDescription
vec2vec
Returns:

Dot product

Type: 
number

cross(vec2) → {number|vec}

Finds 2d cross product of this and vec2

Parameters:
NameTypeDescription
vec2vec | number
Returns:

New vector

Type: 
number | vec

avg(vec2, weight) → {vec}

Finds average of this and vec2

Parameters:
NameTypeDefaultDescription
vec2vec

Second vector

weightnumber0.5

Weight that this has in the average

Returns:

New vector

Type: 
vec

manhattan(vec2) → {number}

Finds the manhattan distance (x + y) between vec and this

Parameters:
NameTypeDescription
vec2vec
Returns:

Distance

Type: 
number

abs() → {vec}

Takes the absolute value of x and y

Returns:

New vector

Type: 
vec

abs2() → {vec}

Takes the absolute value of x and y in place

Returns:

this

Type: 
vec

reflect(vec2) → {vec}

Reflects this over vec2. vec2 must be normalized

Parameters:
NameTypeDescription
vec2vec

Normalized vector reflected across

Returns:

New reflected vector

Type: 
vec

reflect2(vec2) → {vec}

Reflects this over vec2 in place. vec2 must be normalized

Parameters:
NameTypeDescription
vec2vec

Normalized vector reflected across

Returns:

this

Type: 
vec

rotate(angle) → {vec}

Rotates this by angle

Parameters:
NameTypeDescription
anglenumber

Angle rotated by, in radians

Returns:

New rotated vector

Type: 
vec

rotate2(angle) → {vec}

Rotates this by angle in place

Parameters:
NameTypeDescription
anglenumber

Angle rotated by, in radians

Returns:

this

Type: 
vec

project(vec2, boundopt) → {vec}

Projects this onto vec2

Parameters:
NameTypeAttributesDefaultDescription
vec2vec

Vector projected onto

boundboolean<optional>
false

If the projected vector should be forced between the bounds of vec2

Returns:

New rotated vector

Type: 
vec

project2(vec2, boundopt) → {vec}

Projects this onto vec2 in place

Parameters:
NameTypeAttributesDefaultDescription
vec2vec

Vector projected onto

boundboolean<optional>
false

If the projected vector should be forced between the bounds of vec2

Returns:

this

Type: 
vec

normalize() → {vec}

Normalizes this, making its length 1

Returns:

New vector

Type: 
vec

normalize2() → {vec}

Normalizes this in place, making its length 1

Returns:

this

Type: 
vec

normal() → {vec}

Finds the left hand normal

Returns:

New vector

Type: 
vec

normal2() → {vec}

Finds the left hand normal in place

Returns:

this

Type: 
vec

floor() → {vec}

Rounds x and y components down

Returns:

New vector

Type: 
vec

floor2() → {vec}

Rounds x and y components down in place

Returns:

this

Type: 
vec

ceil() → {vec}

Rounds x and y components up

Returns:

New vector

Type: 
vec

ceil2() → {vec}

Rounds x and y components up in place

Returns:

this

Type: 
vec

round() → {vec}

Rounds x and y components

Returns:

New vector

Type: 
vec

round2() → {vec}

Rounds x and y components in place

Returns:

this

Type: 
vec

min(vec2) → {vec}

Finds the minimum x and y components between this and vec2

Parameters:
NameTypeDescription
vec2vec
Returns:

New vector

Type: 
vec

min2(vec2) → {vec}

Finds the minimum x and y components between this and vec2 in place

Parameters:
NameTypeDescription
vec2vec
Returns:

this

Type: 
vec

max(vec2) → {vec}

Finds the maximum x and y components between this and vec2

Parameters:
NameTypeDescription
vec2vec
Returns:

New vector

Type: 
vec

max2(vec2) → {vec}

Finds the maximum x and y components between this and vec2 in place

Parameters:
NameTypeDescription
vec2vec
Returns:

this

Type: 
vec

clamp(min, max) → {vec}

Clamps x and y components between min and max

Parameters:
NameTypeDescription
minvec
maxvec
Returns:

New vector

Type: 
vec

clamp2(min, max) → {vec}

Finds the maximum x and y components between this and vec2 in place

Parameters:
NameTypeDescription
minvec
maxvec
Returns:

this

Type: 
vec

equals(vec2) → {boolean}

Checks if this equals vec2. DOES NOT take into account floating point error.

Parameters:
NameTypeDescription
vec2vec
Returns:
Type: 
boolean

set(vec2) → {vec}

Sets the x and y components to be the same as vec2 in place

Parameters:
NameTypeDescription
vec2vec
Returns:

this

Type: 
vec

toString()

Creates a string in the format "{ x : x, y: y }"

toStringInt()

Creates a string in the format "{ x : x, y: y }", with x and y rounded

toObject() → {Object}

Creates js object in the form of { x: x, y: y }

Returns:
Type: 
Object

toArray()

Creates an array in the format [x, y]

isNaN() → {boolean}

Finds if any part of the vector is NaN

Returns:
Type: 
boolean