Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

Random Shapes

/**
* Random Shapes
*
* This sketch draws random 300 shapes (ellipse, quadrangle or triangle).
*
* @author aa_debdeb
* @date 2015/08/20
*
*/
void setup(){

  size(300, 300);
  noStroke();
  smooth();
  background(255, 255, 255);
  
  for(int i = 0; i < 300; i++){
    drawRandomShape();
  }
}

void drawRandomShape(){
  fill(int(random(255)), int(random(255)), int(random(255)));
  float basicSize = 20;
  int shapeId = int(random(3));
  if(shapeId == 0){
    float x = getRandomX();
    float y = getRandomY();
    float xWidth = basicSize * getRandomScale();
    float yWidth = basicSize * getRandomScale();
    ellipse(x, y, xWidth, yWidth);
  } else if(shapeId == 1){
    float x1 = getRandomX();
    float y1 = getRandomY();
    float x2 = x1 + basicSize * getRandomScale();
    float y2 = y1 + basicSize * getRandomScale();
    float x3 = x1 + basicSize * getRandomScale();
    float y3 = y1 + basicSize * getRandomScale();
    float x4 = x1 + basicSize * getRandomScale();
    float y4 = y1 + basicSize * getRandomScale();
    quad(x1, y1, x2, y2, x3, y3, x4, y4);
  } else {
    float x1 = getRandomX();
    float y1 = getRandomY();
    float x2 = x1 + basicSize * getRandomScale();
    float y2 = y1 + basicSize * getRandomScale();
    float x3 = x1 + basicSize * getRandomScale();
    float y3 = y1 + basicSize * getRandomScale();
    triangle(x1, y1, x2, y2, x3, y3);
  }
}

float getRandomX(){
  return random(1) * width;
}

float getRandomY(){
  return random(1) * height;
}

float getRandomScale(){
  return random(2) - 1.0;
}