Browse Source

continued exercise 5

master
fdai7303 2 years ago
parent
commit
3899fc902b
  1. 9
      src/05/ray.ts
  2. 20
      src/05/sphere.ts
  3. 5
      src/05/vector.ts

9
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);
}
}

20
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;
}
}
}
}

5
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;
}
/**

Loading…
Cancel
Save