We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Fibonnaci tile set and spiral
Page Index Toggle Pages: 1
Fibonnaci tile set and spiral (Read 971 times)
Fibonnaci tile set and spiral
Feb 15th, 2009, 12:22am
 
I couldn't find one, so I made a short script to generate a fibonnaci tile set and corresponding spiral. For some reason I find them aesthetically pleasing, so I'll see if I can render them in a more interesting way later on.
As for the implementation, it's probably terribly clunky and mathmatically inelegant, so please feel free to tell me how's it's really done.
Thanks,
Thor.

void setup() {
size(1000, 1000);
smooth();
noFill();
background(255);
stroke(0, 100);
drawFibonnaciTiles(500);
}


void drawFibonnaciTiles(int iterations)
{
 rect(width/2, height/2, 1, 1);
 int posx = width/2;
 int posy = height/2;
 int fibprev = 1;
 int fibnew = 0;
 for (int fib = 1; fib < iterations;) {
   for (int a = 0; a < 4; a++) {
     if (a == 0) {
     fibnew = fib + fibprev;
     fibprev = fib;
     fib = fibnew;
     arc(posx + fib, posy + fib, fib*2, fib*2, PI, TWO_PI-PI/2);
     rect(posx, posy, fib, fib);
     posx = posx + fib;
     }
     if (a == 1) {
     fibnew = fib + fibprev;
     fibprev = fib;
     fib = fibnew;
     arc(posx, posy + fib, fib*2, fib*2, TWO_PI-PI/2, TWO_PI);
     rect(posx, posy, fib, fib);
     posx = posx - fibprev;
     posy = posy + fib;
     }
     if (a == 2) {
     fibnew = fib + fibprev;
     fibprev = fib;
     fib = fibnew;
     arc(posx, posy, fib*2, fib*2, 0, PI/2);
     rect(posx, posy, fib, fib);
     posx = posx - fib - fibprev;
     posy = posy - fibprev;
     }
     if (a == 3) {
     fibnew = fib + fibprev;
     fibprev = fib;
     fib = fibnew;
     arc(posx + fib, posy, fib*2, fib*2, PI/2, PI);
     rect(posx, posy, fib, fib);
     posy = posy - fib - fibprev;
     }
   }
}
}

Re: Fibonnaci tile set and spiral
Reply #1 - Feb 15th, 2009, 9:38am
 
Nice effect indeed.
Might I suggest some programming improvements -- not on the maths themselves, but on the form.

The part:

      fibnew = fib + fibprev;
      fibprev = fib;
      fib = fibnew;

will gain to be put just after the loop, before the ifs, because it is independent of the value of a.
And although it won't make a difference, conceptually, when I see cascaded tests like that, I put the followers after an else: no need to test further possible values of a once you found one.
Actually, I would have used a switch there, but the gain is minor here.
Code:
  for (int fib = 1; fib < iterations;) {
   for (int a = 0; a < 4; a++) {
     fibnew = fib + fibprev;
     fibprev = fib;
     fib = fibnew;
rect(posx, posy, fib, fib);
     if (a == 0) {
       arc(posx + fib, posy + fib, fib*2, fib*2, PI, TWO_PI-PI/2);
       posx = posx + fib;
     } else if (a == 1) {
       arc(posx, posy + fib, fib*2, fib*2, TWO_PI-PI/2, TWO_PI);
       posx = posx - fibprev;
       posy = posy + fib;
     } else if (a == 2) {
       arc(posx, posy, fib*2, fib*2, 0, PI/2);
       rect(posx, posy, fib, fib);
       posx = posx - fib - fibprev;
       posy = posy - fibprev;
     } else if (a == 3) {
       arc(posx + fib, posy, fib*2, fib*2, PI/2, PI);
       posy = posy - fib - fibprev;
     }
   }
 }
Re: Fibonnaci tile set and spiral
Reply #2 - Feb 15th, 2009, 10:07am
 
Ah, yes. Always, always avoid redundancy Smiley Thanks for the suggestion. I'm a fool with scripting, so it's a miracle I got it working at all.
Re: Fibonnaci tile set and spiral
Reply #3 - Feb 15th, 2009, 10:22am
 
Yes, if possible, avoiding redundancy makes programs simpler to maintain (editing one part and forgetting the others...), more readable (less to read), is better to show structure.

Actually, I edited above, because I found that I can pull out the rect() call too (untested, should be safe).
Re: Fibonnaci tile set and spiral
Reply #4 - Feb 20th, 2009, 5:10pm
 
Cool!
Page Index Toggle Pages: 1