Creature Tunnel

Michael Price


Based on: Creature Tunnel by William Kolomyjec, 1977

Category: direct


Description:

This code is running in the browser.






/* 
Part of the ReCode Project (http://recodeproject.com)
Based on "Creature Tunnel" by William Kolomyjec
Originally published in "Computer Graphics and Art" vol2 no3, 1977
Copyright (c) 2012 Michael Price - OSI/MIT license (http://recodeproject/license).
*/

void setup() {
  size(512,512);
}

final float BASE_ANIMAL_SCALE = 100.0;
final float TUNNEL_RADIUS = 50;

void drawAnimal(float[][] pts) {
  beginShape();
  for(int i = 0; i < pts.length; i++) {
    vertex(BASE_ANIMAL_SCALE*pts[i][0] - BASE_ANIMAL_SCALE/2,
           BASE_ANIMAL_SCALE*pts[i][1] - BASE_ANIMAL_SCALE/2); 
  }
  endShape(CLOSE); 
}

void transformToTunnelPoint() {
   float theta, zz, r, dx, dy, tx, ty, tunneldist;
   do {
     theta = random(TWO_PI);
     // Bastardization of perspective transform.
     zz = random(5) + 1.0;
     r = height/2 + TUNNEL_RADIUS;
     dx = r * cos(theta) / zz;
     dy = r * sin(theta) / zz;
     tx = width/2 + dx;
     ty = height/2 + dy;
     tunneldist = sqrt(dx*dx + dy*dy);
     // Rejection sampling.
   } while (tunneldist < TUNNEL_RADIUS);
   
   translate(tx, ty);
   
   float persp = (tunneldist - TUNNEL_RADIUS + 25) / (height/2);
   // Persepective attentuation.
   float angle = atan2(dy, dx);
   rotate(angle);
   scale(min(1.0,persp*persp*persp*2), min(1.0,persp));
   rotate(-angle);
   
   // Random rotation.
   rotate(random(2*PI));
}

void draw() {
  background(0xffffff);
  fill(0,0,0,0);
  for (int i = 0; i < 300; i++) {
    pushMatrix();
     transformToTunnelPoint();
     drawAnimal(ANIMALS[floor(random(ANIMALS.length))]);
    popMatrix(); 
  }
  noLoop();
}

final float[][] GOOSE = 
{{0.53515625,0.14648438},
{0.56640625,0.14648438},
{0.5839844,0.17382812},
{0.5800781,0.19335938},
{0.5644531,0.20507812},
{0.5625,0.22070312},
{0.5644531,0.23632812},
{0.5761719,0.24414062},
{0.5878906,0.27148438},
{0.58984375,0.2890625},
{0.5683594,0.30664062},
{0.5546875,0.3203125},
{0.5546875,0.34375},
{0.5546875,0.37109375},
{0.5703125,0.37695312},
{0.5839844,0.3984375},
{0.5839844,0.41601562},
{0.5839844,0.4375},
{0.60546875,0.44726562},
{0.6308594,0.44335938},
{0.6582031,0.41210938},
{0.7167969,0.38085938},
{0.7734375,0.37695312},
{0.79296875,0.38671875},
{0.82421875,0.40039062},
{0.8652344,0.42382812},
{0.8984375,0.46679688},
{0.9316406,0.49023438},
{0.953125,0.515625},
{0.9589844,0.52734375},
{0.9589844,0.55078125},
{0.9355469,0.5683594},
{0.91015625,0.5703125},
{0.8730469,0.56640625},
{0.84375,0.5566406},
{0.83203125,0.55078125},
{0.81640625,0.5761719},
{0.796875,0.5878906},
{0.79296875,0.58203125},
{0.7753906,0.5527344},
{0.7558594,0.55078125},
{0.71875,0.56640625},
{0.71875,0.57421875},
{0.7050781,0.5878906},
{0.67578125,0.5957031},
{0.65625,0.5703125},
{0.65625,0.5683594},
{0.6308594,0.5839844},
{0.62890625,0.5859375},
{0.6230469,0.6171875},
{0.6269531,0.6542969},
{0.6542969,0.6816406},
{0.66796875,0.7011719},
{0.6777344,0.7246094},
{0.67578125,0.7441406},
{0.65234375,0.7441406},
{0.62890625,0.734375},
{0.60546875,0.7421875},
{0.609375,0.7636719},
{0.6035156,0.796875},
{0.5839844,0.81640625},
{0.5625,0.8300781},
{0.5234375,0.83203125},
{0.49414062,0.8105469},
{0.49414062,0.7832031},
{0.49414062,0.7597656},
{0.47851562,0.7578125},
{0.46484375,0.7792969},
{0.40625,0.7871094},
{0.3984375,0.7714844},
{0.3984375,0.7480469},
{0.41992188,0.7050781},
{0.43359375,0.6816406},
{0.47070312,0.65234375},
{0.48046875,0.6425781},
{0.46289062,0.5957031},
{0.453125,0.5859375},
{0.42382812,0.5859375},
{0.39257812,0.609375},
{0.3671875,0.59375},
{0.328125,0.59375},
{0.3125,0.6074219},
{0.3046875,0.625},
{0.2734375,0.63671875},
{0.25585938,0.63671875},
{0.22265625,0.6171875},
{0.19335938,0.6113281},
{0.171875,0.62890625},
{0.15234375,0.6464844},
{0.140625,0.6640625},
{0.115234375,0.6425781},
{0.091796875,0.62890625},
{0.068359375,0.625},
{0.046875,0.60546875},
{0.0625,0.5917969},
{0.103515625,0.5449219},
{0.14257812,0.5136719},
{0.19335938,0.45507812},
{0.28320312,0.39453125},
{0.34375,0.390625},
{0.39257812,0.40820312},
{0.42773438,0.43945312},
{0.45898438,0.45507812},
{0.48828125,0.46679688},
{0.5019531,0.46679688},
{0.50390625,0.46679688},
{0.5097656,0.4453125},
{0.5097656,0.421875},
{0.5097656,0.37695312},
{0.51171875,0.34960938},
{0.5292969,0.33398438},
{0.5175781,0.31835938},
{0.5019531,0.30078125},
{0.49414062,0.2734375},
{0.49804688,0.24414062},
{0.5097656,0.234375},
{0.51953125,0.22265625},
{0.51953125,0.203125},
{0.51171875,0.1953125},
{0.5058594,0.16601562},
{0.5058594,0.14648438},
{0.51953125,0.1328125},
{0.5390625,0.12695312},
{0.5488281,0.12695312}};

final float[][] BEAVER = 
{{0.45117188,0.037109375},
{0.49414062,0.048828125},
{0.5527344,0.107421875},
{0.5839844,0.15429688},
{0.5839844,0.171875},
{0.6074219,0.16796875},
{0.6425781,0.16796875},
{0.66796875,0.13867188},
{0.6777344,0.16015625},
{0.6972656,0.17382812},
{0.6738281,0.18359375},
{0.6953125,0.203125},
{0.6699219,0.20898438},
{0.6542969,0.203125},
{0.6074219,0.2421875},
{0.6171875,0.27929688},
{0.63671875,0.29296875},
{0.6464844,0.3046875},
{0.6542969,0.3359375},
{0.640625,0.39453125},
{0.640625,0.4140625},
{0.7109375,0.44335938},
{0.7480469,0.41796875},
{0.7421875,0.43945312},
{0.7597656,0.45117188},
{0.7441406,0.45898438},
{0.7636719,0.47460938},
{0.7441406,0.48242188},
{0.7402344,0.50390625},
{0.7050781,0.4765625},
{0.66796875,0.46289062},
{0.6171875,0.47265625},
{0.59765625,0.48828125},
{0.5859375,0.49804688},
{0.56640625,0.5058594},
{0.5605469,0.5234375},
{0.56640625,0.5546875},
{0.5996094,0.6386719},
{0.62109375,0.703125},
{0.66796875,0.77734375},
{0.6777344,0.8105469},
{0.63671875,0.9042969},
{0.5546875,0.9511719},
{0.49609375,0.9511719},
{0.453125,0.921875},
{0.421875,0.8671875},
{0.40625,0.8203125},
{0.40820312,0.7949219},
{0.41992188,0.7578125},
{0.42382812,0.7011719},
{0.43554688,0.65234375},
{0.44335938,0.6269531},
{0.45117188,0.5859375},
{0.45898438,0.55078125},
{0.46289062,0.5234375},
{0.45703125,0.5078125},
{0.41992188,0.48828125},
{0.38867188,0.484375},
{0.34765625,0.49023438},
{0.32421875,0.49804688},
{0.30273438,0.5},
{0.29101562,0.51171875},
{0.28125,0.53125},
{0.25585938,0.5214844},
{0.25390625,0.50390625},
{0.24023438,0.4921875},
{0.23632812,0.47851562},
{0.25390625,0.4765625},
{0.27148438,0.47070312},
{0.29101562,0.4765625},
{0.30859375,0.46679688},
{0.33007812,0.45117188},
{0.34765625,0.4296875},
{0.34179688,0.40625},
{0.33398438,0.37695312},
{0.33398438,0.33984375},
{0.33398438,0.3046875},
{0.33984375,0.2734375},
{0.34179688,0.24804688},
{0.33007812,0.234375},
{0.30859375,0.2265625},
{0.28710938,0.21679688},
{0.27539062,0.21875},
{0.25,0.22460938},
{0.265625,0.2109375},
{0.265625,0.18945312},
{0.26757812,0.17578125},
{0.26953125,0.1640625},
{0.2890625,0.16992188},
{0.28710938,0.18945312},
{0.3046875,0.1953125},
{0.3203125,0.1953125},
{0.34375,0.19140625},
{0.36914062,0.18164062},
{0.38085938,0.1875},
{0.36914062,0.16992188},
{0.3671875,0.15820312},
{0.36914062,0.140625},
{0.38085938,0.123046875},
{0.40429688,0.109375},
{0.41601562,0.091796875},
{0.43164062,0.064453125},
{0.44921875,0.05078125},
{0.46679688,0.044921875},
{0.49414062,0.05859375},
{0.5097656,0.07421875}};

final float[][] FROG =
{{0.5292969,0.03125},
{0.5625,0.037109375},
{0.6308594,0.0859375},
{0.6660156,0.15625},
{0.640625,0.125},
{0.609375,0.123046875},
{0.56640625,0.15820312},
{0.5722656,0.1953125},
{0.6035156,0.22460938},
{0.6347656,0.22851562},
{0.6660156,0.18945312},
{0.6933594,0.2421875},
{0.7519531,0.23632812},
{0.7910156,0.18554688},
{0.796875,0.24023438},
{0.83203125,0.23046875},
{0.8066406,0.265625},
{0.8378906,0.3046875},
{0.7890625,0.28710938},
{0.79296875,0.31445312},
{0.76171875,0.265625},
{0.6933594,0.296875},
{0.6152344,0.5058594},
{0.7441406,0.58984375},
{0.7597656,0.7597656},
{0.6230469,0.7890625},
{0.7167969,0.8671875},
{0.6699219,0.8886719},
{0.6699219,0.9277344},
{0.64453125,0.9355469},
{0.60546875,0.90625},
{0.5917969,0.9355469},
{0.5527344,0.7714844},
{0.5761719,0.7480469},
{0.65234375,0.7050781},
{0.5878906,0.6875},
{0.5214844,0.6386719},
{0.50390625,0.55078125},
{0.47851562,0.62109375},
{0.45507812,0.6621094},
{0.33984375,0.68359375},
{0.33789062,0.69140625},
{0.4296875,0.765625},
{0.38085938,0.9238281},
{0.375,0.91015625},
{0.36523438,0.9003906},
{0.3046875,0.9121094},
{0.30078125,0.8691406},
{0.27734375,0.8515625},
{0.26953125,0.8515625},
{0.2578125,0.83203125},
{0.36132812,0.78125},
{0.34179688,0.7636719},
{0.22851562,0.7363281},
{0.25195312,0.58203125},
{0.27539062,0.5449219},
{0.3984375,0.49414062},
{0.36328125,0.28710938},
{0.29101562,0.24609375},
{0.25390625,0.28320312},
{0.24804688,0.27148438},
{0.23242188,0.26367188},
{0.1875,0.2734375},
{0.22070312,0.24023438},
{0.21484375,0.20898438},
{0.24023438,0.203125},
{0.25585938,0.15820312},
{0.27148438,0.18359375},
{0.28710938,0.20703125},
{0.34570312,0.22265625},
{0.37890625,0.1640625},
{0.38867188,0.18554688},
{0.40820312,0.22070312},
{0.44335938,0.23046875},
{0.4609375,0.21679688},
{0.47460938,0.18164062},
{0.48046875,0.15625},
{0.4609375,0.12890625},
{0.4375,0.12109375},
{0.40429688,0.115234375},
{0.38671875,0.13867188},
{0.41015625,0.0859375},
{0.45898438,0.056640625},
{0.49804688,0.0390625},
{0.5390625,0.03125}};

final float[][] ECHIDNA =
{{0.50390625,0.046875},
{0.47851562,0.072265625},
{0.47070312,0.099609375},
{0.484375,0.20703125},
{0.48632812,0.22851562},
{0.45898438,0.23828125},
{0.43945312,0.26367188},
{0.42773438,0.2890625},
{0.42578125,0.31835938},
{0.44335938,0.34765625},
{0.46484375,0.359375},
{0.4765625,0.375},
{0.47265625,0.38476562},
{0.44726562,0.359375},
{0.39648438,0.32421875},
{0.37304688,0.31054688},
{0.34375,0.28710938},
{0.31640625,0.29296875},
{0.30273438,0.30859375},
{0.2734375,0.33789062},
{0.26757812,0.37109375},
{0.27148438,0.4140625},
{0.29101562,0.43945312},
{0.31054688,0.44335938},
{0.33984375,0.45703125},
{0.37109375,0.45898438},
{0.39257812,0.45703125},
{0.328125,0.48046875},
{0.28710938,0.49414062},
{0.25585938,0.5644531},
{0.23046875,0.62890625},
{0.23242188,0.6816406},
{0.25,0.75},
{0.29882812,0.7890625},
{0.3125,0.8105469},
{0.30664062,0.8457031},
{0.31054688,0.8925781},
{0.33789062,0.9199219},
{0.41796875,0.8769531},
{0.44921875,0.88671875},
{0.48828125,0.9277344},
{0.50390625,0.9667969},
{0.5332031,0.9140625},
{0.5957031,0.890625},
{0.6386719,0.88671875},
{0.66796875,0.9121094},
{0.6894531,0.8886719},
{0.7050781,0.8613281},
{0.6953125,0.8183594},
{0.76171875,0.75},
{0.765625,0.7109375},
{0.765625,0.6503906},
{0.7265625,0.5605469},
{0.6875,0.51953125},
{0.6171875,0.49414062},
{0.67578125,0.48046875},
{0.71484375,0.48046875},
{0.73828125,0.44726562},
{0.7324219,0.39257812},
{0.7011719,0.35351562},
{0.63671875,0.3515625},
{0.57421875,0.3515625},
{0.5390625,0.38671875},
{0.5683594,0.33789062},
{0.59765625,0.32617188},
{0.6074219,0.30664062},
{0.58203125,0.26953125},
{0.55078125,0.24023438},
{0.5390625,0.21875},
{0.53515625,0.16796875},
{0.5371094,0.13671875},
{0.5371094,0.095703125},
{0.51953125,0.0546875},
{0.50390625,0.046875}};

final float[][][] ANIMALS = {GOOSE, FROG, BEAVER, ECHIDNA};