0.1.1 • Published 3 months ago

lvector v0.1.1

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
3 months ago

LVector - 2D Vector Library for TypeScript

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.

Installation

You can install this library via npm:

npm install lvector

Usage

Import the Vector2 class into your TypeScript project:

import { Vector2 } from 'lvector';

Creating a Vector

To create a 2D vector, use the constructor:

const vector = new Vector2(x, y);

Common Operations

  • 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();

Testing

Unit tests are available to ensure the correctness of the library by running the command as shown below:

npm run test

Contributing

Feel free to contribute to this project by submitting issues or pull requests. Your feedback and contributions are highly appreciated.

License

This library is licensed under the GPL-v3 License.

0.1.1

3 months ago

0.1.0

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago