ts-scikit
Version:
A scientific toolkit written in Typescript
131 lines • 3.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vector2 = void 0;
const tuple2_1 = require("./tuple2");
/**
* A vector with 2 components: x and y.
*/
class Vector2 extends tuple2_1.Tuple2 {
/**
* Constructs a new vector with two components.
* @param x the x-component.
* @param y the y-component.
*/
constructor(x, y) {
super(x, y);
}
/**
* The length of this vector.
* @returns the length of this vector.
*/
get length() {
return Math.sqrt(this.lengthSquared);
}
/**
* The length of this vector, squared.
* @returns the length of this vector, squared.
*/
get lengthSquared() {
return this.x * this.x + this.y * this.y;
}
/**
* Computes the negation -u of this vector u.
* @returns the negation.
*/
negate() {
return new Vector2(-this.x, -this.y);
}
/**
* Sets this vector to its negation.
* @returns a reference to this vector.
*/
negateEquals() {
this.x = -this.x;
this.y = -this.y;
return this;
}
/**
* Computes the unit vector with the same direction as this vector.
* @returns the unit vector.
*/
normalize() {
const d = this.length;
const s = (d > 0.0) ? 1.0 / d : 1.0;
return new Vector2(this.x * s, this.y * s);
}
/**
* Sets this vector to its unit vector.
* @returns a reference to this vector.
*/
normalizeEquals() {
const d = this.length;
const s = (d > 0.0) ? 1.0 / d : 1.0;
this.x *= s;
this.y *= s;
return this;
}
/**
* Returns the vector sum u + v for this vector u
* @param v the other vector.
* @returns the vector sum u + v.
*/
plus(v) {
return new Vector2(this.x + v.x, this.y + v.y);
}
/**
* Adds a vector v to this vector u.
* @param v the other vector.
* @returns a reference to this vector, after adding vector v.
*/
plusEquals(v) {
this.x += v.x;
this.y += v.y;
return this;
}
/**
* Returns the vector difference u - v for this vector u.
* @param v the other vector.
* @returns the vector difference u - v.
*/
minus(v) {
return new Vector2(this.x - v.x, this.y - v.y);
}
/**
* Subtracts a vector v from this vector u.
* @param v the other vector.
* @returns a reference to this vector, after subtracting vector v.
*/
minusEquals(v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
/**
* Returns the scaled vector s * u for this vector u.
* @param s the scale factor.
* @returns the scaled vector.
*/
times(s) {
return new Vector2(this.x * s, this.y * s);
}
/**
* Scales this vector.
* @param s the scale factor.
* @returns a reference to this vector, after scaling.
*/
timesEquals(s) {
this.x *= s;
this.y *= s;
return this;
}
/**
* Computes the dot product of this vector and the specified vector v.
* @param v the vector v.
* @returns the dot product.
*/
dot(v) {
return this.x * v.x + this.y * v.y;
}
}
exports.Vector2 = Vector2;
//# sourceMappingURL=vector2.js.map