Tuesday, March 22, 2011

The Piral

Do you ever think to yourself, "If I took the first 400 digits of pi and drew lines proportional to each of the digits with a fixed angle between each line what it would look like? And then if I change the angle what an animation of that would look like?" Probably not, but I did on the bus home tonight and so with a bit of Processing here's a little animation that I'm dubbing "The Piral".

It starts with the angle between segments as 90 degrees and works its way up to a straight line. The length of each little segment is proportional to the digit of pi (i.e. 3x, 1x, 4x, 1x, 5x, etc.). As pi swirls around it sometimes stretches itself out, and sometimes bunches together, ultimately it spirals ever larger until it becomes a line.



Strangely pleasing to watch.

PS Before you ask. Here's the code
int w = 1024;
int h = 768;

float ad = 90;

void setup()
{
  size( w, h );
  strokeWeight(2);
  background(255);
  frameRate(30);
}

String pi = "314159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679821
48086513282306647093844609550582231725359408128481117
45028410270193852110555964462294895493038196442881097
56659334461284756482337867831652712019091456485669234
60348610454326648213393607260249141273724587006606315
58817488152092096282925409171536436789259036001133053
0548820466521384146951941511609";
  
void draw()
{
  background(255);
  float cx = w / 2;
  float cy = h / 2;
  float angle = 0;
  
  for ( int i = 0; i < pi.length(); ++i ) {
     float d = float(pi.substring(i,i+1))+1;
     float ex = cx + d * cos(radians(angle)) * 3;
     float ey = cy + d * sin(radians(angle)) * 3;
     line( cx, cy, ex, ey );
     
     stroke(255/d, 25*d, 64/d);
     angle += ad;
     if ( angle >= 360 ) {
       angle -= 360;
     }
     cx = ex;
     cy = ey;
  }
  
  ad -= 0.06;
  if ( ad < 0 ) {
    noLoop();
  }
}

Labels:

If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available.

<$BlogCommentBody$>

<$BlogCommentDateTime$> <$BlogCommentDeleteIcon$>

Post a Comment

Links to this post:

<$BlogBacklinkControl$> <$BlogBacklinkTitle$> <$BlogBacklinkDeleteIcon$>
<$BlogBacklinkSnippet$>
Create a Link

<< Home