読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

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

巻き取れる格子

インタラクション

「c」で回転方向の切り替え。

/**
* winding lattice
*  
* @author aa_debdeb
* @date 2016/07/28
*
* press "c" to toggle the direction of rotation
*
*/

int sampleStep = 2;
int lineStep = 10;
boolean clockwise = true;

PVector[][] points;

void setup(){
  size(500, 500);
  strokeWeight(2);
  stroke(0, 153, 153);
  noFill();
  
  int sampleNum = width / sampleStep + 1;
  points = new PVector[sampleNum][sampleNum];  
  for(int x = 0; x < sampleNum; x++){
    for(int y = 0; y < sampleNum; y++){
      points[x][y] = new PVector(x * sampleStep, y * sampleStep);
    }
  }
}

void draw(){
  background(255);
  int step = lineStep / sampleStep;
  for(int x = 0; x < points.length; x += step){
    beginShape();
    curveVertex(points[x][0].x, points[x][0].y);
    for(int y = 0; y < points[x].length; y += 1){
      curveVertex(points[x][y].x, points[x][y].y);
    }  
    curveVertex(points[x][points[x].length - 1].x, points[x][points[x].length - 1].y);
    endShape();
  }

    for(int y = 0; y < points[0].length; y += step){
    beginShape();
    curveVertex(points[0][y].x, points[0][y].y);
    for(int x = 0; x < points.length; x += 1){
      curveVertex(points[x][y].x, points[x][y].y);
    }  
    curveVertex(points[points[0].length - 1][y].x, points[points[0].length - 1][y].y);
    endShape();
  }
  
   if(mousePressed){
     PVector mouse = new PVector(mouseX, mouseY);
     for(int x = 0; x < points.length; x++){
       for(int y = 0; y < points[x].length; y++){
         float d = PVector.dist(mouse, points[x][y]);
         if(d < 200){
         PVector force = PVector.sub(points[x][y], mouse);
         force.normalize();
         force.mult(map(d, 0, 200, 2, 0));
         if(clockwise){
           force.rotate(HALF_PI);
         } else {
           force.rotate(-HALF_PI);         
         }
         points[x][y].add(force);
         }
       }
     }
   }
}

void keyPressed(){
  if(key == 'c'){
    clockwise = !clockwise;
  }
}