Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
golden spiral
int width = 300; // crop window resolution
int height = 550;
int centerX = int(width * 0.72); // precalculated centre of the spiral
int centerY = int(height * 0.72);
float e = 2.71828; // Eulers number
void setup() { // just relevant for Processing
size(width, height);
background(255);
}
void draw() {
float oldx = centerX; // start from the centre
float oldy = centerY;
boolean visible = true; // draw just every second line
// angle from 0 to more than 8*pi means a bit more than 4 complete rounds
// otherwise it would end at the height of centerY - but we need a bit more to fill the window
// set here also the stepwidth for the dashed line
for (float i = 0; i < 25.5 ; i = i + 0.1) {
// calculate the relative distance from center
float length = pow(e, 0.30635 * i); // rule for a Fibonacci spiral
// those magic numbers scale the lenght into any rectangle
// (difficult to explain - just try it out - it works fine with any aspect ratio)
float lenghtX = width * length / 3200;
float lenghtY = height * length / 2000;
// calculate absolute position of pixels based on a Lissajous formula
float x = centerX + (cos(i) * lenghtX * -1);
float y = centerY + (sin(i) * lenghtY );
// draw only visible lines
if (visible) { line(x, y, oldx, oldy); }
// change drawing mode
visible = !visible;
// save last point
oldx = x;
oldy = y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment