-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
34 lines (29 loc) · 913 Bytes
/
Sphere.cpp
File metadata and controls
34 lines (29 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Sphere.h"
#include "Vector.h"
#include <algorithm>
#include <cmath>
using namespace chromeball;
Sphere::Sphere(const Vector &p, float r, const Color &c)
: SceneObject{c}, position{p}, radius{r} {}
float Sphere::intersection(const Ray &r) const {
// position - center
Vector pc = r.get_position() - this->position;
float a = r.get_direction() * r.get_direction();
float b = 2 * (r.get_direction() * pc);
float c = (pc * pc) - (radius * radius);
float discriminant = (b * b) - (4 * a * c);
if (discriminant < 0) {
// No intersections were made.
return -1.0f;
} else {
// An intersection is made! Return the smallest t value where t > 0.
float t1 = (-b + sqrt(discriminant)) / (2 * a);
float t2 = (-b - sqrt(discriminant)) / (2 * a);
if (t1 > 0 && t2 > 0)
return std::min(t1, t2);
else if (t1 > 0)
return t1;
else
return t2;
}
}