Hosting code to git for the first time.

This commit is contained in:
2023-02-02 17:29:05 +08:00
parent 398469ebe4
commit c8fe5854ab
57 changed files with 2419 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
export { Vector2D } from "./Vector2D";
export { Vector3D } from "./Vector3D";
export { Vector4D } from "./Vector4D";
+54
View File
@@ -0,0 +1,54 @@
/**
* Type of update status can be recorded
* An update flag is used here to implement data lazy calculation
* @copyright MrKBear 2022
*/
abstract class UpdatedRecorder {
/**
* The flag for recorded update status
* @defaultValue `true`
*/
protected updateFlag: boolean = true;
/**
* Initialize and set the default update status
* @param updateFlag update status
*/
public constructor(updateFlag: boolean = true) {
this.updateFlag = updateFlag;
}
/**
* Return the update status
* @returns update status
*/
public isUpdated(): boolean {
return this.updateFlag;
}
/**
* Set update status `false`
* @returns update status
*/
public unsetUpdated(): false {
return this.updateFlag = false;
}
/**
* Automatically detect and reset update status
* @returns update status
*/
public testAndUnsetUpdate(): boolean {
if (this.updateFlag) {
this.updateFlag = false;
return true;
}
else {
return false;
}
}
}
export { UpdatedRecorder };
+83
View File
@@ -0,0 +1,83 @@
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 };
+110
View File
@@ -0,0 +1,110 @@
import { UpdatedRecorder } from "./UpdatedRecorder";
/**
* A vector with three dimensions
*/
class Vector3D extends UpdatedRecorder implements ArrayLike<number>, Iterable<number> {
private value0: number = 0;
private value1: number = 0;
private value2: number = 0;
readonly [n: number]: number;
/**
* Vector length
*/
public get length(): number {
return 3;
};
public override toString(): string {
return `Vector3D(${this.value0},${this.value1},${this.value2})`;
}
/**
* Enumerate this vector
*/
public *[Symbol.iterator](): Iterator<number> {
yield this.value0;
yield this.value1;
yield this.value2;
}
/**
* 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;
}
/**
* 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;
}
}
export { Vector3D };
+137
View File
@@ -0,0 +1,137 @@
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 };