@aliser/vector2
v2.0.0
Published
Some basic functionality to represent and work with 2D vectors.
Downloads
8
Readme
@aliser/vector2 / Exports
Usage
Install with npm.
npm install @aliser/vector2
Import as a module.
import { Vector2, RAD2DEG, DEG2RAD } from '@aliser/vector2';
Enjoy!
Below you can find the docs for all of this.
Note: the static and the instance methods are mixed. Sorry for that.
TODO
- [ ] custom interpolation functions for the
static lerp
- slide with ease! - [ ] maybe some static damping function - it'll make the target vector experience some kind of repulsion (the magnitude of which is controller by some specified function) on approaching the end point.
- [ ] getters for directions - up, down, right, west, east, forward, backward, sideways, the 5- and 11- dimensions and other.
- [ ] implement the stuff that other libs like this one are providing: reflect, normal, moveTowards.
Classes
@aliser/vector2 / Exports / Vector2
Class: Vector2
Hierarchy
- Vector2
Implements
- IVector2
Table of contents
Constructors
Properties
Accessors
Methods
- add
- ceil
- clamp
- copy
- div
- floor
- isEquals
- lerp
- mod
- mult
- negate
- normalize
- rotateBy
- rotateTo
- round
- set
- setMag
- sub
- add
- angleBetween
- angleBetweenSigned
- dist
- div
- dot
- fromAngle
- lerp
- mult
- random
- randomWithLength
- sub
- zero
Constructors
constructor
+ new Vector2(x?
: number, y?
: number): Vector2
Creates an instance.
Parameters:
Name | Type |
------ | ------ |
x?
| number |
y?
| number |
Returns: Vector2
+ new Vector2(xy?
: number): Vector2
Creates an instance, where 'x' and 'y' are both equal to 'xy'.
Parameters:
Name | Type |
------ | ------ |
xy?
| number |
Returns: Vector2
+ new Vector2(): Vector2
Creates an instance, where 'x' and 'y' are both equal to 0.
Returns: Vector2
+ new Vector2(vec?
: Vector2): Vector2
Creates an instance, where 'x' and 'y' are equal to 'x' and 'y' of 'vec', respectively.
Parameters:
Name | Type |
------ | ------ |
vec?
| Vector2 |
Returns: Vector2
Properties
x
• x: number
y
• y: number
Accessors
0
• 0(): number
Returns: number
alias for 'x'.
• 0(value
: number): void
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
alias for 'x'.
1
• 1(): number
Returns: number
alias for 'y'.
• 1(value
: number): void
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
alias for 'y'.
angle
• angle(): number
The signed angle (from -PI to PI radians).
Returns: number
• angle(value
: number): void
The signed angle (from -PI to PI radians).
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
mag
• mag(): number
The length.
Note, when setting 'mag' to negative value, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
Returns: number
• mag(value
: number): void
The length.
Note, when setting 'mag' to negative value, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
max
• max(): number
Returns: number
the largest of 'x' and 'y'.
min
• min(): number
Returns: number
the smallest of 'x' and 'y'.
sqrMag
• sqrMag(): number
Returns: number
the length, squared.
u
• u(): number
Returns: number
alias for 'x'.
• u(value
: number): void
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
alias for 'x'.
v
• v(): number
Returns: number
alias for 'y'.
• v(value
: number): void
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: void
alias for 'y'.
Methods
add
▸ add(deltaX
: number): Vector2
Adds 'deltaX' to both 'x' and 'y'.
Parameters:
Name | Type |
------ | ------ |
deltaX
| number |
Returns: Vector2
▸ add(deltaX
: number, deltaY
: number): Vector2
Adds 'deltaX' and 'deltaY' to 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
deltaX
| number |
deltaY
| number |
Returns: Vector2
Adds 'x' and 'y' of 'vec' to 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
ceil
▸ ceil(): Vector2
Does the rounding to the nearest larger integer.
Returns: Vector2
clamp
▸ clamp(): Vector2
Restricts 'x' and 'y' from going below 0 or above 1.
Returns: Vector2
▸ clamp(upperBound
: number): Vector2
Restricts 'x' and 'y' from going below 0 or above 'upperBound'.
Parameters:
Name | Type |
------ | ------ |
upperBound
| number |
Returns: Vector2
▸ clamp(lowerBound
: number, upperBound
: number): Vector2
Restricts 'x' and 'y' from going below 'lowerBound' or above 'upperBound'.
Parameters:
Name | Type |
------ | ------ |
lowerBound
| number |
upperBound
| number |
Returns: Vector2
▸ clamp(lowerBoundX
: number, upperBoundX
: number, lowerBoundY
: number, upperBoundY
: number): Vector2
Restricts 'x' from going below 'lowerBoundX' or above 'upperBoundX', and for 'y' from going below 'lowerBoundY' or above 'upperBoundY'.
Parameters:
Name | Type |
------ | ------ |
lowerBoundX
| number |
upperBoundX
| number |
lowerBoundY
| number |
upperBoundY
| number |
Returns: Vector2
copy
▸ copy(): Vector2
Returns: Vector2
a copy of this instance.
div
▸ div(divisor
: number): Vector2
Divides both 'x' and 'y' by 'divisor'.
Parameters:
Name | Type |
------ | ------ |
divisor
| number |
Returns: Vector2
▸ div(divisorX
: number, divisorY
: number): Vector2
Divides 'x' and 'y' by 'divisorX' and 'divisorY', respectively.
Parameters:
Name | Type |
------ | ------ |
divisorX
| number |
divisorY
| number |
Returns: Vector2
Divides 'x' and 'y' by 'x' and 'y' of vec, respectively.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
floor
▸ floor(): Vector2
Does the rounding to the nearest smaller integer.
Returns: Vector2
isEquals
▸ isEquals(vec
: Vector2): boolean
Checks if 'x' and 'y' are strictly equal to 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: boolean
lerp
▸ lerp(vec1
: Vector2, vec2
: Vector2, t
: number): Vector2
Linearly interpolates between 'vec1' and 'vec2' by 't', setting this instance to result.
Parameters:
Name | Type |
------ | ------ |
vec1
| Vector2 |
vec2
| Vector2 |
t
| number |
Returns: Vector2
mod
▸ mod(divisor
: number): Vector2
Divides both 'x' and 'y' by 'divisor' and sets them to the remainder of that division, respectively.
Parameters:
Name | Type |
------ | ------ |
divisor
| number |
Returns: Vector2
▸ mod(divisorX
: number, divisorY
: number): Vector2
Divides 'x' and 'y' by 'divisorX' and 'divisorY', respectively, and sets them to the remainder of that division, also respectively.
Parameters:
Name | Type |
------ | ------ |
divisorX
| number |
divisorY
| number |
Returns: Vector2
mult
▸ mult(multiplier
: number): Vector2
Multiplies both 'x' and 'y' by 'multiplier'.
Parameters:
Name | Type |
------ | ------ |
multiplier
| number |
Returns: Vector2
▸ mult(multiplierX
: number, multiplierY
: number): Vector2
Multiplies 'x' and 'y' by 'multiplierX' and 'multiplierY', respectively.
Parameters:
Name | Type |
------ | ------ |
multiplierX
| number |
multiplierY
| number |
Returns: Vector2
Multiplies 'x' and 'y' by 'x' and 'y' of 'vec', respectively.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
negate
▸ negate(): Vector2
Inverts both 'x' and 'y' (makes them -'x' and -'y').
Returns: Vector2
normalize
▸ normalize(): Vector2
Mangles the length of this instance until it will be 1.
Note, if the length of this instance is 0, than it will not be changed.
Returns: Vector2
rotateBy
▸ rotateBy(angleInRadiansDelta
: number): Vector2
Rotates this instance by 'angleInRadiansDelta' radians.
Parameters:
Name | Type |
------ | ------ |
angleInRadiansDelta
| number |
Returns: Vector2
▸ rotateBy(vec
: Vector2): Vector2
Rotates this instance by the signed angle of 'vec'.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
rotateTo
▸ rotateTo(angleInRadians
: number): Vector2
Rotates this instance to 'angleInRadians' radians.
Parameters:
Name | Type |
------ | ------ |
angleInRadians
| number |
Returns: Vector2
▸ rotateTo(vec
: Vector2): Vector2
Rotates this instance to the angle of 'vec'.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
round
▸ round(): Vector2
Does the rounding to the nearest integer.
Returns: Vector2
set
▸ set(x?
: number, y?
: number): Vector2
Sets 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
x?
| number |
y?
| number |
Returns: Vector2
▸ set(xy?
: number): Vector2
Sets both 'x' and 'y' to 'xy'.
Parameters:
Name | Type |
------ | ------ |
xy?
| number |
Returns: Vector2
▸ set(): Vector2
Sets both 'x' and 'y' to 0.
Returns: Vector2
Sets 'x' and 'y' to 'x' and 'y' of 'vec', respectively.
Parameters:
Name | Type |
------ | ------ |
vec?
| Vector2 |
Returns: Vector2
setMag
▸ setMag(value
: number): Vector2
Function alias of 'mag'.
Sets the length.
Note, when setting 'value' is negative, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
Parameters:
Name | Type |
------ | ------ |
value
| number |
Returns: Vector2
▸ setMag(vec
: Vector2): Vector2
Sets the length to the length of vec
.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
sub
▸ sub(delta
: number): Vector2
Subtracts 'delta' from both 'x' and 'y'.
Parameters:
Name | Type |
------ | ------ |
delta
| number |
Returns: Vector2
▸ sub(deltaX
: number, deltaY
: number): Vector2
Subtracts 'deltaX' and 'deltaY' from 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
deltaX
| number |
deltaY
| number |
Returns: Vector2
Subtracts 'x' and 'y' of 'vec' from 'x' and 'y', respectively.
Parameters:
Name | Type |
------ | ------ |
vec
| Vector2 |
Returns: Vector2
add
▸ Static
add(...vecs
: [Vector2, Vector2, ...Vector2[]]): Vector2
Adds 'vecs' together.
Parameters:
Name | Type |
------ | ------ |
...vecs
| [Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
angleBetween
▸ Static
angleBetween(vec1
: Vector2, vec2
: Vector2): number
Parameters:
Name | Type |
------ | ------ |
vec1
| Vector2 |
vec2
| Vector2 |
Returns: number
an unsigned angle (from 0 to PI radians) between 'vec1' and 'vec2'.
angleBetweenSigned
▸ Static
angleBetweenSigned(vec1
: Vector2, vec2
: Vector2): number
Parameters:
Name | Type |
------ | ------ |
vec1
| Vector2 |
vec2
| Vector2 |
Returns: number
the smallest signed angle (from -PI to PI radians) between 'vec1' and 'vec2'.
Note, that the resulting angle is the angle that, if applied as a rotation to 'vec1', will result in 'vec2'.
dist
▸ Static
dist(vec1
: Vector2, vec2
: Vector2): number
Parameters:
Name | Type |
------ | ------ |
vec1
| Vector2 |
vec2
| Vector2 |
Returns: number
the distance between 'vec1' and 'vec2'.
div
▸ Static
div(...vectors
: [Vector2, Vector2, ...Vector2[]]): Vector2
Sequentially divides 'vecs', one by one.
Parameters:
Name | Type |
------ | ------ |
...vectors
| [Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
dot
▸ Static
dot(vec1
: Vector2, vec2
: Vector2): number
Parameters:
Name | Type |
------ | ------ |
vec1
| Vector2 |
vec2
| Vector2 |
Returns: number
the dot product of 'vec1' and 'vec2'.
fromAngle
▸ Static
fromAngle(angleOfRadians
: number, length?
: number): Vector2
Parameters:
Name | Type | Default value |
------ | ------ | ------ |
angleOfRadians
| number | - |
length
| number | 1 |
Returns: Vector2
a new instance with angle 'angleOfRadians' radians and length 'length'.
lerp
▸ Static
lerp(vecOut
: Vector2, vec1
: Vector2, vec2
: Vector2, t
: number): Vector2
Linearly interpolates between 'vec1' and 'vec2' by 't', setting 'vecOut' to result.
Parameters:
Name | Type |
------ | ------ |
vecOut
| Vector2 |
vec1
| Vector2 |
vec2
| Vector2 |
t
| number |
Returns: Vector2
'vecOut'.
mult
▸ Static
mult(...vecs
: [Vector2, Vector2, ...Vector2[]]): Vector2
Multiplies 'vecs' together.
Parameters:
Name | Type |
------ | ------ |
...vecs
| [Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
random
▸ Static
random(multiplier?
: number): Vector2
Parameters:
Name | Type | Default value |
------ | ------ | ------ |
multiplier
| number | 1 |
Returns: Vector2
a new instance with 'x' and 'y' individually set to some random value from -1 to below 1, multiplied by 'multiplier'.
randomWithLength
▸ Static
randomWithLength(length?
: number): Vector2
Parameters:
Name | Type | Default value |
------ | ------ | ------ |
length
| number | 1 |
Returns: Vector2
a new instance with 'x' and 'y' individually set to some random value in such a way that the length of the new instance will be 'length'.
sub
▸ Static
sub(...vecs
: [Vector2, Vector2, ...Vector2[]]): Vector2
Sequentially subtracts 'vecs', one by one.
Parameters:
Name | Type |
------ | ------ |
...vecs
| [Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
zero
▸ Static
zero(): Vector2
Returns: Vector2
a new instance with both 'x' and 'y' set to 0.
@aliser/vector2 / Exports
@aliser/vector2
Table of contents
Classes
Variables
Variables
DEG2RAD
• Const
DEG2RAD: number
Multiply your degrees by this to convert them to radians.
RAD2DEG
• Const
RAD2DEG: number
Multiply your radians by this to convert them to degrees.