2017年10月26日 星期四

week 7. Project 1 demo & using sin(x)

sample codes:

float radius;
int segment = 10;
float centerX;
float centerY;

void setup(){
 size(600,600);
 background(0);
 stroke(255);
 centerX = height/2;
 centerY = width/2;
}

void draw(){
  background(0);
  radius = dist(centerX, centerY, mouseX, mouseY);
  spiral(centerX,centerY,radius,segment);
}
void spiral(float x_c,float y_c, float r, int seg){
    for(int i =0;i<=seg;i++){
      float x = x_c+cos(i*2*PI/seg)*r;  //算出x的位置
      float y = y_c+sin(i*2*PI/seg)*r;  //算出y的位置
      point(x,y);  // 畫出點,也能用其他形狀
    }
}



EX:
1. 每次點擊滑鼠,劃一個隨機半徑大小的圓。
2. draw flower of life (生命之花)

reference:

https://processing.org/examples/sine.html

https://processing.org/examples/sinecosine.html

John Whitney
https://www.youtube.com/watch?v=BzB31mD4NmA

https://www.youtube.com/watch?v=s_KIrIJkLoE

2017年10月12日 星期四

week 5. 2D Arrays

review:
  draw a rotating shape in the center of screen.

  also use 'scale()' to change the size of the object.


參考:
https://processing.org/examples/array2d.html

用 2D array 設定每個元素的顏色,旋轉速度,屬性。

float[][] distances;
float[][] rotateAngles;
float[][] rotateRates;
float maxDistance;
int spacer;

void setup() {
  size(640, 360);
  maxDistance = dist(width/2, height/2, width, height);
  distances = new float[width][height];
  rotateAngles = new float[width][height];
  rotateRates = new float[width][height];
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      float distance = dist(width/2, height/2, x, y);
      distances[x][y] = distance/maxDistance * 255;
      rotateAngles[x][y] = random(5.0);
      rotateRates[x][y] = random(5.0, 40.0);
    }
  }
  spacer = 30;
  strokeWeight(3);
  //noLoop();  // Run once and stop
}

void draw() {
  background(0);
  // This embedded loop skips over values in the arrays based on
  // the spacer variable, so there are more values in the array
  // than are drawn here. Change the value of the spacer variable
  // to change the density of the points
  for (int y = 0; y < height; y += spacer) {
    for (int x = 0; x < width; x += spacer) {
      pushMatrix();
      translate(x, y);
      rotate(frameCount/rotateRates[x][y]+rotateAngles[x][y]);
      rect(-spacer/2, -spacer/2, spacer, spacer);
      stroke(distances[x][y]);
      popMatrix();
    }
  }
}


Project 1:
  2D 陣列作品系列五連作,參考 CODE+FORM
  Deadline Oct. 27, 2017

2017年10月5日 星期四

week 4. 2D rotation


download processing on ios (android) and try



void setup(){
  size(640, 360);
  background(0);
}

int gridSize = 40;

void draw() {
background(0);
stroke(200);
noFill();
for (int x = gridSize; x <= width - gridSize; x += gridSize) {
  for (int y = gridSize; y <= height - gridSize; y += gridSize) {
 
   rotateShape(x, y);
 
  }
}

}

void rotateShape(int xloc, int yloc) {
  pushMatrix();
  translate(xloc, yloc);
 // rotate((frameCount / 8.0)+xloc+yloc+mouseX);
 rotate((mouseX/100)*frameCount / 8.0+xloc+yloc);
 rect(-10, -10, 20, 20);
 line(-10, -10, 10, 10);
  popMatrix();


}


References:
https://processing.org/examples/star.html



example #2:

void setup(){
  size(640, 360);
  background(0);
}

int gridSize = 40;

void draw() {
background(0);
stroke(200);
noFill();
for (int x = gridSize; x <= width - gridSize; x += gridSize) {
  for (int y = gridSize; y <= height - gridSize; y += gridSize) {
   
   rotateShape(x, y);
   
  }
}

}

void rotateShape(int xloc, int yloc) {
  pushMatrix();
  translate(xloc, yloc);
 rotate((frameCount / 8.0)+xloc+yloc);
 //rotate((mouseX/100)*frameCount / 8.0+xloc+yloc);
 beginShape();
vertex(-10, -10);
vertex(0, -10);
vertex(0, 0);
vertex(10, 0);
vertex(10, 10);
vertex(-10, 10);
endShape(CLOSE);
 //rect(-10, -10, 20, 20);
// line(-10, -10, 10, 10);
  popMatrix();
 
 
}