AAD

(Algoritmicly Aided Desing)

Thursday, June 12, 2014

#FLcreativecoding 25 Squares B answer to challenge

/*
 * Creative Coding
 * Week 2, 03 - n squares
completely rec0ded by Marius Ivaskevicius
 */
int minSqrSize=10;
class Square{
  float xPos;
  float yPos;
  int DesX;
  int DesY;
  float size=minSqrSize;
  float desSize= size;
  int h=10;
  int speed=10;//record2 else10-----------------------------------------------------------------------------------
  boolean isFalling=false;
  int fallSpeed=0;
  boolean isReadied=false;
  void teleport(int x,int y){
    this.isFalling=false;
    this.xPos=this.DesX=x;
    this.yPos=this.DesY=y;
  }
  void target(int x,int y){
    this.DesX=x;
    this.DesY=y;
  }
  void plotShadow(){
    fill(128,61,0,100); // shadow
    rect(this.xPos+this.h,
         this.yPos+this.h,
         this.size,
         this.size);
  }
  void plotBody(){
    fill(255,136,4,200); // rectangle
    rect(this.xPos,
         this.yPos,
         this.size,
         this.size);
  }
  void update(){
    this.xPos-=(this.xPos-this.DesX)/speed;
    if(this.isFalling){
      if(this.yPos      this.fallSpeed+=1;
      this.yPos+=this.fallSpeed;
    }else{
      this.yPos-=(this.yPos-this.DesY)/speed;
    }
    this.size-=(this.size-this.desSize)/speed;
  }
  void fall(){
    this.isFalling=true;
    this.isReadied=false;
    this.fallSpeed=0;
    this.DesX+=random(-500,500);
  }
  void ready(){this.isReadied=true;}
  boolean isReady(){return this.isReadied;}
  boolean isFallen(){return this.isFalling;}
  void descent(){
    this.isFalling=false;
    this.isReadied=false;
    this.yPos=this.size*-1;
  }
  void grow(int size){
    this.desSize=size;
  }
}
int eventCounter=0;
int eventCounterTot=120;//record5 else100 ------------------------------------------------------------------------
int eventShortCounter=eventCounterTot-60;
int eventNumber=0;

int sqrCountX;
int sqrCountY;
int sqrTot;
int oldSqrTot;
//int[] arr= new int[10];
//arr[1]=0;
Square[] squares;

int curSqrCountX;
int curSqrCountY;
int stepX;
int stepY;
int curSqrTot;
void newScale(int size){
  curSqrCountX=(width/(size+minSqrSize))-1;
  curSqrCountY=(height/(size+minSqrSize))-1;
  curSqrTot=curSqrCountX*curSqrCountY;
  //oldSqrTot=curSqrTot;
  stepX=width/(curSqrCountX+1);
  stepY=height/(curSqrCountY+1);
}
void setup() {
  size(800, 600);
  sqrCountX=width/(minSqrSize*2)-1;
  sqrCountY=height/(minSqrSize*2)-1;
  sqrTot=sqrCountX*sqrCountY;
  newScale(minSqrSize);
  squares= new Square[sqrTot];
  int curY=0;
  for(int i=0;i      squares[i]=new Square();
      if((i%sqrCountX)==0){curY++;}
      squares[i].teleport((i%sqrCountX)*stepX+stepX,curY*stepY);
  }
  rectMode(CENTER);
  noStroke();
  //frameRate(1);//record ------------------------------------------------------------------------------------------
 
}
void shuffle(){
  Square temp;
  int pick;
   for(int i=0; i       temp = squares[i];
       pick  = int(random(squares.length));
       squares[i] = squares[pick];
       squares[pick]= temp;
     }
}
void sort(){
  Square[] tmpSquares;
  tmpSquares= new Square[sqrTot];
  int j=0;
  int f=sqrTot-1;
  for(int i=0;i    if(squares[i].isFallen() && !squares[i].isReady()){tmpSquares[f--]=squares[i];}
    else{tmpSquares[j++]=squares[i];}
  }
  squares=tmpSquares;
}
int newSize;
void draw() {
    background(90,114,169); // clear the screen to grey
    for(int i=0;i    for(int i=0;i    for(int i=0;i    if(++eventCounter>=eventCounterTot){ //------( event !!! )-----
      if(eventNumber==0){
          oldSqrTot=curSqrTot;
          newSize=int(random(minSqrSize,100));
          newScale(newSize);
          if(oldSqrTot>curSqrTot){
            eventNumber=1;//drop
          }else{
            eventNumber=3;//grow
          }
      }
      switch(eventNumber){
        case 1://drop
          shuffle();
          sort();
          int toDrop=sqrTot-curSqrTot;
          for(int i=curSqrTot;i          eventCounter=eventShortCounter;
          eventNumber=2;//move
          break;
        case 2://move
          eventCounter=eventShortCounter;
          for(int i=oldSqrTot;i          shuffle();
          sort();
          int curY=0;
          for(int i=0;i            if(i%curSqrCountX==0){curY++;}
            squares[i].target((i%curSqrCountX)*stepX+stepX,curY*stepY);
          }
          if(oldSqrTot>curSqrTot){
            eventNumber=3;//grow
          }else{
            eventNumber=4;//descent
          }
          break;
        case 3://grow
          for(int i=0;i          if(oldSqrTot>curSqrTot){
            eventCounter=0;
            eventNumber=0;//reconfigure
          }else{
            eventCounter=eventShortCounter;
            eventNumber=2;//move
          }
          break;
        case 4://descent
          eventCounter=0;
          for(int i=0;i            if(squares[i].isReady()){squares[i].descent();}
          }
          eventNumber=0;//reconfigure
          break;
      }
    }
    //saveFrame("sqrn####.jpg");//record -------------------------------------------------------------------------
} //end of draw

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home