Vector2
Class that represents a point in a 2-dimensional space.
Usage
import { Vector2 } from '@krutoo/utils';
const vector = new Vector2(10, 20);
Static methods
of
Creates new vector by x and y.
const vector = Vector2.of(10, 20);
from
Creates new vector from object that implements { x: number; y: number }.
const vector = Vector2.from({ x: 10, y: 20 });
Instance methods
setX
Changes x coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.setX(99);
setY
Changes y coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.setY(99);
addX
Adds value to the x coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.addX(99);
addY
Adds value to the y coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.addY(99);
subX
Subtracts value to the x coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.subX(99);
subY
Subtracts value to the y coordinate.
This method is mutable
const vector = Vector2.of(10, 20);
vector.subY(99);
add
Adds other vector.
This method is mutable
const vector1 = Vector2.of(10, 20);
const vector2 = Vector2.of(20, 30);
vector1.add(vector2);
subtract
Subtracts other vector.
This method is mutable
const vector1 = Vector2.of(10, 20);
const vector2 = Vector2.of(20, 30);
vector1.subtract(vector2);
scale
Scale vector by scalar.
This method is mutable
const vector = Vector2.of(10, 20);
vector.scale(10);
normalize
Scales vector to make it length equal to 1.
This method is mutable
const vector = Vector2.of(10, 20);
vector.normalize();
rotate
Rotates vector around origin by given radians.
This method is mutable
const vector = Vector2.of(10, 20);
// rotate by 3 radians
vector.rotate(3);
rotateAround
Rotates vector around origin by given radians.
This method is mutable
const vector = Vector2.of(10, 20);
const pivot = Vector2.of(5, 5);
// rotate around pivot by 3 radians
vector.rotateAround(pivot, 3);
getLength
Returns vector length.
const vector = Vector2.of(10, 20);
vector.getLength(); // number
getDistance
Returns distance to other vector.
const vector1 = Vector2.of(10, 20);
const vector2 = Vector2.of(20, 30);
vector1.getDistance(vector2); // number
equalsTo
Checks that vector is equal to other vector.
const vector1 = Vector2.of(10, 20);
const vector2 = Vector2.of(20, 30);
vector1.equalsTo(vector2); // boolean
clone
Creates new copy of vector.
Most of the methods are mutable so it changes the instance.
But you can use clone() to work in immutable manner.
const vector1 = Vector2.of(10, 20);
const vector2 = vector1.clone();
console.log(vector1 === vector2); // false
console.log(vector1.equalsTo(vector2)); // true
copy
Copies coordinate values from given vector like object.
Mutates instance.
const vector = Vector2.of(10, 20);
vector.copy({ x: 9.9, y: 11.5 });
console.log(vector.x, vector.y); // 9.9 11.5
toJSON
Returns plain object with vector coordinates.
const vector = new Vector2(10, 20);
console.log(vector.toJSON()); // { x: 10, y: 20 }
Methods chaining
Most ot the instance methods are chainable:
const vector = new Vector2(10, 20);
vector
.setX(20)
.setY(20)
.add(new Vector2(2, 3))
.subtract(new Vector2(4, 5))
.scale(10)
.normalize(10)
.clone();
Mutability
Most of the methods are mutable so it changes the instance.
You can use clone() to work in immutable manner.