Skip to content

Commit ca843b1

Browse files
committed
nextWeek: update moving_sphere.h
Improvements we made to sphere.h didn't make it into moving_sphere.h. Fixing that here.
1 parent b23bb9e commit ca843b1

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@
206206
auto a = r.direction().length_squared();
207207
auto half_b = dot(oc, r.direction());
208208
auto c = oc.length_squared() - radius*radius;
209+
209210
auto discriminant = half_b*half_b - a*c;
210211

211212
if (discriminant > 0) {
212213
auto root = sqrt(discriminant);
214+
213215
auto temp = (-half_b - root)/a;
214216
if (temp < t_max && temp > t_min) {
215217
rec.t = temp;
@@ -221,6 +223,7 @@
221223
rec.mat_ptr = mat_ptr;
222224
return true;
223225
}
226+
224227
temp = (-half_b + root) / a;
225228
if (temp < t_max && temp > t_min) {
226229
rec.t = temp;

src/TheNextWeek/moving_sphere.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ bool moving_sphere::bounding_box(double t0, double t1, aabb& output_box) const {
5656
// replace "center" with "center(r.time())"
5757
bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
5858
vec3 oc = r.origin() - center(r.time());
59-
auto a = dot(r.direction(), r.direction());
60-
auto b = dot(oc, r.direction());
61-
auto c = dot(oc, oc) - radius*radius;
59+
auto a = r.direction().length_squared();
60+
auto half_b = dot(oc, r.direction());
61+
auto c = oc.length_squared() - radius*radius;
6262

63-
auto discriminant = b*b - a*c;
63+
auto discriminant = half_b*half_b - a*c;
6464

6565
if (discriminant > 0) {
66+
auto root = sqrt(discriminant);
6667

67-
auto temp = (-b - sqrt(discriminant))/a;
68+
auto temp = (-half_b - root)/a;
6869
if (temp < t_max && temp > t_min) {
6970
rec.t = temp;
7071
rec.p = r.at(rec.t);
@@ -74,7 +75,7 @@ bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& re
7475
return true;
7576
}
7677

77-
temp = (-b + sqrt(discriminant))/a;
78+
temp = (-half_b + root)/a;
7879
if (temp < t_max && temp > t_min) {
7980
rec.t = temp;
8081
rec.p = r.at(rec.t);

src/TheNextWeek/sphere.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class sphere: public hittable {
2323
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
2424
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
2525

26+
public:
2627
vec3 center;
2728
double radius;
2829
shared_ptr<material> mat_ptr;
@@ -41,6 +42,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
4142
auto a = r.direction().length_squared();
4243
auto half_b = dot(oc, r.direction());
4344
auto c = oc.length_squared() - radius*radius;
45+
4446
auto discriminant = half_b*half_b - a*c;
4547

4648
if (discriminant > 0) {
@@ -68,6 +70,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
6870
return true;
6971
}
7072
}
73+
7174
return false;
7275
}
7376

0 commit comments

Comments
 (0)