‹ Construct2

Construct2_3d

See /f0blog/useless-supercollider-class-no-2/

//
//  Construct2_3d
//
//  Copyright (c) 2003 Fredrik Olofsson Musikproduktion. All rights reserved.
//
//  040920 ported to processing
//  060227 3d version for processing
//  200801 updated for processing3
//----------------------------------------------------------------------------------------------
int i, step_X, step_Y, step_Z;
int x= 10, y= 10, z= 10;
int dx= 1, dy= 1, dz= 1;
int v;                      //variant
int m;                      //mapping
int col, row, depth;
int mov_X= 95, mov_Y= 95, mov_Z= 95;
int max_XPerCol, max_YPerRow, max_ZPerDepth;

void setup() {
  size(640, 480, P3D);
  frameRate(60);
  background(255);
  nytt();
}

void mousePressed() {
  background(255);
  nytt();
}

void keyTyped() {
  if (key==' ') {
    background(255);
    nytt();
  }
}

void draw() {
  if(int(random(1881))==0) {                 //lite d� och d�
    background(255);                         //rensa sk�rmen
    nytt();                                  //nya v�rden
  }
  drawFO();                                  //rita punkt(er)
  
  x= x+(dx*step_X);                          //flytta position x
  if(x<1)                                    //om v�nster kant
    dx= int(random(2));                      //v�nd eller st� still i x-led
  else if(x>(max_XPerCol))                   //om h�ger kant
    dx= 0-int(random(2));                    //v�nd eller st� still i x-led
  
  y= y+(dy*step_Y);                          //flytta position y
  if(y<1)                                    //om �vre kanten
    dy= int(random(2));                      //v�nd eller st� still i y-led
  else if(y>(max_YPerRow))                   //om nedre kanten
    dy= 0-int(random(2));                    //v�nd eller st� still i y-led
  
  z= z+(dz*step_Z);                          //flytta position z
  if(z<1)                                    //om botten
    dz= int(random(2));                      //v�nd eller st� still i z-led
  else if(z>(max_ZPerDepth))                 //om h�gst upp
    dz= 0-int(random(2));                    //v�nd eller st� still i z-led
  
  switch(v) {
    case 1:
      variant1();
      break;
    case 2:
      variant2();
      break;
    case 3:
      variant3();
      break;
    case 4:
      variant4();
      break;
    case 5:
      variant5();
      break;
    case 6:
      variant6();
      break;
    case 7:
      variant7();
      break;
    case 8:
      variant8();
      break;
    case 9:
      variant9();
      break;
    case 10:
      variant10();
      break;
  }
}

//----------------------------------------------------------------------------------------------
void variant1() {                              //1. slumpa riktningar individuellt
  if(random(100)>=mov_X) {slumpaRiktningX();}
  if(random(100)>=mov_Y) {slumpaRiktningY();}
  if(random(100)>=mov_Z) {slumpaRiktningZ();}
}
void variant2() {                              //2. slumpa alltid riktningar tillsammans
  slumpaRiktningXYZ();
}
void variant3() {                              //3. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draVanster();}
  if(random(100)>=mov_Y) {draUppat();}
  if(random(100)>=mov_Z) {draHogre();}
}
void variant4() {                              //4. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draHoger();}
  if(random(100)>=mov_Y) {draUppat();}
  if(random(100)>=mov_Z) {draHogre();}
}
void variant5() {                              //5. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draVanster();}
  if(random(100)>=mov_Y) {draNedat();}
  if(random(100)>=mov_Z) {draHogre();}
}
void variant6() {                              //6. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draHoger();}
  if(random(100)>=mov_Y) {draNedat();}
  if(random(100)>=mov_Z) {draHogre();}
}
void variant7() {                              //7. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draVanster();}
  if(random(100)>=mov_Y) {draUppat();}
  if(random(100)>=mov_Z) {draLagre();}
}
void variant8() {                              //8. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draHoger();}
  if(random(100)>=mov_Y) {draUppat();}
  if(random(100)>=mov_Z) {draLagre();}
}
void variant9() {                              //9. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draVanster();}
  if(random(100)>=mov_Y) {draNedat();}
  if(random(100)>=mov_Z) {draLagre();}
}
void variant10() {                             //10. slumpa riktningar individuellt
  if(random(100)>=mov_X) {draHoger();}
  if(random(100)>=mov_Y) {draNedat();}
  if(random(100)>=mov_Z) {draLagre();}
}

//----------------------------------------------------------------------------------------------
void nytt() {
  v= int(random(10))+1;        //variant
  m= int(random(36));          //mapping
  col= int(random(15))+1;
  row= int(random(13))+1;
  depth= int(random(11))+1;
  max_XPerCol= width/col;
  max_YPerRow= height/row;
  max_ZPerDepth= int((random(600)+20))/depth;
  step_X= 1;
  //step_X= int(random(2))+1;
  step_Y= 1;
  //step_Y= int(random(2))%2)+1;
  step_Z= 1;
  //step_Z= int(random(2))+1;
  slumpaRiktningXYZ();
  //println("v:"+v+" m:"+m+" col:"+col+" row:"+row+" depth:"+depth+" max_XPerCol:"+max_XPerCol+" max_YPerRow:"+max_YPerRow+" max_ZPerDepth:"+max_ZPerDepth);
  stroke(random(255), 55, 66);
}
void slumpaRiktningX() {
  dx= int(random(2));
}
void slumpaRiktningY() {
  dy= int(random(2));
}
void slumpaRiktningZ() {
  dz= int(random(2));
}
void slumpaRiktningXYZ() {
  while((dx==0)&&(dy==0)&&(dz==0)) {                  //kolla att inte alla riktningar blir 0
    dx= int(random(2));
    dy= int(random(2));
    dz= int(random(2));
  }
}
void draVanster() {
  if(dx==0&&x>=1) {dx= -1;} else {dx= 0;}             //dra �t v�nster
}
void draHoger() {
  if(dx==0&&x<=max_XPerCol) {dx= 1;} else {dx= 0;}    //dra �t h�ger
}
void draNedat() {
  if(dy==0&&y<=max_YPerRow) {dy= 1;} else {dy= 0;}    //dra ned�t
}
void draUppat() {
  if(dy==0&&y>=1) {dy= -1;} else {dy= 0;}             //dra upp�t
}
void draHogre() {
  if(dz==0&&z>=1) {dz= -1;} else {dz= 0;}             //dra h�gre
}
void draLagre() {
  if(dz==0&&z<=max_ZPerDepth) {dz= 1;} else {dz= 0;}  //dra l�gre
}

//----------------------------------------------------------------------------------------------
void drawFO() {
  int iCol, jRow, kDepth;
  int mx= m%3;
  int my= int(m/3)%3;
  int mz= int(m/6)%3;
  for(int i= 0; i<col; i++) {
    iCol= round((max_XPerCol)*i);
    if((mx==1&&i%2==0) || (mx==2&&i%2==1)) {
          x= mirrorX(x);
        }
    for(int j= 0; j<row; j++) {
      jRow= round((max_YPerRow)*j);
      if((my==1&&j%2==0) || (my==2&&j%2==1)) {
          y= mirrorY(y);
        }
      for(int k= 0; k<depth; k++) {
        kDepth= round((max_ZPerDepth)*k);
        if((mz==1&&k%2==0) || (mz==2&&k%2==1)) {
          z= mirrorZ(z);
        }
        point(x+iCol, y+jRow, z+kDepth);
      }
    }
  }
}
int mirrorX(int x) {
  return round((max_XPerCol)-x);
}
int mirrorY(int y) {
  return round((max_YPerRow)-y);
}
int mirrorZ(int z) {
  return round((max_ZPerDepth)-z);
}