Last active
May 3, 2023 09:56
-
-
Save robot-dreams/b6e6196e671053917ec4b848c40e805b to your computer and use it in GitHub Desktop.
Draw a golden spiral with Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float PHI = (1 + sqrt(5)) / 2; | |
void goldenSpiral(float h) { | |
// Base case: stop drawing if the height is too small. | |
if (h < 2) { | |
return; | |
} | |
// Draw bounding box and quarter circle. For some reason, using 2 * h - 1 | |
// looks better than using 2 * h. | |
rect(0, 0, h, h); | |
arc(h, h, 2 * h - 1, 2 * h - 1, PI, PI + HALF_PI); | |
// Reposition canvas for next iteration. | |
translate(h * PHI, 0); | |
rotate(HALF_PI); | |
// Perform recursive call (with height scaled down by golden ratio). | |
goldenSpiral(h / PHI); | |
} | |
void setup() { | |
// 500 * PHI is approximately 809. We add an additional 100 to the width | |
// and height to account for margins. | |
size(909, 600); | |
background(255); // Set a white background. | |
translate(50, 50); // Add margins. | |
noFill(); // Draw outlines only. | |
goldenSpiral(500); | |
save("goldenSpiral.png"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was inspired by https://thirdfoundation.github.io/#/art/pyramids