mv

magik-vector

Vector class for 2D, 3D and multi dimensional vector calculations

Showing:

Popularity

Downloads/wk

4

GitHub Stars

1

Maintenance

Last Commit

2mos ago

Contributors

4

Package

Dependencies

0

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

CircleCI NPM version npm module downloads

MagikVector

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

Kind: inner class of MagikVector

new MagikVector([...args])

Initialise a new Vector instance with coordinates as arguments, either supply the individual coordinates or supply an array with number values.

ParamTypeDescription
[...args]number | arrayoptional 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

ParamTypeDescription
valuenumberthe 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

ParamTypeDescription
valuenumberthe 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

ParamTypeDescription
valuenumberthe 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()
ParamType
indexnumber

magikVector.getCoord(index) ⇒ number | undefined

Alias of MagikVector.getCoordinate()

Kind: instance method of MagikVector
Returns: number | undefined - - value at the specified index or undefined

ParamType
indexnumber

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

ParamTypeDescription
valuenumberthe 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

ParamTypeDescription
valuenumberthe 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

ParamTypeDescription
valuenumberthe 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()
ParamTypeDescription
indexnumberthe index to set the value for
valuenumberthe value to set

magikVector.setCoord(index, value) ⇒ MagikVector

Alias of MagikVector.setCoordinate()

Kind: instance method of MagikVector
Returns: MagikVector - the object itself

ParamTypeDescription
indexnumberthe index to set the value for
valuenumberthe 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

ParamTypeDescription
vectorMagikVectorvector 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

ParamTypeDescription
vectorMagikVectorvector to subtract from current instance

magikVector.sub(vector) ⇒ MagikVector

Alias of MagikVector.substract()

Kind: instance method of MagikVector
Returns: MagikVector - - returns the current Vector

ParamTypeDescription
vectorMagikVectorvector 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

ParamTypeDescription
valueMagikVector | numberscalar 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

ParamTypeDescription
valueMagikVector | numberscalar 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

ParamTypeDescription
valueMagikVector | numberscalar 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

ParamTypeDescription
valueMagikVector | numberscalar 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

ParamTypeDescription
magnitudenumbermagnitude 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

ParamType
magnitudenumber

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

ParamTypeDescription
vectorMagikVectorthe 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

ParamTypeDescription
vectorMagikVectorthe 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

ParamTypeDescription
vectorMagikVectorvector to calculate the dot product with

magikVector.dot(vector) ⇒ number

Alias for dotProduct()

Kind: instance method of MagikVector
Returns: number - - the calculated dot product

ParamTypeDescription
vectorMagikVectorvector 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

ParamTypeDescription
scalarnumbervalue 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

ParamTypeDefaultDescription
[dimensions]number3optional number of dimensions to use

MagikVector.rand([dimensions]) ⇒ MagikVector

Alias for random()

Kind: static method of MagikVector
Returns: MagikVector - - instance of the MagikVector Class

ParamTypeDefaultDescription
[dimensions]number3optional 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

ParamTypeDescription
[...args]Object
[args.minimum]numberoptional minimum value
[ars.maximum]numberoptional 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

ParamTypeDescription
[...args]Object
[args.minimum]numberoptional minimum value
[ars.maximum]numberoptional maximum value

MagikVector.toRadians(degrees) ⇒ number

Converts degrees to radians

Kind: static method of MagikVector
Returns: number - - the converted degrees as radians

ParamTypeDescription
degreesnumberthe 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

ParamTypeDescription
radiansnumberthe 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.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial