MagikVector
MagikVector 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
MagikVector~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]) ⇒
new MagikVector([...args])
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);
number
magikVector.length ⇒ 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
number
magikVector.x ⇒ 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
number
magikVector.y ⇒ 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
number
magikVector.z ⇒ 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
void
magikVector.x ⇒ 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;
void
magikVector.y ⇒ 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;
void
magikVector.z ⇒ 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;
number
magikVector.getX() ⇒ Returns the x
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- - the x coordinate
number
magikVector.getY() ⇒ Returns the y
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- the x coordinate
number
magikVector.getZ() ⇒ Returns the z
coordinate of this MagikVector instance.
Kind: instance method of MagikVector
Returns: number
- the x coordinate
number
| undefined
magikVector.getCoordinate(index) ⇒ 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 |
number
| undefined
magikVector.getCoord(index) ⇒ Alias of MagikVector.getCoordinate()
Kind: instance method of MagikVector
Returns: number
| undefined
- - value at the specified index or undefined
Param | Type |
---|---|
index | number |
MagikVector
magikVector.setX(value) ⇒ 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);
MagikVector
magikVector.setY(value) ⇒ 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);
MagikVector
magikVector.setZ(value) ⇒ 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);
MagikVector
magikVector.setCoordinate(index, value) ⇒ 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 |
MagikVector
magikVector.setCoord(index, value) ⇒ 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 |
MagikVector
magikVector.add(vector) ⇒ 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 |
MagikVector
magikVector.subtract(vector) ⇒ 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 |
MagikVector
magikVector.sub(vector) ⇒ 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 |
MagikVector
| number
magikVector.multiply(value) ⇒ 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 |
MagikVector
| number
magikVector.mult(value) ⇒ 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 |
MagikVector
magikVector.divide(value) ⇒ 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 |
MagikVector
magikVector.div(value) ⇒ 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 |
MagikVector
magikVector.clone() ⇒ 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
number
magikVector.getMagnitudeSquared() ⇒ Returns the magnitude squared
Kind: instance method of MagikVector
Returns: number
- - the magnitude squared value of the current instance
number
magikVector.getMagnitude() ⇒ Returns the magnitude of the vector
Kind: instance method of MagikVector
Returns: number
- - the magnitude of the current instance
number
magikVector.getMag() ⇒ Returns the magnitude of the vector
Kind: instance method of MagikVector
Returns: number
- - the magnitude of the current instance
MagikVector
magikVector.setMagnitude(magnitude) ⇒ 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 |
MagikVector
magikVector.setMag(magnitude) ⇒ Alias for the setMagnitude()
method
Kind: instance method of MagikVector
Returns: MagikVector
- the adjusted MagikVector object
Param | Type |
---|---|
magnitude | number |
number
magikVector.getDirection() ⇒ 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
number
magikVector.getDir() ⇒ Alias for the getDirection() method
Kind: instance method of MagikVector
Returns: number
- - the angle of rotation in radians
number
magikVector.getDistanceTo(vector) ⇒ 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 |
number
magikVector.getDistance(vector) ⇒ 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 |
number
magikVector.dotProduct(vector) ⇒ 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 |
number
magikVector.dot(vector) ⇒ 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 |
MagikVector
magikVector.normalise() ⇒ Normalises the Vector
Kind: instance method of MagikVector
Returns: MagikVector
- - the normalised Vector
MagikVector
magikVector.normalize() ⇒ Alias of normalise()
Kind: instance method of MagikVector
Returns: MagikVector
- - the normalised Vector
MagikVector
magikVector.limit(scalar) ⇒ 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 |
string
magikVector.toString() ⇒ Returns the string representation of the Vector
Kind: instance method of MagikVector
Returns: string
- - a string representation of the Vector
MagikVector
MagikVector.random([dimensions]) ⇒ 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 |
MagikVector
MagikVector.rand([dimensions]) ⇒ 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 |
MagikVector
MagikVector.random2D() ⇒ Returns a new vector with two random coordinates
Kind: static method of MagikVector
Returns: MagikVector
- - instance of MagikVector with two random values
MagikVector
MagikVector.random3D() ⇒ 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
number
MagikVector.randomInteger([...args]) ⇒ 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 |
number
MagikVector.randomInt([...args]) ⇒ 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 |
number
MagikVector.toRadians(degrees) ⇒ 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);
number
MagikVector.toDegrees(radians) ⇒ 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);
License
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.