Turtles - a triangle life form


int     MAX     = 15;
float   DIVISOR = 10.0;
ArrayList lstBase = new ArrayList();

void setup() {
  size(800, 600);  
  stroke (0);        
  strokeWeight(2); 
  background (255);  
  frameRate (100);
    
  lstBase.add(new TTurtel (400, 300, 0.0));
  for (int i=0; i <= MAX-1; i++) 
    lstBase.add(new TTurtel (400, 5*(i+5), 0.0));
  }

void drawTriangle(int iLevel, TTurtel TurtelBase) {

  TTurtel Turtel = new TTurtel(400.0, 300.0, 0.0);
  
  TPunkt P1 = new TPunkt(0,0);
  TPunkt P2 = new TPunkt(0,0);
  TPunkt P3 = new TPunkt(0,0);
  
  float flLen = 10 * iLevel;
  
  Turtel.PU();
  
  Turtel.CT ();
  Turtel.flDeg = TurtelBase.P0.x;
  Turtel.FW (flLen);
  P1.Assign(Turtel.P0);

  Turtel.CT ();
  Turtel.LT (120);
  Turtel.FW (flLen);
  P2.Assign(Turtel.P0);

  Turtel.CT ();
  Turtel.LT (120);
  Turtel.FW (flLen);
  P3.Assign(Turtel.P0);

  Turtel.PD();
  
  
  Turtel.Draw(P1,P2);
  Turtel.Draw(P2,P3);
  Turtel.Draw(P3,P1);
  }

int iStep = 0;

void draw() {

  background (255);  
  
  strokeWeight(2);   
  
  iStep++;
  if (iStep == 100) {
    TTurtel Turtel0 = lstBase.get(0);    
    Turtel0.P0.x = 50;
    }
  else if (iStep == 200) {
    TTurtel Turtel0 = lstBase.get(0);    
    Turtel0.P0.x = 100;
    iStep = 0;
    }
   
  
  for (int i=MAX-1; i > 0; i--) {
    TTurtel TurtelFrom = lstBase.get(i);
    TTurtel TurtelTo   = lstBase.get(i-1);
  
    TurtelFrom.MoveTo(TurtelTo.P0);
    TurtelFrom.Draw();
    
    drawTriangle(i, TurtelFrom);
    }    
 }



class TPunkt {
  float x;
  float y;
  
  TPunkt(float _x, float _y) {
     x = _x;
     y = _y;
    }
    
  void Assign(TPunkt _p) {
    x = _p.x;
    y = _p.y;
    }
  }

  
class TTurtel {
  boolean boPenDown;
  float   flDeg;
  TPunkt  P0;
  
  TTurtel (float x, float y, float _flDeg) {
    boPenDown = true;
    flDeg     = _flDeg;
    P0        = new TPunkt (x,y);
    }
    
  void PU () {
    boPenDown = false;
    }
  
  void PD () {
    boPenDown = true;
    }
          
   void LT (float _flDeg) {
     flDeg -= _flDeg;
     }
   
   void RT (float _flDeg) {
     flDeg += _flDeg;
     }

   void FW (float flLen) {
     Exec (flLen);
     }
 
    void BW (float flLen) {
     float flTmp = flDeg;
     flDeg += 180;
     Exec (flLen);
     flDeg = flTmp;
     }

    void FW (float _flDeg, float flLen) {
     Exec (flLen);
     }
     
    void BW (float _flDeg, float flLen) {
     float flTmp = flDeg;
     flDeg += (_flDeg + 180);
     Exec (flLen);
     flDeg = flTmp;
     }

    void CT () {
      P0.x = width / 2;
      P0.y = height / 2;      
      //     flDeg = 0.0;
      }

    void CLR () {
      background (255);  
      }

  void Exec (float flLen) {    
    float flFactor = TWO_PI / 360.0;
    TPunkt P1 = new TPunkt(P0.x + sin(flFactor * flDeg) * flLen, P0.y + cos(flFactor* flDeg) * flLen);
    if (boPenDown)
      Draw(P0, P1);
    P0.Assign(P1);
    }


  void MoveTo (TPunkt P1) {
    
    TPunkt P0Old = new TPunkt(0.0, 0.0);
    P0Old.Assign(P0);
    
    float flx2h  = (P0.x - P1.x) / DIVISOR;
    float fly2h  = (P0.y - P1.y) / DIVISOR;

    PU ();
    
    if (P0.x <  P1.x && P0.y <  P1.y) {
       // ok println (1);
       flDeg = 270;
       FW (flx2h);
       LT (90);
       FW (fly2h);    
      }
    else if (P0.x <  P1.x && P0.y >= P1.y) {
       // ok println (2);
       flDeg = 270;
       FW (flx2h);
       LT (90);
       FW (fly2h);
      }
    else if (P0.x >= P1.x && P0.y <  P1.y) {
       // ok println (3); 
       flDeg = 270;
       FW (flx2h);
       LT (90);
       FW (fly2h);
      }
    else if (P0.x >= P1.x && P0.y >= P1.y) {
       // ok println (4);
       flDeg = 270;
       FW (flx2h);
       LT (90);
       FW (fly2h);    
      }    
      
      PD ();
      // Draw (P0Old, P0);
      
      P0.y = P0Old.y;
    }

   void Draw (TPunkt P1, TPunkt P2) {
    line (P1.x, P1.y, P2.x, P2.y);
    }
        
   void Draw () {
    line (P0.x -5, P0.y -5, P0.x +5, P0.y -5);
    line (P0.x +5, P0.y -5, P0.x +5, P0.y +5);
    line (P0.x +5, P0.y +5, P0.x -5, P0.y +5);
    line (P0.x -5, P0.y +5, P0.x -5, P0.y -5);
    }
  }
  
Tagged:
Sign In or Register to comment.