@rnacanvas/vectors.oopified
v2.1.1
Published
Work with vectors in an object-oriented way
Readme
Installation
With npm:
npm install @rnacanvas/vectors.oopifiedUsage
All exports of this package can be accessed as named imports.
// an example import
import { Vector } from '@rnacanvas/vectors.oopified';Vector
The Vector class represents a two-dimensional vector.
var vector = new Vector(56.1, -88.2);
vector.x; // 56.1
vector.y; // -88.2
// vectors are mutable
vector.x = -101.4;
vector.y = 18.9;
// vectors are iterable
[...vector]; // [-101.4, 18.9]magnitude
The magnitude of the vector.
var vector = new Vector(5, 12);
vector.magnitude; // 13direction
The angle (in radians) that is the direction of the vector in the standard Cartesian coordinate system.
var vector = new Vector(100, 0);
vector.direction; // 0
vector.y = 100;
vector.direction; // Math.PI / 4
vector.x = 0;
vector.direction; // Math.PI / 2
vector.x = -100;
vector.y = -100;
vector.direction; // -3 * Math.PI / 4static matching()
Creates a new vector instance with the same X and Y components (and magnitude and direction) as the input vector-like object.
(See type definition below for VectorLike objects.)
var vector = Vector.matching({ x: 57, y: 36 });
vector.x; // 57
vector.y; // 36
vector = Vector.matching({ magitude: 2, direction: Math.PI / 3 });
vector.x; // 1
vector.y; // 3**0.5type VectorLike
For objects that are similar to vectors.
type VectorLike = (
{
readonly x: number;
readonly y: number;
}
| {
readonly magnitude: number;
/**
* In radians.
*/
readonly direction: number;
}
);