Skip to content

Commit d160727

Browse files
committed
books: sanity pass on code blocks
Resolves #14
1 parent 4febda9 commit d160727

File tree

3 files changed

+149
-161
lines changed

3 files changed

+149
-161
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@
164164
Here’s the top part of my vec3 class:
165165

166166
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
167+
#include <iostream>
167168
#include <math.h>
168169
#include <stdlib.h>
169-
#include <iostream>
170170

171171
class vec3 {
172172
public:
@@ -232,24 +232,26 @@
232232
return vec3(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1], v1.e[2] * v2.e[2]);
233233
}
234234

235-
inline vec3 operator/(const vec3 &v1, const vec3 &v2) {
236-
return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]);
235+
inline vec3 operator*(float t, const vec3 &v) {
236+
return vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
237237
}
238238

239-
inline vec3 operator*(float t, const vec3 &v) {
239+
inline vec3 operator*(const vec3 &v, float t) {
240240
return vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
241241
}
242242

243-
inline vec3 operator/(vec3 v, float t) {
244-
return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t);
243+
inline vec3 operator/(const vec3 &v1, const vec3 &v2) {
244+
return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]);
245245
}
246246

247-
inline vec3 operator*(const vec3 &v, float t) {
248-
return vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
247+
inline vec3 operator/(vec3 v, float t) {
248+
return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t);
249249
}
250250

251251
inline float dot(const vec3 &v1, const vec3 &v2) {
252-
return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2];
252+
return v1.e[0]*v2.e[0]
253+
+ v1.e[1]*v2.e[1]
254+
+ v1.e[2]*v2.e[2];
253255
}
254256

255257
inline vec3 cross(const vec3 &v1, const vec3 &v2) {
@@ -258,47 +260,47 @@
258260
v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]);
259261
}
260262

261-
inline vec3& vec3::operator+=(const vec3 &v){
262-
e[0] += v.e[0];
263-
e[1] += v.e[1];
264-
e[2] += v.e[2];
263+
inline vec3& vec3::operator+=(const vec3 &v) {
264+
e[0] += v.e[0];
265+
e[1] += v.e[1];
266+
e[2] += v.e[2];
265267
return *this;
266268
}
267269

268-
inline vec3& vec3::operator*=(const vec3 &v){
269-
e[0] *= v.e[0];
270-
e[1] *= v.e[1];
271-
e[2] *= v.e[2];
270+
inline vec3& vec3::operator-=(const vec3& v) {
271+
e[0] -= v.e[0];
272+
e[1] -= v.e[1];
273+
e[2] -= v.e[2];
272274
return *this;
273275
}
274276

275-
inline vec3& vec3::operator/=(const vec3 &v){
276-
e[0] /= v.e[0];
277-
e[1] /= v.e[1];
278-
e[2] /= v.e[2];
277+
inline vec3& vec3::operator*=(const vec3 &v) {
278+
e[0] *= v.e[0];
279+
e[1] *= v.e[1];
280+
e[2] *= v.e[2];
279281
return *this;
280282
}
281283

282-
inline vec3& vec3::operator-=(const vec3& v) {
283-
e[0] -= v.e[0];
284-
e[1] -= v.e[1];
285-
e[2] -= v.e[2];
284+
inline vec3& vec3::operator*=(const float t) {
285+
e[0] *= t;
286+
e[1] *= t;
287+
e[2] *= t;
286288
return *this;
287289
}
288290

289-
inline vec3& vec3::operator*=(const float t) {
290-
e[0] *= t;
291-
e[1] *= t;
292-
e[2] *= t;
291+
inline vec3& vec3::operator/=(const vec3 &v) {
292+
e[0] /= v.e[0];
293+
e[1] /= v.e[1];
294+
e[2] /= v.e[2];
293295
return *this;
294296
}
295297

296298
inline vec3& vec3::operator/=(const float t) {
297299
float k = 1.0/t;
298300

299-
e[0] *= k;
300-
e[1] *= k;
301-
e[2] *= k;
301+
e[0] *= k;
302+
e[1] *= k;
303+
e[2] *= k;
302304
return *this;
303305
}
304306

@@ -311,13 +313,15 @@
311313
Now we can change our main to use this:
312314

313315
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
314-
#include <iostream>
315316
#include "vec3.h"
316317

318+
#include <iostream>
319+
317320
int main() {
318321
int nx = 200;
319322
int ny = 100;
320323
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
324+
321325
for (int j = ny-1; j >= 0; j--) {
322326
for (int i = 0; i < nx; i++) {
323327
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -395,10 +399,11 @@
395399
for now because we’ll add antialiasing later):
396400

397401
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
398-
#include <iostream>
399402
#include "ray.h"
400403

401-
vec3 color(const ray& r, hittable *world, int depth) {
404+
#include <iostream>
405+
406+
vec3 color(const ray& r) {
402407
vec3 unit_direction = unit_vector(r.direction());
403408
float t = 0.5*(unit_direction.y() + 1.0);
404409
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
@@ -636,8 +641,7 @@
636641

637642
#include "ray.h"
638643

639-
struct hit_record
640-
{
644+
struct hit_record {
641645
float t;
642646
vec3 p;
643647
vec3 normal;
@@ -711,7 +715,7 @@
711715

712716
#include "hittable.h"
713717

714-
class hittable_list: public hittable {
718+
class hittable_list: public hittable {
715719
public:
716720
hittable_list() {}
717721
hittable_list(hittable **l, int n) {list = l; list_size = n; }
@@ -721,8 +725,8 @@
721725
int list_size;
722726
};
723727

724-
bool hittable_list::hit(
725-
const ray& r, float t_min, float t_max, hit_record& rec) const {
728+
bool hittable_list::hit(const ray& r, float t_min, float t_max,
729+
hit_record& rec) const {
726730

727731
hit_record temp_rec;
728732
bool hit_anything = false;
@@ -745,10 +749,11 @@
745749
And the new main:
746750

747751
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
748-
#include <iostream>
749-
#include "sphere.h"
750-
#include "hittable_list.h"
751752
#include "float.h"
753+
#include "hittable_list.h"
754+
#include "sphere.h"
755+
756+
#include <iostream>
752757

753758

754759
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -937,7 +942,7 @@
937942
for (int i = 0; i < nx; i++) {
938943
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
939944
vec3 col(0, 0, 0);
940-
for (int s=0; s < ns; s++) {
945+
for (int s = 0; s < ns; s++) {
941946
float u = float(i + random_double()) / float(nx);
942947
float v = float(j + random_double()) / float(ny);
943948
ray r = cam.get_ray(u, v);
@@ -1022,7 +1027,7 @@
10221027
Then update the `color()` function to use the new random direction generator:
10231028

10241029
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1025-
vec3 color(const ray& r, hittable *world, int depth) {
1030+
vec3 color(const ray& r, hittable *world) {
10261031
hit_record rec;
10271032
if (world->hit(r, 0.0, MAXFLOAT, rec)) {
10281033
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1125,8 +1130,7 @@
11251130

11261131
class material;
11271132

1128-
struct hit_record
1129-
{
1133+
struct hit_record {
11301134
float t;
11311135
vec3 p;
11321136
vec3 normal;
@@ -1205,12 +1209,11 @@
12051209
public:
12061210
lambertian(const vec3& a) : albedo(a) {}
12071211
virtual bool scatter(const ray& r_in, const hit_record& rec,
1208-
vec3& attenuation, ray& scattered) const
1209-
{
1210-
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
1211-
scattered = ray(rec.p, target-rec.p);
1212-
attenuation = albedo;
1213-
return true;
1212+
vec3& attenuation, ray& scattered) const {
1213+
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
1214+
scattered = ray(rec.p, target-rec.p);
1215+
attenuation = albedo;
1216+
return true;
12141217
}
12151218

12161219
vec3 albedo;
@@ -1249,8 +1252,7 @@
12491252
public:
12501253
metal(const vec3& a) : albedo(a) {}
12511254
virtual bool scatter(const ray& r_in, const hit_record& rec,
1252-
vec3& attenuation, ray& scattered) const
1253-
{
1255+
vec3& attenuation, ray& scattered) const {
12541256
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
12551257
scattered = ray(rec.p, reflected);
12561258
attenuation = albedo;
@@ -1302,9 +1304,9 @@
13021304

13031305
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
13041306
hittable *list[4];
1305-
list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5)));
1307+
list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.8, 0.3, 0.3)));
13061308
list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
1307-
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0));
1309+
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2)));
13081310
list[3] = new sphere(vec3(-1,0,-1), 0.5, new metal(vec3(0.8, 0.8, 0.8)));
13091311
hittable *world = new hittable_list(list,4);
13101312
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1313,11 +1315,11 @@
13131315
for (int j = ny-1; j >= 0; j--) {
13141316
for (int i = 0; i < nx; i++) {
13151317
vec3 col(0, 0, 0);
1316-
for (int s=0; s < ns; s++) {
1318+
for (int s = 0; s < ns; s++) {
13171319
float u = float(i + random_double()) / float(nx);
13181320
float v = float(j + random_double()) / float(ny);
13191321
ray r = cam.get_ray(u, v);
1320-
col += color(r, world,0);
1322+
col += color(r, world, 0);
13211323
}
13221324
col /= float(ns);
13231325
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1366,8 +1368,7 @@
13661368
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13671369

13681370
virtual bool scatter(const ray& r_in, const hit_record& rec,
1369-
vec3& attenuation, ray& scattered) const
1370-
{
1371+
vec3& attenuation, ray& scattered) const {
13711372
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
13721373
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere());
13731374
attenuation = albedo;
@@ -1452,12 +1453,8 @@
14521453
class dielectric : public material {
14531454
public:
14541455
dielectric(float ri) : ref_idx(ri) {}
1455-
virtual bool scatter(
1456-
const ray& r_in,
1457-
const hit_record& rec,
1458-
vec3& attenuation,
1459-
ray& scattered) const
1460-
{
1456+
virtual bool scatter(const ray& r_in, const hit_record& rec,
1457+
vec3& attenuation, ray& scattered) const {
14611458
vec3 outward_normal;
14621459
vec3 reflected = reflect(r_in.direction(), rec.normal);
14631460
float ni_over_nt;
@@ -1499,7 +1496,7 @@
14991496
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15001497
list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5)));
15011498
list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
1502-
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0));
1499+
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2)));
15031500
list[3] = new sphere(vec3(-1,0,-1), 0.5, new dielectric(1.5));
15041501
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15051502

@@ -1536,12 +1533,8 @@
15361533
class dielectric : public material {
15371534
public:
15381535
dielectric(float ri) : ref_idx(ri) {}
1539-
virtual bool scatter(
1540-
const ray& r_in,
1541-
const hit_record& rec,
1542-
vec3& attenuation,
1543-
ray& scattered) const
1544-
{
1536+
virtual bool scatter(const ray& r_in, const hit_record& rec,
1537+
vec3& attenuation, ray& scattered) const {
15451538
vec3 outward_normal;
15461539
vec3 reflected = reflect(r_in.direction(), rec.normal);
15471540
float ni_over_nt;
@@ -1726,7 +1719,7 @@
17261719
float half_height = tan(theta/2);
17271720
float half_width = aspect * half_height;
17281721
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1729-
origin = lookatfrom;
1722+
origin = lookfrom;
17301723
w = unit_vector(lookfrom - lookat);
17311724
u = unit_vector(cross(vup, w));
17321725
v = cross(w, u);
@@ -1821,8 +1814,7 @@
18211814
public:
18221815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
18231816
camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect,
1824-
float aperture, float focus_dist)
1825-
{
1817+
float aperture, float focus_dist) {
18261818
lens_radius = aperture / 2;
18271819
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
18281820
float theta = vfov*M_PI/180;

0 commit comments

Comments
 (0)