Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Commit e3d3e3a

Browse files
committed
refactor: 🏷️ improve type support in Vector3D
1 parent 0419a86 commit e3d3e3a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/operations/Vector3D.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* SOFTWARE.
2222
*/
2323

24-
import { Kilometers, Radians, linearDistance } from '../main';
24+
import { Radians, linearDistance } from '../main';
2525
import { Matrix } from './Matrix';
2626
import { Vector } from './Vector';
2727

@@ -125,36 +125,36 @@ export class Vector3D<T extends number = number> {
125125
}
126126

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

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

137137
/**
138138
* Return the Euclidean distance between this and another Vector3D.
139139
* @param v The other Vector3D.
140140
* @returns The distance between this and the other Vector3D.
141141
*/
142-
distance(v: Vector3D): number {
142+
distance(v: Vector3D<T>): T {
143143
return linearDistance(this, v);
144144
}
145145

146146
/**
147147
* Convert this to a unit Vector3D.
148148
* @returns A unit Vector3D.
149149
*/
150-
normalize(): Vector3D {
150+
normalize(): Vector3D<T> {
151151
const m = this.magnitude();
152152

153153
if (m === 0) {
154-
return Vector3D.origin;
154+
return Vector3D.origin as Vector3D<T>;
155155
}
156156

157-
return new Vector3D(this.x / m, this.y / m, this.z / m);
157+
return new Vector3D<T>(this.x / m as T, this.y / m as T, this.z / m as T);
158158
}
159159

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

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

0 commit comments

Comments
 (0)