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