ray-lab/engine/math/Vector4D.ts

137 lines
2.7 KiB
TypeScript

import { UpdatedRecorder } from "./UpdatedRecorder";
/**
* A vector with four dimensions
*/
class Vector4D extends UpdatedRecorder implements ArrayLike<number>, Iterable<number> {
private value0: number = 0;
private value1: number = 0;
private value2: number = 0;
private value3: number = 0;
readonly [n: number]: number;
/**
* Vector length
*/
public get length(): number {
return 4;
};
public override toString(): string {
return `Vector4D(${this.value0},${this.value1},${this.value2},${this.value3})`;
}
/**
* Enumerate this vector
*/
public *[Symbol.iterator](): Iterator<number> {
yield this.value0;
yield this.value1;
yield this.value2;
yield this.value3;
}
/**
* The first component of this vector
*/
public get [0](): number {
return this.value0;
}
public set [0](newValue: number) {
if (!this.updateFlag && this.value0 !== newValue) {
this.updateFlag = true;
}
this.value0 = newValue;
}
/**
* The second component of this vector
*/
public get [1](): number {
return this.value1;
}
public set [1](newValue: number) {
if (!this.updateFlag && this.value1 !== newValue) {
this.updateFlag = true;
}
this.value1 = newValue;
}
/**
* The third component of this vector
*/
public get [2](): number {
return this.value2;
}
public set [2](newValue: number) {
if (!this.updateFlag && this.value2 !== newValue) {
this.updateFlag = true;
}
this.value2 = newValue;
}
/**
* The fourth component of this vector
*/
public get [3](): number {
return this.value3;
}
public set [3](newValue: number) {
if (!this.updateFlag && this.value3 !== newValue) {
this.updateFlag = true;
}
this.value3 = newValue;
}
/**
* X component of the vector
*/
public get x(): number {
return this.value0;
}
public set x(newValue: number) {
this[0] = newValue;
}
/**
* Y component of the vector
*/
public get y(): number {
return this.value1;
}
public set y(newValue: number) {
this[1] = newValue;
}
/**
* Z component of the vector
*/
public get z(): number {
return this.value2;
}
public set z(newValue: number) {
this[2] = newValue;
}
/**
* W component of the vector
*/
public get w(): number {
return this.value3;
}
public set w(newValue: number) {
this[3] = newValue;
}
}
export { Vector4D };