From 3899fc902b3e64be348654f681fcb5474796abc1 Mon Sep 17 00:00:00 2001 From: fdai7303 Date: Thu, 1 Jun 2023 09:48:20 +0200 Subject: [PATCH] continued exercise 5 --- src/05/ray.ts | 9 ++++++++- src/05/sphere.ts | 20 +++++++++++++++++++- src/05/vector.ts | 5 ++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/05/ray.ts b/src/05/ray.ts index ff38d30..3bebd27 100644 --- a/src/05/ray.ts +++ b/src/05/ray.ts @@ -33,6 +33,13 @@ export default class Ray { // TODO: on the image plane. In addition to the coordinates (x, y), you will need the // TODO: width and height of the camera (i.e. the width and height of the camera's // TODO: image plane), and the angle alpha specifying the camera's field of view. - return null; + var temp = new Vector(0,0,0,0); + temp.x = x - ((camera.width-1) / 2); + temp.y = ((camera.height-1) / 2) - y; + temp.z = - ((camera.width/2) / (Math.tan(camera.alpha/2))); + + temp = temp.normalize(); + + return new Ray(camera.origin, temp); } } diff --git a/src/05/sphere.ts b/src/05/sphere.ts index bbf22cc..2024b7a 100644 --- a/src/05/sphere.ts +++ b/src/05/sphere.ts @@ -44,6 +44,24 @@ export default class Sphere { // TODO: Return an Intersection or null if there was no hit. In case // TODO: of two hits, return the one closer to the start point of // TODO: the ray. - return null; + + let c = Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2); + // hier oben noch nicht berechenenm da c negativ sein kann und dann wurzel nicht funktioniert + + if(c < 0) { + return null; + } else if(c == 0) { + return new Intersection(t1, ); //schnittpunkt ist origin + direction * t + + } else { + let t1 = - ray.origin.dot(ray.direction) + Math.sqrt( Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2)); + let t2 = - ray.origin.dot(ray.direction) - Math.sqrt( Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2)); + + if(t1 < t2) { + return null; + } else { + return null; + } + } } } diff --git a/src/05/vector.ts b/src/05/vector.ts index 50ff1ad..4d20310 100644 --- a/src/05/vector.ts +++ b/src/05/vector.ts @@ -238,7 +238,10 @@ export default class Vector { */ normalize(): Vector { // TODO: Normalize this vector and return it - return this.div(Math.sqrt(Math.pow(this.data[0],2)+Math.pow(this.data[1],2)+Math.pow(this.data[2],2))); + this.x = this.x/Math.sqrt(Math.pow(this.data[0],2)+Math.pow(this.data[1],2)+Math.pow(this.data[2],2)); + this.y = this.y/Math.sqrt(Math.pow(this.data[0],2)+Math.pow(this.data[1],2)+Math.pow(this.data[2],2)); + this.z = this.z/Math.sqrt(Math.pow(this.data[0],2)+Math.pow(this.data[1],2)+Math.pow(this.data[2],2)); + return this; } /**