Constructor
new vec(x, yopt)
Creates a new vector
Name | Type | Attributes | Description |
---|---|---|---|
x | number | | x coordinate, array | |
y | number | <optional> | y coordinate |
- Source
Members
length
Finds the length of this
- Source
length
Sets the length of this
, keeping its direction the same
- Source
let v = new vec(1, 1);
v.length = 10; // v = { x: 7.07, y: 7.07 }
angle
Finds the angle of this
- Source
area
Finds area of the rectangle created by this
- Source
Methods
add(vec2) → {vec}
Adds vec2
to this
, returning a new vector
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- vec
sub(vec2) → {vec}
Subtracts vec2
from this
, returning a new vector
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- vec
mult(vec2) → {vec}
Multiplies this
by vec2
, returning a new vector
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- vec
div(vec2) → {vec}
Divides this
by vec2
, returning a new vector
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- vec
add2(vec2) → {vec}
Adds vec2
to this
in place, returning this
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
this
- Type:
- vec
sub2(vec2) → {vec}
Subtracts vec2
from this
in place, returning this
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
this
- Type:
- vec
mult2(vec2) → {vec}
Multiplies this
by vec2
in place, returning this
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
this
- Type:
- vec
div2(vec2) → {vec}
Divides this
by vec2
in place, returning this
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
this
- Type:
- vec
pow(vec2) → {vec}
Raises this
to the power of vec2
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- vec
pow2(vec2) → {vec}
Raises this
to the power of vec2
in place
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
this
- Type:
- vec
sign() → {vec}
Finds the signed values of x
and y
- Source
New vector
- Type:
- vec
let signed = new vec(4, -2).sign(); // signed = { x: 1, y: -1 }
sign2() → {vec}
Finds the signed values of x
and y
in place
- Source
this
- Type:
- vec
let signed = new vec(0, -4);
signed.sign2(); // signed = { x: 0, y: -1 }
mod(vec2) → {vec}
Finds the modulus of this
and vec2
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
New vector
- Type:
- vec
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
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
this
- Type:
- vec
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
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
Dot product
- Type:
- number
cross(vec2) → {number|vec}
Finds 2d cross product of this
and vec2
Name | Type | Description |
---|---|---|
vec2 | vec | |
- Source
New vector
- Type:
- number |
vec
avg(vec2, weight) → {vec}
Finds average of this
and vec2
Name | Type | Default | Description |
---|---|---|---|
vec2 | vec | Second vector | |
weight | number | 0.5 | Weight that |
- Source
New vector
- Type:
- vec
manhattan(vec2) → {number}
Finds the manhattan distance (x + y) between vec
and this
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
Distance
- Type:
- number
abs() → {vec}
Takes the absolute value of x
and y
- Source
New vector
- Type:
- vec
abs2() → {vec}
Takes the absolute value of x
and y
in place
- Source
this
- Type:
- vec
reflect(vec2) → {vec}
Reflects this
over vec2
. vec2
must be normalized
Name | Type | Description |
---|---|---|
vec2 | vec | Normalized vector reflected across |
- Source
New reflected vector
- Type:
- vec
reflect2(vec2) → {vec}
Reflects this
over vec2
in place. vec2
must be normalized
Name | Type | Description |
---|---|---|
vec2 | vec | Normalized vector reflected across |
- Source
this
- Type:
- vec
rotate(angle) → {vec}
Rotates this
by angle
Name | Type | Description |
---|---|---|
angle | number | Angle rotated by, in radians |
- Source
New rotated vector
- Type:
- vec
rotate2(angle) → {vec}
Rotates this
by angle
in place
Name | Type | Description |
---|---|---|
angle | number | Angle rotated by, in radians |
- Source
this
- Type:
- vec
project(vec2, boundopt) → {vec}
Projects this
onto vec2
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
vec2 | vec | Vector projected onto | ||
bound | boolean | <optional> | false | If the projected vector should be forced between the bounds of |
- Source
New rotated vector
- Type:
- vec
project2(vec2, boundopt) → {vec}
Projects this
onto vec2
in place
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
vec2 | vec | Vector projected onto | ||
bound | boolean | <optional> | false | If the projected vector should be forced between the bounds of |
- Source
this
- Type:
- vec
normalize() → {vec}
Normalizes this
, making its length 1
- Source
New vector
- Type:
- vec
normalize2() → {vec}
Normalizes this
in place, making its length 1
- Source
this
- Type:
- vec
normal() → {vec}
Finds the left hand normal
- Source
New vector
- Type:
- vec
normal2() → {vec}
Finds the left hand normal in place
- Source
this
- Type:
- vec
floor() → {vec}
Rounds x
and y
components down
- Source
New vector
- Type:
- vec
floor2() → {vec}
Rounds x
and y
components down in place
- Source
this
- Type:
- vec
ceil() → {vec}
Rounds x
and y
components up
- Source
New vector
- Type:
- vec
ceil2() → {vec}
Rounds x
and y
components up in place
- Source
this
- Type:
- vec
round() → {vec}
Rounds x
and y
components
- Source
New vector
- Type:
- vec
round2() → {vec}
Rounds x
and y
components in place
- Source
this
- Type:
- vec
min(vec2) → {vec}
Finds the minimum x
and y
components between this
and vec2
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
New vector
- Type:
- vec
min2(vec2) → {vec}
Finds the minimum x
and y
components between this
and vec2
in place
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
this
- Type:
- vec
max(vec2) → {vec}
Finds the maximum x
and y
components between this
and vec2
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
New vector
- Type:
- vec
max2(vec2) → {vec}
Finds the maximum x
and y
components between this
and vec2
in place
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
this
- Type:
- vec
clamp(min, max) → {vec}
Clamps x
and y
components between min
and max
- Source
New vector
- Type:
- vec
clamp2(min, max) → {vec}
Finds the maximum x
and y
components between this
and vec2
in place
- Source
this
- Type:
- vec
equals(vec2) → {boolean}
Checks if this
equals vec2
. DOES NOT take into account floating point error.
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
- Type:
- boolean
set(vec2) → {vec}
Sets the x
and y
components to be the same as vec2
in place
Name | Type | Description |
---|---|---|
vec2 | vec |
- Source
this
- Type:
- vec
toString()
Creates a string in the format "{ x : x, y: y }"
- Source
toStringInt()
Creates a string in the format "{ x : x, y: y }"
, with x
and y
rounded
- Source
toObject() → {Object}
Creates js object in the form of { x: x, y: y }
- Source
- Type:
- Object
toArray()
Creates an array in the format [x, y]
- Source
isNaN() → {boolean}
Finds if any part of the vector is NaN
- Source
- Type:
- boolean