Zero dependency class for handling 2 dimensional, 3 dimensional or n dimensional vectors.
Example
// import the vector and create a new vector object
const MagikVector = import 'MagikVector';
const vector = new MagikVector();
// initialise a new vector with `x` and `y` coordinates
const vector2D = new MagikVector(12, 42);
// it's also possible to add a `z` coordinate
const vector3D = new MagikVector(15, 12, 71);
// or even more coordinates
const multiDimensional = new MagikVector(15, 12, 71, 7, 38, 0);
// create a MagikVector from an Array
const myCoordinates = [4, 6, 28, 5, 33, 12, 8, 22, 785, 38, 56];
const multiDimensional = new MagikVector(...myCoordinates);
- MagikVector
- ~MagikVector
- new MagikVector([...args])
- instance
- .length ⇒
number
- .x ⇒
number
- .y ⇒
number
- .z ⇒
number
- .x ⇒
void
- .y ⇒
void
- .z ⇒
void
- .getX() ⇒
number
- .getY() ⇒
number
- .getZ() ⇒
number
- .getCoordinate(index) ⇒
number
|undefined
- .getCoord(index) ⇒
number
|undefined
- .setX(value) ⇒
MagikVector
- .setY(value) ⇒
MagikVector
- .setZ(value) ⇒
MagikVector
- .setCoordinate(index, value) ⇒
MagikVector
- .setCoord(index, value) ⇒
MagikVector
- .add(vector) ⇒
MagikVector
- .subtract(vector) ⇒
MagikVector
- .sub(vector) ⇒
MagikVector
- .multiply(value) ⇒
MagikVector
|number
- .mult(value) ⇒
MagikVector
|number
- .divide(value) ⇒
MagikVector
- .div(value) ⇒
MagikVector
- .clone() ⇒
MagikVector
- .getMagnitudeSquared() ⇒
number
- .getMagnitude() ⇒
number
- .getMag() ⇒
number
- .setMagnitude(magnitude) ⇒
MagikVector
- .setMag(magnitude) ⇒
MagikVector
- .getDirection() ⇒
number
- .getDir() ⇒
number
- .getDistanceTo(vector) ⇒
number
- .getDistance(vector) ⇒
number
- .dotProduct(vector) ⇒
number
- .dot(vector) ⇒
number
- .normalise() ⇒
MagikVector
- .normalize() ⇒
MagikVector
- .limit(scalar) ⇒
MagikVector
- .toString() ⇒
string
- .length ⇒
- static
- .random([dimensions]) ⇒
MagikVector
- .rand([dimensions]) ⇒
MagikVector
- .random2D() ⇒
MagikVector
- .random3D() ⇒
MagikVector
- .randomInteger([...args]) ⇒
number
- .randomInt([...args]) ⇒
number
- .toRadians(degrees) ⇒
number
- .toDegrees(radians) ⇒
number
- .random([dimensions]) ⇒
- ~MagikVector
Kind: inner class of MagikVector
- ~MagikVector
- new MagikVector([...args])
- instance
- .length ⇒
number
- .x ⇒
number
- .y ⇒
number
- .z ⇒
number
- .x ⇒
void
- .y ⇒
void
- .z ⇒
void
- .getX() ⇒
number
- .getY() ⇒
number
- .getZ() ⇒
number
- .getCoordinate(index) ⇒
number
|undefined
- .getCoord(index) ⇒
number
|undefined
- .setX(value) ⇒
MagikVector
- .setY(value) ⇒
MagikVector
- .setZ(value) ⇒
MagikVector
- .setCoordinate(index, value) ⇒
MagikVector
- .setCoord(index, value) ⇒
MagikVector
- .add(vector) ⇒
MagikVector
- .subtract(vector) ⇒
MagikVector
- .sub(vector) ⇒
MagikVector
- .multiply(value) ⇒
MagikVector
|number
- .mult(value) ⇒
MagikVector
|number
- .divide(value) ⇒
MagikVector
- .div(value) ⇒
MagikVector
- .clone() ⇒
MagikVector
- .getMagnitudeSquared() ⇒
number
- .getMagnitude() ⇒
number
- .getMag() ⇒
number
- .setMagnitude(magnitude) ⇒
MagikVector
- .setMag(magnitude) ⇒
MagikVector
- .getDirection() ⇒
number
- .getDir() ⇒
number
- .getDistanceTo(vector) ⇒
number
- .getDistance(vector) ⇒
number
- .dotProduct(vector) ⇒
number
- .dot(vector) ⇒
number
- .normalise() ⇒
MagikVector
- .normalize() ⇒
MagikVector
- .limit(scalar) ⇒
MagikVector
- .toString() ⇒
string
- .length ⇒
- static
- .random([dimensions]) ⇒
MagikVector
- .rand([dimensions]) ⇒
MagikVector
- .random2D() ⇒
MagikVector
- .random3D() ⇒
MagikVector
- .randomInteger([...args]) ⇒
number
- .randomInt([...args]) ⇒
number
- .toRadians(degrees) ⇒
number
- .toDegrees(radians) ⇒
number
- .random([dimensions]) ⇒
Initialise a new Vector instance with coordinates as arguments, either supply the individual coordinates or supply an array with number values.
Param | Type | Description |
---|---|---|
[...args] | number | array |
optional coordinate list or Array |
Example
// empty vector
const vector = new MagikVector();
// two dimensional
const vector2D = new MagikVector(12, 15);
// three dimensional
const vector3D = new MagikVector(12, 15, 71);
// multi dimensional
const multiDimensional = new MagikVector(3, 4, 5, 99, 12, 14, 42);
// from Array
const myCoordinates = [4, 6, 28, 5, 33, 12, 8, 22, 785, 38, 56];
const multiDimensional = new MagikVector(...myCoordinates);
Returns the length of the coordinates array, in other words the number of dimensions of this MagikVector
Kind: instance property of MagikVector
Returns: number
- - the number of dimensions of this MagikVector
Returns the x
coordinate of this Vector.
Kind: instance property of MagikVector
Returns: number
- - the x
coordinate of this MagikVector instance
Example
const vector = new MagikVector(3, 4, 5);
const xCoordinate = vector.x; // returns 3
Returns the y
coordinate of this Vector.
Kind: instance property of MagikVector
Returns: number
- - the y
coordinate of this MagikVector instance
Example
const vector = new MagikVector(3, 4, 5);
const yCoordinate = vector.y; // returns 4
Returns the z
coordinate of this Vector.
Kind: instance property of MagikVector
Returns: number
- - the z
coordinate of this MagikVector instance
Example
const vector = new MagikVector(3, 4, 5);
const zCoordinate = vector.z; // returns 5
Sets the x
coordinate of this MagikVector instance.
Kind: instance property of MagikVector
Param | Type | Description |
---|---|---|
value | number |
the value to set as x coordinate |
Example
const vector = new MagikVector();
const vector.x = 3;
Sets the y
coordinate of this MagikVector instance.
Kind: instance property of MagikVector
Param | Type | Description |
---|---|---|
value | number |
the value to set as y coordinate |
Example
const vector = new MagikVector();
const vector.y = 4;
Sets the z
coordinate of this MagikVector instance.
Kind: instance property of MagikVector
Param | Type | Description |
---|---|---|
value | number |
the value to set as z coordinate |
Example
const vector = new MagikVector();
const vector.z = 5;
Returns the x
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- - the x coordinate
Returns the y
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- the x coordinate
Returns the z
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- the x coordinate
Returns the coordinate at the specified index, consider using:
const x = vector.x
or const x = vector.getX()
,
const y = vector.y
or const y = vector.getY()
,
const z = vector.z
or const z = vector.getZ()
,
to retrieve the coordinates of a 2D or 3D vector.
Kind: instance method of MagikVector
Returns: number
| undefined
- - value at the specified index or undefined
See
- MagikVector.x
- MagikVector.getX()
- MagikVector.y
- MagikVector.getY()
- MagikVector.z
- MagikVector.getZ()
Param | Type |
---|---|
index | number |
Alias of MagikVector.getCoordinate()
Kind: instance method of MagikVector
Returns: number
| undefined
- - value at the specified index or undefined
Param | Type |
---|---|
index | number |
Sets the x
coordinate of this MagikVector and returns the instance
so it can be chained.
Kind: instance method of MagikVector
Returns: MagikVector
- - the instance itself
Param | Type | Description |
---|---|---|
value | number |
the value to set |
Example (set x coordinate and chain)
const vector = new MagikVector();
vector.setX(33).setY(44).setZ(55);
Returns the y
coordinate of this MagikVector and returns the instance
so it can be chained.
Kind: instance method of MagikVector
Returns: MagikVector
- - the instance itself
Param | Type | Description |
---|---|---|
value | number |
the value to set |
Example (set y coordinate and chain)
const vector = new MagikVector();
vector.setX(33).setY(44).setZ(55);
Returns the z
coordinate of this MagikVector and returns the instance
so it can be chained.
Kind: instance method of MagikVector
Returns: MagikVector
- - the instance itself
Param | Type | Description |
---|---|---|
value | number |
the value to set |
Example (set z coordinate and chain)
const vector = new MagikVector();
vector.setX(33).setY(44).setZ(55);
Sets the coordinate at the specified index, consider using
vector.x = 3
or vector.setX()
,
vector.y = 4
or vector.setY()
,
vector.z = 5
or vector.setZ()
,
to set the coordinates of a 2D or 3D vector.
Returns the instance so it's possible to chain this method.
Kind: instance method of MagikVector
Returns: MagikVector
- the object itself
See
- MagikVector.x
- MagikVector.setX()
- MagikVector.y
- MagikVector.setY()
- MagikVector.z
- MagikVector.setZ()
Param | Type | Description |
---|---|---|
index | number |
the index to set the value for |
value | number |
the value to set |
Alias of MagikVector.setCoordinate()
Kind: instance method of MagikVector
Returns: MagikVector
- the object itself
Param | Type | Description |
---|---|---|
index | number |
the index to set the value for |
value | number |
the value to set |
Adds given vector to the current vector, i.e. adds the individual coordinates.
Kind: instance method of MagikVector
Returns: MagikVector
- - the current MagikVector instance
Param | Type | Description |
---|---|---|
vector | MagikVector |
vector to add to current instance |
subtracts given vector from the current vector, i.e. subtracts the individual coordinates.
Kind: instance method of MagikVector
Returns: MagikVector
- - returns the current Vector
Param | Type | Description |
---|---|---|
vector | MagikVector |
vector to subtract from current instance |
Alias of MagikVector.substract()
Kind: instance method of MagikVector
Returns: MagikVector
- - returns the current Vector
Param | Type | Description |
---|---|---|
vector | MagikVector |
vector to subtract from current instance |
Multiplies the current Vector by value, you can supply either a scalar value or a Vector
Kind: instance method of MagikVector
Returns: MagikVector
| number
- - returns the current MagikVector instance
Param | Type | Description |
---|---|---|
value | MagikVector | number |
scalar or Vector to use to multiply |
Alias for the multiply()
method
Kind: instance method of MagikVector
Returns: MagikVector
| number
- - returns the current MagikVector instance
Param | Type | Description |
---|---|---|
value | MagikVector | number |
scalar or Vector to use to multiply |
Divides the current vector by the supplied scalar or Vector value
Kind: instance method of MagikVector
Returns: MagikVector
- - the divided MagikVector
Param | Type | Description |
---|---|---|
value | MagikVector | number |
scalar or MagikVector to use to divide |
Alias for the divide()
method
Kind: instance method of MagikVector
Returns: MagikVector
- - the divided MagikVector
Param | Type | Description |
---|---|---|
value | MagikVector | number |
scalar or MagikVector to use to divide |
Returns a clone of the current vector, i.e. creates a new Vector Object with the same coordinates as the current MagikVector instance.
Kind: instance method of MagikVector
Returns: MagikVector
- - returns a clone of the current instance
Returns the magnitude squared
Kind: instance method of MagikVector
Returns: number
- - the magnitude squared value of the current instance
Returns the magnitude of the vector
Kind: instance method of MagikVector
Returns: number
- - the magnitude of the current instance
Returns the magnitude of the vector
Kind: instance method of MagikVector
Returns: number
- - the magnitude of the current instance
Sets the magnitude of the Vector and returns the changed vector
Kind: instance method of MagikVector
Returns: MagikVector
- - the adjusted MagikVector instance
Param | Type | Description |
---|---|---|
magnitude | number |
magnitude to set this instance to |
Alias for the setMagnitude()
method
Kind: instance method of MagikVector
Returns: MagikVector
- the adjusted MagikVector object
Param | Type |
---|---|
magnitude | number |
Calculates the direction, i.e. the angle of rotation for this vector (only for 2D vectors)
Kind: instance method of MagikVector
Returns: number
- the angle of rotation in radians
Alias for the getDirection() method
Kind: instance method of MagikVector
Returns: number
- - the angle of rotation in radians
Returns the calculated distance from the current Vector to the supplied one
Kind: instance method of MagikVector
Returns: number
- - the distance between the two vectors
Param | Type | Description |
---|---|---|
vector | MagikVector |
the vector to calculate the distance to |
Alias for the getDistanceTo() method
Kind: instance method of MagikVector
Returns: number
- - the distance between the two vectors
Param | Type | Description |
---|---|---|
vector | MagikVector |
the vector to calculate the distance to |
Returns the dot Product of the current Vector with the supplied Vector, throws an Error if both Vectors do not have the same number of coordinates
Kind: instance method of MagikVector
Returns: number
- - the calculated dot product
Param | Type | Description |
---|---|---|
vector | MagikVector |
vector to calculate the dot product with |
Alias for dotProduct()
Kind: instance method of MagikVector
Returns: number
- - the calculated dot product
Param | Type | Description |
---|---|---|
vector | MagikVector |
vector to calculate the dot product with |
Normalises the Vector
Kind: instance method of MagikVector
Returns: MagikVector
- - the normalised Vector
Alias of normalise()
Kind: instance method of MagikVector
Returns: MagikVector
- - the normalised Vector
Limits the magnitude of the Vector to the supplied scalar value
Kind: instance method of MagikVector
Returns: MagikVector
- - the current instance
Param | Type | Description |
---|---|---|
scalar | number |
value to limit by |
Returns the string representation of the Vector
Kind: instance method of MagikVector
Returns: string
- - a string representation of the Vector
Returns a new MagikVector with random coordinates, defaults to a normalised 3D vector
Kind: static method of MagikVector
Param | Type | Default | Description |
---|---|---|---|
[dimensions] | number |
3 |
optional number of dimensions to use |
Alias for random()
Kind: static method of MagikVector
Returns: MagikVector
- - instance of the MagikVector Class
Param | Type | Default | Description |
---|---|---|---|
[dimensions] | number |
3 |
optional number of dimensions to use |
Returns a new vector with two random coordinates
Kind: static method of MagikVector
Returns: MagikVector
- - instance of MagikVector with two random values
Returns a new vector with three random coordinates, basically this is an alias for calling MagikVector.random() without any arguments.
Kind: static method of MagikVector
Returns: MagikVector
- - instance of MagikVector with three random values
Returns a random integer optionally bound by the minimum(included) and
maximum (included) arguments. If only one argument is supplied, it is
the maximum number (same as MagikVector.randomInteger(0, maximum)
)
Kind: static method of MagikVector
Returns: number
- - the random number
Param | Type | Description |
---|---|---|
[...args] | Object |
|
[args.minimum] | number |
optional minimum value |
[ars.maximum] | number |
optional maximum value |
Returns a random integer optionally bound by the minimum(included) and
maximum (included) arguments. If only one argument is supplied, it is
the maximum number (same as MagikVector.randomInteger(0, maximum)
)
Kind: static method of MagikVector
Returns: number
- - the random number
Param | Type | Description |
---|---|---|
[...args] | Object |
|
[args.minimum] | number |
optional minimum value |
[ars.maximum] | number |
optional maximum value |
Converts degrees to radians
Kind: static method of MagikVector
Returns: number
- - the converted degrees as radians
Param | Type | Description |
---|---|---|
degrees | number |
the number of degrees to convert |
Example (Convert degrees to Radians)
radians = MagikVector.toRadians(degrees);
Converts radians to degrees
Kind: static method of MagikVector
Returns: number
- - the converted radians as degrees
Param | Type | Description |
---|---|---|
radians | number |
the number of radians to convert |
Example (Convert Radian to degrees)
degrees = MagikVector.toDegrees(radians);
Copyright (C) 2016 - 2021 Bjørn Wikkeling (magikMaker)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.