vectors-co
Version:
A library for handling multi dimensional vectors
288 lines • 6.93 kB
JavaScript
export class MismatchedSizeError {
message;
name;
constructor(lhsWidth, rhsWidth) {
this.message = `The left hand side, and the right hand side have mis-matched sizes. ${lhsWidth} and ${rhsWidth} respectively`;
this.name = 'MismatchedSizeError';
}
}
/**
* This is the super class. A generically lengthened vector that takes in a type argument for the length.
*/
export class Vec {
points;
length;
constructor(points, length) {
this.points = points;
this.length = length;
}
/**
* This function creates a vector where each element is the same value
* @param value The value for each element to be
* @param length The length of the vector
*/
static fromScalar(value, length) {
return new Vec(new Array(length).fill(value), length);
}
/**
*
* @param rhs
* @protected
* @throws MismatchedSizeError
*/
_add(rhs) {
if (this.length !== rhs.length) {
throw new MismatchedSizeError(this.length, rhs.length);
}
return this.points.map((point, index) => point + rhs.points[index]);
}
/**
* Adds two vectors together
* @param rhs
* @throws MismatchedSizeError
*/
add(rhs) {
return new Vec(this._add(rhs), this.length);
}
/**
*
* @param rhs
* @protected
* @throws MismatchedSizeError
*/
_sub(rhs) {
if (this.length !== rhs.length) {
throw new MismatchedSizeError(this.length, rhs.length);
}
return this.points.map((point, index) => point - rhs.points[index]);
}
/**
* Subtracts two vectors togethers
* @param rhs
* @throws MismatchedSizeError
*/
sub(rhs) {
return new Vec(this._sub(rhs), this.length);
}
/**
*
* @param rhs
* @protected
* @throws MismatchedSizeError
*/
_mul(rhs) {
if (this.length !== rhs.length) {
throw new MismatchedSizeError(this.length, rhs.length);
}
return this.points.map((point, index) => point * rhs.points[index]);
}
/**
* Multiples each value of two vectors together
* @param rhs
* @throws MismatchedSizeError
*/
mul(rhs) {
return new Vec(this._mul(rhs), this.length);
}
/**
*
* @param rhs
* @protected
* @throws MismatchedSizeError
*/
_div(rhs) {
if (this.length !== rhs.length) {
throw new MismatchedSizeError(this.length, rhs.length);
}
return this.points.map((point, index) => point / rhs.points[index]);
}
_pow(power) {
return this.points.map((point) => point ** power);
}
/**
* Raises each element to the power given
* @param power
*/
pow(power) {
return new Vec(this._pow(power), this.length);
}
/**
* Divides each value of two vectors together
* @param rhs
* @protected
* @throws MismatchedSizeError
*/
div(rhs) {
return new Vec(this._div(rhs), this.length);
}
/**
* Gives the dot product of two vectors
* @param rhs
* @throws MismatchedSizeError
*/
dot(rhs) {
return this._mul(rhs).reduce((n, acc) => n + acc, 0);
}
/**
* Gives the magnitude of the vector
*/
get size() {
return this.dot(this) ** 0.5;
}
/**
* Gives the values of the vector summed up
*/
get sum() {
return this.points.reduce((n, acc) => n + acc, 0);
}
get _unit() {
const size = this.size;
return this.points.map((point) => point / size);
}
/**
* Returns a unit vector of the current vector
*/
get unit() {
return new Vec(this._unit, this.length);
}
}
export class Vec2 extends Vec {
constructor(points) {
super(points, 2);
}
add(rhs) {
return new Vec2(this._add(rhs));
}
sub(rhs) {
return new Vec2(this._sub(rhs));
}
mul(rhs) {
return new Vec2(this._mul(rhs));
}
div(rhs) {
return new Vec2(this._div(rhs));
}
pow(power) {
return new Vec2(this._pow(power));
}
get x() {
return this.points[0];
}
set x(val) {
this.points[0] = val;
}
get y() {
return this.points[1];
}
set y(val) {
this.points[1] = val;
}
get unit() {
return new Vec2(this._unit);
}
}
export class Vec3 extends Vec {
constructor(points) {
super(points, 3);
}
add(rhs) {
return new Vec3(this._add(rhs));
}
sub(rhs) {
return new Vec3(this._sub(rhs));
}
mul(rhs) {
return new Vec3(this._mul(rhs));
}
div(rhs) {
return new Vec3(this._div(rhs));
}
pow(power) {
return new Vec3(this._pow(power));
}
/**
* Calculates a cross product
* @param _rhs
* @throws MismatchedSizeError
*/
cross(_rhs) {
if (this.length !== _rhs.length) {
throw new MismatchedSizeError(this.length, _rhs.length);
}
const rhs = new Vec3(_rhs.points);
return new Vec3([
this.y * rhs.z - this.z * rhs.y,
this.z * rhs.x - this.x * rhs.z,
this.z * rhs.y - this.y * rhs.x
]);
}
get x() {
return this.points[0];
}
set x(val) {
this.points[0] = val;
}
get y() {
return this.points[1];
}
set y(val) {
this.points[1] = val;
}
get z() {
return this.points[2];
}
set z(val) {
this.points[2] = val;
}
get unit() {
return new Vec3(this._unit);
}
}
export class Vec4 extends Vec {
constructor(points) {
super(points, 4);
}
add(rhs) {
return new Vec4(this._add(rhs));
}
sub(rhs) {
return new Vec4(this._sub(rhs));
}
mul(rhs) {
return new Vec4(this._mul(rhs));
}
div(rhs) {
return new Vec4(this._div(rhs));
}
pow(power) {
return new Vec4(this._pow(power));
}
get x() {
return this.points[0];
}
set x(val) {
this.points[0] = val;
}
get y() {
return this.points[1];
}
set y(val) {
this.points[1] = val;
}
get z() {
return this.points[2];
}
set z(val) {
this.points[2] = val;
}
get w() {
return this.points[3];
}
set w(val) {
this.points[3] = val;
}
get unit() {
return new Vec4(this._unit);
}
}
//# sourceMappingURL=main.js.map