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 );

String pi = "314159265358979323846264338327950288419716939937510
void draw()
  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 ) {


Ruben Berenguel said...

This is... odd. But I share your passion for doing odd programs that you think no-one else may find interesting. I found it interesting ;)



Anonymous said...

Odd. In reverse it would be an accurate simile of DNA wrapping around histones in eukaryotes.

Most apt.

Francis Turner said...

Not only odd and interesting but a whole new language to play with. As I'm currently struggling with some data visualization issues and have a long plane ride to Japan on Monday this looks very very useful

I'm sorry you shut down usethesource by the way. It was an interesting place until the spammers discovered it.