LVector
is a TypeScript library that simplifies 2D vector manipulation. It offers methods to perform common vector operations like addition, subtraction, multiplication, division, scaling, and more.
You can install this library via npm:
npm install lvector
Import the Vector2
class into your TypeScript project:
import { Vector2 } from 'lvector';
To create a 2D vector, use the constructor:
const vector = new Vector2(x, y);
- Addition:
const result = vector1.add(vector2);
- Subtraction:
const result = vector1.subtract(vector2);
- Multiplication:
const result = vector1.multiply(vector2);
- Division:
const result = vector1.divide(vector2);
- Scaling:
const scaledVector = vector.scale(factor);
- Equality Check:
// Checks if two vectors are equal. Epsilon is optional, defaults to 0.001.
const isEqual = vector1.isEqual(vector2, epsilon);
- Magnitude:
const magnitude = vector.mag();
- Dot Product:
const dotProduct = v1.dot(v2);
- Move To:
const origin = new Vector2(10, 10);
const destination = new Vector2(20, 20);
// Interpolation value from 0 to 1
let interp = 0.5;
const interpolatedPosition = origin.moveTo(destination, interp);
// Result: interpolatedPosition is (15, 15)
- Angle:
const angleRadians = vector.angle();
- Angle in Degrees:
const angleDegrees = vector.angleDeg();
- Normalization:
const normalizedVector = vector.normalize();
- Rotation by 90 Degrees:
const rotatedVector = vector.r90deg();
- Rotation by 180 Degrees:
const rotatedVector = vector.r180deg();
- Rotation by 270 Degrees:
const rotatedVector = vector.r270deg();
- Create a Copy:
const copy = vector.copy();
Unit tests are available to ensure the correctness of the library by running the command as shown below:
npm run test
Feel free to contribute to this project by submitting issues or pull requests. Your feedback and contributions are highly appreciated.
This library is licensed under the GPL-v3 License.