1.0.6 • Published 6 months ago

simple-2d-vectors v1.0.6

Weekly downloads
-
License
-
Repository
-
Last release
6 months ago

simple-2d-vectors

A lightweight TypeScript library for creating and manipulating 2d vectors. I created it because all vector libraries out there were either too old, depending on browser API or too heavy.

https://www.npmjs.com/package/simple-2d-vectors

https://github.com/lunarW1TCH/simple-2d-vectors

Quick start

import { Vector } from 'simple-2d-vectors';

const vector = new Vector([3, 4]);

Parameters

Most methods require you to pass a PointLike datatype, which is a union between a Vector, a Point and an ArrayPoint:

type PointLike = Vector | Point | ArrayPoint;
type Point = {
  x: number;
  y: number;
};
type ArrayPoint = [number, number];
class Vector implements Point {
  // ...
}

No matter which type you choose, the result will be the same.

const v1 = new Vector([3, 4]);
const v2 = new Vector({ x: 3, y: 4 });
const v3 = new Vector(v1);

Methods

Methods called on a vector object modify it in-place. To avoid changing the original object you can either first copy the vector or use the static version of a method (most methods have both static and non-static versions).

const vec = new Vector([3, 4]);

const copy1 = vec.copy();
const copy2 = new Vector(vec);

copy1.add([1, 2]); // `vec` isn't affected by this operation
copy2.add([1, 2]); // neither by this one

const newVector = Vector.add([3, 4], [1, 2]); // returns a new vector
MethodNon-staticStaticNon-static paramsStatic paramsReturnsDescription
copy()VectorReturns a copy of a vector.
add()param: PointlikeparamA: PointLike, paramB: PointLikeVectorSums individually x and y components.
subtract()param: PointlikeparamA: PointLike, paramB: PointLikeVectorSubtracts x and y components from each other.
multiply()param: PointlikeparamA: PointLike, paramB: PointLikeVectorMultiplies x and y components.
divide()param: PointlikeparamA: PointLike, paramB: PointLikeVectorDivides x and y components.
invertX()VectorInverts x component of a vector.
invertY()VectorInverts y component of a vector.
invert()VectorInverts both components of a vector.
magnitude()param: PointlikenumberReturns magnitude/length of the vector.
setMagnitude()newMag: numberparam: Pointlike, newMag: numberVectorSets the magnitude/length of the vector to the provided value (>= 0) while keeping its direction.
normalize()param: PointlikeVectorSets length/magnitude of a vector to 1 while keeping its direction.
rotateBy()radians: numberparam: Pointlike, radians: numberVectorRotates the vector by the provided value (in radians).
rotateByDeg()degrees: numberparam: Pointlike, degrees: numberVectorRotates the vector by the provided value (in degrees).
rotateTo()radians: numberparam: Pointlike, radians: numberVectorRotates vector to a provided angle (in radians), counting from the positive X axis, while keeping its magnitude.
rotateToDeg()degrees: numberparam: Pointlike, degrees: numberVectorRotates vector to a provided angle (in degrees), counting from the positive X axis, while keeping its magnitude.
dotProduct()param: PointLikeparamA: Pointlike, paramB: PointLikenumberReturns a dot products of two vectors.
angle()param: PointLikenumberReturns the angle (in radians) between any Vector(x, 0) and the original vector, where x is positive.
angleDeg()param: PointLikenumberReturns the angle (in degrees) between any Vector(x, 0) and the original vector, where x is positive.
angleBetween()param: PointLikeparamA: Pointlike, paramB: PointLikenumberReturns the angle (in radians) between original vector and provided vector.
angleBetweenDeg()param: PointLikeparamA: Pointlike, paramB: PointLikenumberReturns the angle (in degrees) between original vector and provided vector.
toString()stringReturns a string representation of a vector '(x, y)'.
toPoint()PointReturns x and y components as a Point.
toArray()ArrayPointReturns x and y components as an ArrayPoint.
fromPoints()paramA: Pointlike, paramB: PointLikeVectorReturns a vector which is a path between two provided points.
fromAngle()radians: number, length?: numberVectorReturns a vector with a provided angle (in radians) counting from positive X axis. Length is optional and defaults to 1.

Properties

PropertyTypeDescription
xnumberx component of a Vector
ynumbery component of a Vector
1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago