Press "Enter" to skip to content

Motifs de tâches aléatoires / Processing


Génération de tâches aléatoires à travers une class Processing createSpash()

Source: drawSplash.pde / Processing 3.5.3

int middleWidth;
int middleHeight;

void setup(){
  size(800,600);
  background(255);
  middleWidth = width/2;
  middleHeight = height/2;
  imageMode(CENTER);
  //noLoop();
  drawingSplash();
  smooth();
}

void draw(){
  pushStyle();
  fill(150);
  noStroke();
  textSize(20);
  textAlign(CENTER);
  text("Tape space to drawing splash", width/2, height-20);
  popStyle();
}

void drawingSplash(){
  background(255);
  image(createSplash(color(int(random(255)), 100, 200)), middleWidth, middleHeight);
}

PGraphics createSplash(color c){
  int count = 20;
  int maxRadius = 100;
  PGraphics splashShape;
  PVector vStart;
  PVector startPoint = new PVector();
  PVector endPoint = new PVector();
  PVector shapeSize = new PVector(maxRadius*3, maxRadius*3);
  PVector shapeCenter = new PVector(shapeSize.x/2, shapeSize.y/2);;
  float angle = 0;
  color shapeColor = c;
  
  splashShape = createGraphics(int(shapeSize.x),int(shapeSize.y));
  splashShape.beginDraw();
  splashShape.beginShape();
  splashShape.fill(shapeColor);
  splashShape.noStroke();

  for(int i=0; i<count; i++){
    vStart = new PVector(random(maxRadius*0.50, maxRadius), 0);
    angle = i*(TWO_PI/count);
    vStart.rotate(angle);
    
    if(i == 0){
       startPoint.x = vStart.x + shapeCenter.x;
       startPoint.y = vStart.y + shapeCenter.y;
       splashShape.curveVertex(startPoint.x, startPoint.y);
    }else if(i==1){
      endPoint = new PVector(vStart.x + shapeCenter.x,vStart.y + shapeCenter.y);
    }
    
    splashShape.curveVertex(vStart.x + shapeCenter.x, vStart.y + shapeCenter.y);
    
    if(i == count-1){
      splashShape.curveVertex(startPoint.x, startPoint.y);
      splashShape.curveVertex(endPoint.x, endPoint.y);
    }
  }
  splashShape.endShape();
  splashShape.endDraw();
  
  return splashShape;
}

void keyPressed(){
  if(key == ' '){
    drawingSplash();
  }
}

Be First to Comment

Laisser un commentaire