Turtles - 3D system in motion

edited March 2016 in Share Your Work

int     MAX       = 90;
int     DELTA     = 15;
float   flDivisor = 10;
float   flZoom    = 2;

TTurtel Turtel0 = new TTurtel (0, 0, 90);
TTurtel TurtelX = new TTurtel (0, 0, 90);
TTurtel TurtelY = new TTurtel (0, 0, 90);
TTurtel TurtelZ = new TTurtel (0, 0, 90);

TTurtel Turtel  = new TTurtel (0,0, 0);      

boolean bAnimation = true;


void DrawFrameTo( 
    TTurtel TurtelA,
    TTurtel TurtelB,
    TTurtel TurtelC) {
      
  Turtel.PlaceTo (Turtel0);
  Turtel.LookAt (TurtelA);

  float flen = 100;

  float fzd = flen;
  for (int i=0; i< 15; i++) {
    
    TTurtel T1 = new TTurtel(Turtel.P0.x, Turtel.P0.y, 0);
    T1.DrawTo (TurtelB);
    
    T1.PlaceTo (Turtel);
    T1.DrawTo (TurtelC);
    
    Turtel.FW (fzd);

    fzd = fzd - (fzd * 0.1);
    
    if (abs(fzd) < 0.1)
     return; 
  }
}


void draw() {

  background (255);

  DrawFrameTo (TurtelX, TurtelY, TurtelZ);
  DrawFrameTo (TurtelY, TurtelX, TurtelZ);
  DrawFrameTo (TurtelZ, TurtelX, TurtelY);

  if (bAnimation) {
    TurtelX.FW (1);
    TurtelX.RT (1.8); 
  
    TurtelY.FW (1);
    TurtelY.RT (1.8); 
  
    TurtelZ.FW (1);
    TurtelZ.RT (3.6);
  
    Turtel0.FW (1);
    Turtel0.RT (3.6); 
    }
 }


void DrawTurtel (TPunkt P0, float flDelta) {  
  line (P0.x -flDelta, P0.y -flDelta, P0.x +flDelta, P0.y -flDelta);
  line (P0.x +flDelta, P0.y -flDelta, P0.x +flDelta, P0.y +flDelta);
  line (P0.x +flDelta, P0.y +flDelta, P0.x -flDelta, P0.y +flDelta);
  line (P0.x -flDelta, P0.y +flDelta, P0.x -flDelta, P0.y -flDelta);
}


void drawPoint(float x, float y, float z, boolean invisible) {
   
  Turtel.PU();
  Turtel.P0.Assign(Turtel0.P0);  
  Turtel.LookAt (TurtelX);
  Turtel.FW (flZoom * x);  
  Turtel.LookAt (TurtelY);
  Turtel.FW (flZoom * y);  
  Turtel.LookAt (TurtelZ);

  float fz  = z;
  float ffz = 0;
  for (int i = 0; i < z; i++) {
    float fzDelta = fz / flDivisor;
    fz -= fzDelta;
    if (fz < 0)
      break;
    ffz += fz; 
    }
  Turtel.FW (flZoom * ffz);
  Turtel.PD();  
  }
  
  
int iDim   = 1;
int iPoint = 1;

void keyPressed() {  
  if (key == 'z') {
    Turtel0.P0.x = random(width);
    Turtel0.P0.y = random(height);
    loop();
    return;
  }
  else if (key == 'a') {
    bAnimation = !bAnimation;
    return;
  }
  
  int iRichtung = 1;
  
       if (key == 'x') iDim = 1;
  else if (key == 'y') iDim = 2;
  

  else if (key == '0') iPoint = 0;  
  else if (key == '1') iPoint = 1;  
  else if (key == '2') iPoint = 2;  
  else if (key == '3') iPoint = 3;  

  
  else if (key == '+')
     iRichtung = 1;
     
  else if (key == '-') 
     iRichtung = -1;
     
  else if (key == 'q')  {
    Turtel0.P0.Set(400, 300);
    TurtelX.P0.Set(700, 300);
    TurtelY.P0.Set(400, 100);
    TurtelZ.P0.Set(700, 100);
   }
     
     
 switch (iPoint) {
    case 0: 
      switch (iDim) {
        case 1: Turtel0.P0.x += (iRichtung * DELTA); break;
        case 2: Turtel0.P0.y += (iRichtung * DELTA); break;
        }
      break;
    case 1: 
      switch (iDim) {
        case 1: TurtelX.P0.x += (iRichtung * DELTA); break;
        case 2: TurtelX.P0.y += (iRichtung * DELTA); break;
        }
      break;
    case 2: 
      switch (iDim) {
        case 1: TurtelY.P0.x += (iRichtung * DELTA); break;
        case 2: TurtelY.P0.y += (iRichtung * DELTA); break;
        }
      break;
    case 3:   
      switch (iDim) {
        case 1: TurtelZ.P0.x += (iRichtung * DELTA); break;
        case 2: TurtelZ.P0.y += (iRichtung * DELTA); break;
        }
      break;
   }

  
  
  println ("Punkt0: " + Turtel0.P0.x + " / " + Turtel0.P0.y);
  println ("PunktX: " + TurtelX.P0.x + " / " + TurtelX.P0.y);
  println ("PunktY: " + TurtelY.P0.x + " / " + TurtelY.P0.y);
  println ("PunktZ: " + TurtelZ.P0.x + " / " + TurtelZ.P0.y);
  
  loop();
}
  






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;
    }
    
  void Set (float _x, float _y) {
    x = _x;
    y = _y;
    }
  };


  
class TTurtel {
  boolean boPenDown;
  float   flDeg;
  TPunkt  P0;
  float   flLen;
  float   flFactor;
  
  TTurtel (float x, float y, float _flDeg) {
    boPenDown = true;
    flDeg     = _flDeg;
    P0        = new TPunkt (x,y);
    flLen     = 0.0;
    flFactor  = TWO_PI / 360.0;
    }

  void Set (float x, float y, float _flDeg, float _flLen) {
    flDeg    = _flDeg;
    flLen    = _flLen;
    P0.x     = x;
    P0.y     = 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 FW () {
      
      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;      
      
      }

    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);
      Draw(P0, P1);
      P0.Assign(P1);
      }


  void MoveTo (TPunkt P1, float divisor, boolean boPenUpDown) {
    
    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;

    if (boPenUpDown) 
      PU ();
    flDeg = 270;
    FW (flx2h);
    LT (90);
    FW (fly2h);    
    if (boPenUpDown) 
      PD ();
    
    
    }


  void DrawTo (TTurtel T) {
    if (!boPenDown)
      return;

    line (P0.x, P0.y, T.P0.x, T.P0.y);
    }
    

   void DrawVector () {
    float fl1 = 3;
    float fl2 = fl1*2;
    float fl3 = fl1*3;

     PU ();
     FW (fl1);
     PD ();
     
     RT (fl1);
     BW (fl2);
     
     PU ();
     FW (fl2);
     PD ();
     
     LT (fl2);
     BW (fl2);
     
     PU ();
     FW (fl2);     
     RT (fl1);
     BW (fl1);
     PD ();

     BW (fl3);
     PU ();
     FW (fl3);
     PD ();
     }

   void Draw (TPunkt P1, TPunkt P2) {
    if (!boPenDown)
      return;

    line (P1.x, P1.y, P2.x, P2.y);
    }
  
        
   void Draw (float flDelta) {

    if (!boPenDown)
      return;
     
    line (P0.x -flDelta, P0.y -flDelta, P0.x +flDelta, P0.y -flDelta);
    line (P0.x +flDelta, P0.y -flDelta, P0.x +flDelta, P0.y +flDelta);
    line (P0.x +flDelta, P0.y +flDelta, P0.x -flDelta, P0.y +flDelta);
    line (P0.x -flDelta, P0.y +flDelta, P0.x -flDelta, P0.y -flDelta);
    }
    
    void LookAt (TTurtel Turtel, float factor) {
     float _flDeg = flDeg % 360;
     LookAt(Turtel); 
     flDeg = _flDeg + (flDeg - _flDeg) * factor;
     }
    
    void LookAt (TTurtel Turtel) {
      float xDelta = Turtel.P0.x - P0.x;
      float yDelta = Turtel.P0.y - P0.y;
      
      if (abs(xDelta) <= 0.0001) {
        flDeg = 0; 
        return;
        }
        
      if (abs(yDelta) <= 0.0001) {
        flDeg = 90.0; 
        return;
        }
        
      float alpha = atan (yDelta / xDelta) / flFactor;
       
      if (xDelta >= 0 && yDelta >= 0)
        flDeg = 90 - alpha;
      else if (xDelta >= 0 && yDelta < 0)
        flDeg = 90 - alpha;
      else if (xDelta < 0 && yDelta >= 0)
        flDeg = 270 - alpha;
      else if (xDelta < 0 && yDelta < 0)
        flDeg = 270 - alpha;  
      }

    void PlaceTo (TTurtel T) {
      P0.Assign (T.P0);
      }
    
  };
  
  
  
  
  



void setup() {
  size(800, 600);       
  strokeWeight(1); 
  frameRate (40);
  smooth ();
  
  float cx = width / 2;
  float cy = height / 2;
  Turtel0 = new TTurtel (cx, cy, 0);
  TurtelX = new TTurtel (cx, cy, 0);
  TurtelY = new TTurtel (cx, cy, 0);
  TurtelZ = new TTurtel (cx, cy, 0);
  
  TurtelX.flDeg =  90 + 5;
  TurtelY.flDeg = 270 - 5;
  TurtelZ.flDeg =   0; 
  
  TurtelX.FW (1000);
  TurtelY.FW (1000);
  TurtelZ.FW (1000);

  TurtelX.RT (90);
  TurtelY.RT (90);
  TurtelZ.RT (90);
  }
Tagged:

Comments

Sign In or Register to comment.