-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.cpp
74 lines (54 loc) · 1.21 KB
/
sphere.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// File: sphere.cpp
// Author: Samuel McFalls
// Description: Implements the Sphere class
#include "sphere.hpp"
Sphere::Sphere() {
color = Color();
lambert = 0;
center = Vec3();
radius = 0;
}
Sphere::Sphere(const Color &colorCon, double lambertCon, const Vec3 ¢erCon,
double radiusCon) {
color = colorCon;
lambert = lambertCon;
center = centerCon;
radius = radiusCon;
}
Vec3 Sphere::getCenter() const {
return center;
}
double Sphere::getRadius() const {
return radius;
}
double Sphere::intersectedBy(Ray ray) const {
Vec3 L = ray.getOrigin() - center;
double a = ray.getDirection().dot(ray.getDirection());
double b = 2 * (L.dot(ray.getDirection()));
double c = L.dot(L) - pow(radius, 2);
double delta = pow(b, 2) - (4 * a * c);
double t0, t1;
if (delta < 0) {
return std::numeric_limits<double>::infinity();
}
if (delta == 0) {
t0 = -(b / (2 * a));
t1 = t0;
}
else {
t0 = (-b + sqrt(delta)) / (2 * a);
t1 = (-b - sqrt(delta)) / (2 * a);
}
double small = 1e-9;
if (t0 < small && t1 < small) {
return std::numeric_limits<double>::infinity();
}
if (t0 < small) {
return t1;
}
if (t1 < small) {
return t0;
}
double t = (t0 < t1) ? t0 : t1;
return t;
}