openobject.org

Emergence Kaleidoscope final version

From Physical Programming

This's the same as the one Scott edited for me [[1]], just putting it here for easier access and with more explanations.

PS>> With this shorter codes, it's a bit different from my original program because now you can't save with 'S' key and when you press any key it actually restarts the mode again.

But anyway...Enjoy~!! :D

 float x, y;
 int i, j;
 int a, z;
 
 int checkPress = 0; 
 
 void setup(){
  frameRate(15);
  size(screen.width, screen.height);
  background(0);
  noStroke();
  smooth();
  //Import an image for your cover with these codes
  //PImage img;
  //img = loadImage("cover.jpg");
  //image(img, screen.width,/2-250, screen.height/2-250);
 }

change the originate point to mid screen so we can get the kaleidoscope display from the center

 void draw(){
  translate(screen.width/2, screen.height/2);
 

get the state of the mouse button. if it's down it's true

  boolean mouseState = mousePressed;

if the mouse button is being clicked then clear the screen with the background on every frame while doing IF condition of mode 1-4

  if (mouseState) {
    background(0);
  }

Set the kaleidoscope drawing patterns for each mode

  if (checkPress == 1){
    x=-300;
    y=-300;
    gridA(mouseState);
  }
 
  if (checkPress == 2){
    x=-490;
    y=-386.6;
    gridB(x, mouseState);
 
    x=-415;
    y=-343.3;
    gridB(x, mouseState);
  }
 
  if (checkPress == 3){
    x=-290;
    y=-310;
    gridC(x, mouseState);
 
    x=-330;
    y=-350;
    gridC(x, mouseState);
  }
 
  if (checkPress == 4){
    x=(-30/tan(PI/12))-230;
    y=(-30/tan(PI/12))-250;
    gridD(x, mouseState);
 
    x=(-50/tan(PI/12))-230;
    y=(-50/tan(PI/12))-250;
    gridD(x, mouseState);
  }
 }

if the mouse is not click, use the background to clear the screen only once before going into each IF condition of mode 1-4

 void keyPressed() {
  if (mousePressed == false) {
    background(0);
  }
  if (key == '1') {
    checkPress = 1;
  } 
  else if (key == '2') {
    checkPress = 2;
  }  
  else if (key == '3') {
    checkPress = 3;
  } 
  else if (key == '4') {
    checkPress = 4;
  }
 }

If the mouse is clicked, then it will draw a full color ellipse which is Alpha 255, while if not, it'll draw a more vivid color, so just Alpha 30. In these commands, they duplicate and rotate around the main triangle into patterns.

 void gridA(boolean checkalpha){
  for (a=0;a<5;a++){
    y+=100;
    for (z=0;z<5;z++){
      x+=100;
 
      translate(x,y);
      for (j=0;j<4;j++){
        noFill();
        if (checkalpha == false)
          drawCell(0, 0, -50, 50, 50, 50, 20, 30, 30, 30);
        else
          drawCell(0, 0, -50, 50, 50, 50, 255, 255, 255, 255);
        rotate(PI/2);
      }
      translate(-x,-y);
    }
    x=-300;
  }
 }
 
 void gridB(float origin, boolean checkalpha){
  for (a=0;a<7;a++){
    y+=86.6;
    for (z=0;z<5;z++){
      x+=150;
 
      translate(x,y);
 
      for (j=0;j<6;j++){
        noFill();
        if (checkalpha == false)
          drawCell(0, 0, -25, (sqrt(sq(50)-sq(25))), 25, (sqrt(sq(50)-sq(25))), 20, 30, 30, 30);  
        else
          drawCell(0, 0, -25, (sqrt(sq(50)-sq(25))), 25, (sqrt(sq(50)-sq(25))), 255, 255, 255, 255);  
        rotate(PI/3);
      }
      translate(-x,-y);
    }
    x=origin;  
  } 
 }
 
 void gridC(float origin, boolean checkalpha){
  for (a=0;a<7;a++){
    y+=80;
    for (z=0;z<7;z++){
      x+=80;
 
      translate(x,y);
      for (j=0;j<4;j++){
        noFill();
        if (checkalpha == false)
          drawCell(0, 0, 0, 40, -40, 0, 20, 30, 30, 30);
        else
          drawCell(0, 0, 0, 40, -40, 0, 255, 255, 255, 255);
        rotate(PI/2);
      }
      translate(-x,-y);
    }
    x=origin;
  }
 }
 
 void gridD(float origin, boolean checkalpha){  
  for (a=0;a<4;a++){
    y+=(40/tan(PI/12));
    for (z=0;z<4;z++){
      x+=(40/tan(PI/12));
      translate(x, y);
      for (j=0;j<12;j++){
        noFill();
        if (checkalpha == false)
          drawCell(0, 0,-20, 20/(tan(PI/12)), 20, 20/(tan(PI/12)), 20, 30, 30, 30);
        else
          drawCell(0, 0,-20, 20/(tan(PI/12)), 20, 20/(tan(PI/12)), 255, 255, 255, 255);
        rotate(PI/6);
      }
      translate(-x,-y);
    }
    x=origin;
  } 
 }
 

Basically, I just have the same triangle duplicated and rotated around to create the kaleidoscope patterns. In the triangle, it has symmetric images of ellipses which move and change colors according to its location of X, Y.

 void drawCell(float x1, float y1, float x2, float y2, float x3, float y3, float A1, float A2, float A3, float A4){
  triangle (x1, y1, x2, y2, x3, y3);
  int r = int(mouseX);
  int g = int(mouseY);
  int b = int(abs(mouseX-mouseY));
 
  fill(abs(r-200), abs(g-50), abs(b-100),A1);
  ellipse (mouseX/10, mouseY/10, 7, 7);
  ellipse (-mouseX/10, mouseY/10, 7, 7);
 
  fill(abs(r-100), abs(g-100), abs(b-200), A2);
  ellipse (mouseY/20, mouseX/20, 5, 5);
  ellipse (-mouseY/20, mouseX/20, 5, 5);
 
  fill(abs(r-50), abs(g-200), abs(b-50), A3);
  ellipse (mouseX/20, mouseX/20, 3, 3);
  ellipse (-mouseX/20, mouseX/20, 3, 3);
 
  fill(r, g, b, A4);
  ellipse (mouseY/20, mouseY/20, 3, 3);
  ellipse (-mouseY/20, mouseY/20, 3, 3);
 }