int n=20; // any values here ~30 and over will take too long to run and be seen as infinite loop
int[] fib = new int[n];
int RIGHT = 0;
int UP = 1;
int LEFT = 2;
int DOWN = 3;
// initialized for every spiral
int direction, b;
float x, y;
PImage bg = loadImage("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTSCZec0R3WXp0ksCLw8ykZKzcNSILxa9hq6p_czrE88xtxl3EncRXv6Orqgx1K-WVN0aQ&usqp=CAU");
void setup() {
size(1000, 1000);
colorMode(HSB);
strokeWeight(3);
// fill the array with fibonacci numbers
fib[0] = 1;
fib[1] = 1;
for(int i=2; i<n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
}
void draw() {
background(255);
image(bg, 0, 0, width, height);
drawSpiral();
// show the ratio on the screen
fill(0);
float ratio = (float)fib[n-1] / fib[n-2];
textSize(40);
text("Golden Ratio: " + nf(ratio, 0, 6), 100, 150);
}
void drawSpiral() {
x = width/2;
y = width/2;
// uncomment this line to draw a static image (no motion)
// direction = 2;
b = 0;
stroke(255);
// draw the spiral
for(int i=0; i<n; i++) {
fill(128, 200, b, 50);
stroke(0);
rect(x, y, fib[i], fib[i]);
drawArc(i, direction);
// change start point for next rectangle
if(i > 0) {
if(direction == RIGHT) {
x = x + fib[i];
} else if(direction == UP) {
x = x - fib[i-1];
y = y + fib[i];
} else if(direction == LEFT) {
x = x - fib[i] - fib[i-1];
y = y - fib[i-1];
} else {
y = y - fib[i] - fib[i-1];
}
direction = (direction + 1) % 4;
}
b += 15; b %= 256;
}
}
void drawArc(int i, int direction) {
if(direction == 0) {
arc(x + fib[i], y + fib[i], fib[i]*2, fib[i]*2, PI, TWO_PI-PI/2);
} else if(direction == 1) {
arc(x, y + fib[i], fib[i]*2, fib[i]*2, TWO_PI-PI/2, TWO_PI);
} else if(direction == 2) {
arc(x, y, fib[i]*2, fib[i]*2, 0, PI/2);
} else {
arc(x + fib[i], y, fib[i]*2, fib[i]*2, PI/2, PI);
}
}