diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2025-07-26 11:17:10 +0100 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2025-07-26 11:17:10 +0100 |
| commit | 10bfc58085c6ab7e62077a1a9b6a6d922fffb025 (patch) | |
| tree | 3e1d3ff39891c2c2408235c209566f4c51915859 /src/models/Vector.ts | |
| parent | ebc500c522e7233fbc6037bce0569edf3a0b5136 (diff) | |
| download | js-raytracer-10bfc58085c6ab7e62077a1a9b6a6d922fffb025.tar.xz js-raytracer-10bfc58085c6ab7e62077a1a9b6a6d922fffb025.zip | |
Rework multithreaded rendering
Render worker tasks are now split into chunks rather than row blocks for better thread utilisation
Diffstat (limited to 'src/models/Vector.ts')
| -rw-r--r-- | src/models/Vector.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/models/Vector.ts b/src/models/Vector.ts new file mode 100644 index 0000000..2be8e57 --- /dev/null +++ b/src/models/Vector.ts @@ -0,0 +1,40 @@ +export class Vector { + constructor(readonly x: number, readonly y: number, readonly z: number) {} + + add(v: Vector): Vector { + return new Vector(this.x + v.x, this.y + v.y, this.z + v.z); + } + + addScalar(n: number): Vector { + return new Vector(this.x + n, this.y + n, this.z + n); + } + + subtract(v: Vector): Vector { + return new Vector(this.x - v.x, this.y - v.y, this.z - v.z); + } + + multiply(n: number): Vector { + return new Vector(this.x * n, this.y * n, this.z * n); + } + + dotProduct(v: Vector): number { + return this.x * v.x + this.y * v.y + this.z * v.z; + } + + normalise(): Vector { + const vecLength = this.norm(); + return new Vector( + this.x / vecLength, + this.y / vecLength, + this.z / vecLength + ); + } + + norm(): number { + return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); + } + + negative(): Vector { + return new Vector(-this.x, -this.y, -this.z); + } +} |