Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Book 2, Chapter 8.2] Use range-based for-loop #1482

Open
dimitry-ishenko opened this issue Mar 29, 2024 · 3 comments
Open

[Book 2, Chapter 8.2] Use range-based for-loop #1482

dimitry-ishenko opened this issue Mar 29, 2024 · 3 comments
Assignees
Milestone

Comments

@dimitry-ishenko
Copy link

dimitry-ishenko commented Mar 29, 2024

In Listing 70, instead of using math tricks to permute between min/max members of bbox, use range-based for-loop that was introduced in C++11. This improves readability and allows people to understand what is actually being done in the loop.

         point3 min( infinity,  infinity,  infinity);
         point3 max(-infinity, -infinity, -infinity);
 
-        for (int i = 0; i < 2; i++) {
-            for (int j = 0; j < 2; j++) {
-                for (int k = 0; k < 2; k++) {
-                    auto x = i*bbox.x.max + (1-i)*bbox.x.min;
-                    auto y = j*bbox.y.max + (1-j)*bbox.y.min;
-                    auto z = k*bbox.z.max + (1-k)*bbox.z.min;
-
-                    auto newx =  cos_theta*x + sin_theta*z;
-                    auto newz = -sin_theta*x + cos_theta*z;
-
-                    vec3 tester(newx, y, newz);
+        for (auto x : { bbox.x.min, bbox.x.max }) {
+            for (auto y : { bbox.y.min, bbox.y.max }) {
+                for (auto z : { bbox.z.min, bbox.z.max }) {
+
+                    vec3 tester {
+                         cos_theta*x + sin_theta*z,
+                         y,
+                        -sin_theta*x + cos_theta*z
+                    };
 
                     for (int c = 0; c < 3; c++) {
                         min[c] = fmin(min[c], tester[c]);```
@hollasch
Copy link
Collaborator

Thanks Dimitry.

@hollasch hollasch self-assigned this Apr 2, 2024
@hollasch hollasch added this to the Backlog milestone Apr 2, 2024
@hollasch
Copy link
Collaborator

hollasch commented Apr 4, 2024

Ref #819

@hollasch
Copy link
Collaborator

hollasch commented Apr 6, 2024

Note the echo of this approach in perlin::perlin_interp().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants