83 lines
1.6 KiB
TypeScript
83 lines
1.6 KiB
TypeScript
import { UpdatedRecorder } from "./UpdatedRecorder";
|
|
|
|
/**
|
|
* A vector with two dimensions
|
|
*/
|
|
class Vector2D extends UpdatedRecorder implements ArrayLike<number>, Iterable<number> {
|
|
|
|
private value0: number = 0;
|
|
private value1: number = 0;
|
|
|
|
readonly [n: number]: number;
|
|
|
|
/**
|
|
* Vector length
|
|
*/
|
|
public get length(): number {
|
|
return 2;
|
|
};
|
|
|
|
public override toString(): string {
|
|
return `Vector2D(${this.value0},${this.value1})`;
|
|
}
|
|
|
|
/**
|
|
* Enumerate this vector
|
|
*/
|
|
public *[Symbol.iterator](): Iterator<number> {
|
|
yield this.value0;
|
|
yield this.value1;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
|
|
export { Vector2D }; |