Skip to content

Commit

Permalink
refactor: 🏷️ improve type support in Vector3D
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 16, 2024
1 parent 0419a86 commit e3d3e3a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/operations/Vector3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* SOFTWARE.
*/

import { Kilometers, Radians, linearDistance } from '../main';
import { Radians, linearDistance } from '../main';
import { Matrix } from './Matrix';
import { Vector } from './Vector';

Expand Down Expand Up @@ -125,36 +125,36 @@ export class Vector3D<T extends number = number> {
}

// / Return a copy of this [Vector3D] scaled by [n];
scale(n: number): Vector3D {
return new Vector3D(this.x * n, this.y * n, this.z * n);
scale(n: number): Vector3D<T> {
return new Vector3D<T>(this.x * n as T, this.y * n as T, this.z * n as T);
}

// / Return a copy of this [Vector3D] with the elements negated.
negate(): Vector3D<Kilometers> {
return this.scale(-1) as Vector3D<Kilometers>;
negate(): Vector3D<T> {
return this.scale(-1);
}

/**
* Return the Euclidean distance between this and another Vector3D.
* @param v The other Vector3D.
* @returns The distance between this and the other Vector3D.
*/
distance(v: Vector3D): number {
distance(v: Vector3D<T>): T {
return linearDistance(this, v);
}

/**
* Convert this to a unit Vector3D.
* @returns A unit Vector3D.
*/
normalize(): Vector3D {
normalize(): Vector3D<T> {
const m = this.magnitude();

if (m === 0) {
return Vector3D.origin;
return Vector3D.origin as Vector3D<T>;
}

return new Vector3D(this.x / m, this.y / m, this.z / m);
return new Vector3D<T>(this.x / m as T, this.y / m as T, this.z / m as T);
}

// Calculate the dot product of this and another [Vector3D].
Expand Down Expand Up @@ -259,7 +259,7 @@ export class Vector3D<T extends number = number> {
}

// / Return the unit vector that bisects this and another [Vector3D].
bisect(v: Vector3D): Vector3D {
bisect(v: Vector3D<T>): Vector3D<T> {
return this.scale(v.magnitude()).add(v.scale(this.magnitude())).normalize();
}

Expand Down

0 comments on commit e3d3e3a

Please sign in to comment.