Update constructor signatures

This commit is contained in:
Eugene 2024-12-19 16:29:16 +03:00
parent 2870230eff
commit 06d9da662d
3 changed files with 28 additions and 9 deletions

View File

@ -20,7 +20,8 @@
"predeploy": "npm run dist",
"lint": "eslint src --ext .ts",
"lint:fix": "eslint src --ext .ts --fix",
"format": "prettier --write ./src/**/*.ts"
"format": "prettier --write ./src/**/*.ts",
"test": "jest"
},
"devDependencies": {
"@jest/globals": "^29.7.0",

View File

@ -120,11 +120,11 @@ describe("Vector2D", () => {
expect(result.getY()).toBeCloseTo(1);
});
test('lerp interpolates correctly', () => {
test("lerp interpolates correctly", () => {
const vector1 = new Vector2D(0, 0);
const vector2 = new Vector2D(10, 10);
const result = vector1.lerp(vector2, 0.5);
expect(result.getX()).toBe(5);
expect(result.getY()).toBe(5);
});
});
});

View File

@ -2,14 +2,32 @@ export class Vector2D {
private x: number;
private y: number;
constructor();
/**
* Creates a new vector with coordinates of provided Vector2D.
* @param {Vector2D} vec - The x-coordinate.
*/
constructor(vec: Vector2D);
/**
* Creates a new vector with coordinates (x, y).
* @param {number} x - The x-coordinate.
* @param {number} y - The y-coordinate.
* @param {number} a - The x-coordinate.
* @param {number} b - The y-coordinate.
*/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
constructor(a: number, b: number);
constructor(a?: number | Vector2D, b?: number) {
if (a === undefined && b === undefined) {
this.x = this.y = 0;
return;
}
if (a instanceof Vector2D) {
this.x = a.x ?? 0;
this.y = a.y ?? 0;
} else {
this.x = a ?? 0;
this.y = b ?? 0;
}
}
/**
@ -130,7 +148,7 @@ export class Vector2D {
* @returns {Vector2D} The cloned vector.
*/
clone(): Vector2D {
return new Vector2D(this.x, this.y);
return new Vector2D(this);
}
/**