From 7ad1b7efabea1349107669a432e6c88305f8d825 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Sat, 1 Jan 2022 21:21:52 +0000 Subject: Implement basic ray tracing --- src/Vector.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Vector.ts (limited to 'src/Vector.ts') diff --git a/src/Vector.ts b/src/Vector.ts new file mode 100644 index 0000000..3cebf6d --- /dev/null +++ b/src/Vector.ts @@ -0,0 +1,36 @@ +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); + } +} -- cgit v1.2.3