2025年3月27日 星期四

晚安week06

 。T移動-R旋轉-T移動

0.上週複習

//week06_01_translate_rotate_matrix
void setup() {
  size(500, 500, P3D);
}
void draw() {
  background(142);
  ellipse(width/2, height/2, 200, 200);
  translate(width/2, height/2);
  pushMatrix();
    if(mousePressed) rotateZ(radians(frameCount));
    box(100, 30, 30);
  popMatrix();
}
就只是上周的東西

1.T-R-T

//week06_02_translate_rotate_translate_matrix
void setup() {
  size(500, 500, P3D);
}
void draw() {
  background(142);
  ellipse(width/2, height/2, 200, 200);
  translate(width/2, height/2);
  pushMatrix();
    if(mousePressed) rotateZ(radians(frameCount));
    translate(-50,0,0);
    box(100, 30, 30);
  popMatrix();
}

執行結果:


T-R-T_2
//week06_03_pusht-r-t-popmatrix
void setup() {
  size(500, 500, P3D);
}
void draw() {
  background(142);
  ellipse(width/2, height/2, 200, 200);
  translate(width/2, height/2);
  pushMatrix();
  translate(x,y);
    if(mousePressed && mouseButton == RIGHT) rotateZ(radians(frameCount));
    //按下滑鼠右鍵旋轉
    translate(-50,0,0);
    box(100, 30, 30);
  popMatrix();
}
float x=0,y=0;
void mouseDragged(){ //拖曳到指定位置
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
}

執行結果:

 。組裝機械手臂
1.端點+下臂
//week06_04_box_push_T_R_T__box_pop
//組裝機械臂
void setup() {
  size(500, 400, P3D);
}
void draw() {
  background(255);
  translate(width/2,height/2);
  sphere(10);//做為中心參照物
  
  box(200,25,25);//step05手肘
  
  fill(252,131,77);
  pushMatrix();
    translate(x,y); //step06
    if(mousePressed) rotateZ(radians(frameCount));
    translate(25,0,0);//step02,中心在左側
    box(50,25,50);//step01
  popMatrix();
}
float x=0, y=0;//step06
void mouseDragged(){//step06拖曳到位置
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
  println("x:" + x + " y:" + y);//step07 印出數值
}

"拖曳"程式碼用來找到適合的座標
執行結果:


2.組裝下臂
//week06_05_push_t_push_T_R_T_pop_pop
//組裝機械臂
void setup() {
  size(500, 400, P3D);
}
void draw() {
  background(255);
  translate(width/2,height/2);
  sphere(10);//做為中心參照物
  
  
  fill(252,131,77);
  pushMatrix();//step04 新的矩陣
    translate(x,y);//step05 拖曳整個下臂
    box(200,50,25);//手肘
    //step03加入新的階層,往右縮排
    //_________________________________________________________________________________
    pushMatrix();
      translate(100,0);//step01指定到確定的座標
      //if(mousePressed) 
      rotateZ(radians(frameCount));//step02 只轉動,不等待mousePressed
      translate(25,0,0);//中心在左側
      box(50,25,50);
    popMatrix();
    //_____________________________________________________________________________________
  popMatrix();
}
float x=0, y=0;
void mouseDragged(){
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
  println("x:" + x + " y:" + y);
}

執行結果:


3.下臂+上臂
//week06_05_push_t_push_T_R_T_pop_pop
//組裝機械臂
void setup() {
  size(500, 400, P3D);
}
void draw() {
  background(255);
  translate(width/2,height/2);
  sphere(10);//做為中心參照物
  
  
  fill(252,131,77);
  pushMatrix();//step04 新的矩陣
    translate(x,y);//step05 拖曳整個下臂
    box(200,50,25);//手肘
    //step03加入新的階層,往右縮排
    //_________________________________________________________________________________
    pushMatrix();
      translate(100,0);//step01指定到確定的座標
      //if(mousePressed) 
      rotateZ(radians(frameCount));//step02 只轉動,不等待mousePressed
      translate(25,0,0);//中心在左側
      box(50,25,50);
    popMatrix();
    //_____________________________________________________________________________________
  popMatrix();
}
float x=0, y=0;
void mouseDragged(){
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
  println("x:" + x + " y:" + y);
}
執行結果:


4.組裝整個機械手臂(T-R-T*3)
//week06_07_push_T_R_T_pop_3times
//組裝機械臂
void setup() {
  size(500, 800, P3D);
}
void draw() {
  background(255);
  translate(width/2,height/2);
  sphere(10);//做為中心參照物
  
  
  fill(252,131,77);
  pushMatrix();//縮排後加入新的階層矩陣
    if(mousePressed && mouseButton == RIGHT) rotateX(radians(frameCount));//step02 右鍵旋轉
    if(mousePressed && mouseButton == RIGHT) rotateY(radians(frameCount));//step02 右鍵旋轉
    if(mousePressed && mouseButton == RIGHT) rotateZ(radians(frameCount));//step02 右鍵旋轉
    translate(x,y);//step01移動整個手臂 0,100移動到視窗中心
    box(50,200,25);//上臂
    //=================================================================================
    pushMatrix();
      translate(0,-100);//移動到指定座標
      //if(mousePressed) //不等待mousePressed
      rotateZ(radians(frameCount));//轉動下臂
      translate(100,0);//下臂移動座標
      box(200,50,25);//手肘
     
      //_________________________________________________________________________________
      pushMatrix();
        translate(100,0);
        //if(mousePressed) 
        rotateZ(radians(frameCount));
        translate(25,0,0);//中心在左側
        box(50,25,50);
      popMatrix();
      //_____________________________________________________________________________________
    popMatrix();
    //=================================================================================
  popMatrix();
}
float x=0, y=0;
void mouseDragged(){
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
  println("x:" + x + " y:" + y);
}

執行結果:




















ゲッダン☆

?????????????



沒有留言:

張貼留言