2025年4月17日 星期四

12750530李忻穎_week09

 作業1:

  • 程式碼:
PShape body, head;
void setup() {
  size(400, 400, P3D);
  body = loadShape("body.obj");
  head = loadShape("head.obj");
}
void draw() {
  background(204);
  translate(200, 300);
  sphere(10); // 原點的球

  scale(10, -10, 10); // y要上下再反過來
  shape(body, 0, 0);
  shape(head, 0, 0);
}
  • 執行結果:



作業2:
  • 程式碼:
// week09_2_gundam_head_body_push_trt_pop
PShape body, head;
void setup() {
  size(400, 400, P3D);
  body = loadShape("body.obj");
  head = loadShape("head.obj");
}
void draw() {
  background(204);
  translate(200, 300);
  sphere(10); // 原點的球

  scale(10, -10, 10);
  
  shape(body, 0, 0);
  pushMatrix();
    translate(0, 22.5);
    rotateY(radians(mouseX-200));
    rotateX(radians(mouseY-60));
    translate(0, -22.5);
    shape(head, 0, 0);
  popMatrix();
}
  • 執行結果:




作業3:
  • 程式碼:
// week09_3_gundam_uparm_upuparm_push_trt_pop
PShape uparm1, upuparm1;
void setup(){
  size(400, 400, P3D);
  uparm1 = loadShape("uparm1.obj");
  upuparm1 = loadShape("upuparm1.obj");
}
void draw(){
  background(204);
  translate(200, 300);
  sphere(3); // 小一點, 比較清楚
  
  scale(10, -10, 10);
  
  shape(upuparm1, 0, 0); // 上上手臂
  pushMatrix();
    translate(-4.1, 19.9); // 再掛回去原本的位置
    rotateX(radians(mouseY));
    translate(4.1, -19.9); // 把物體的旋轉中心, 放到座標中心
    //translate(mouseX/10.0, -mouseY/10.0); // 一邊移動、一邊找到數值
    //println(mouseX/10.0, -mouseY/10.0); // 印出適合的數值 ex. 4.1, -19.9
    shape(uparm1, 0, 0); // 上手臂
  popMatrix();
}
  • 執行結果:



作業4:
  • 程式碼:
// week09_4_gundam_uparm_upuparm_hand_push_trt_pop
PShape uparm1, upuparm1, hand1;
void setup(){
  size(400, 400, P3D);
  uparm1 = loadShape("uparm1.obj");
  upuparm1 = loadShape("upuparm1.obj");
  hand1 = loadShape("hand1.obj");
}
void draw(){
  background(204);
  translate(200, 300);
  sphere(3); // 小一點, 比較清楚
  
  scale(10, -10, 10);
  
  shape(upuparm1, 0, 0); // 上上手臂
  pushMatrix();
    translate(-4.1, 19.9); // 再掛回去原本的位置
    rotateZ(radians(mouseX));
    translate(4.1, -19.9); // 把物體的旋轉中心, 放到座標中心
    shape(uparm1, 0, 0); // 上手臂
    pushMatrix();
      translate(-4.5, +16.9);
      rotateX(radians(mouseY));
      translate(4.5, -16.9); // 剛剛把手移到座標中心的移動量
      //translate(mouseX/10.0, -mouseY/10.0); // 一邊移動、一邊找到數值
      //println(mouseX/10.0, -mouseY/10.0); // 印出適合的數值 ex. 4.5, -16.9
      shape(hand1, 0, 0);
    popMatrix();
  popMatrix();
}
  • 執行結果:




作業5-1:
  • 程式碼:
// week09_5_gundam_uparm_upuparm_hand_keyboard_mouse_angle
PShape uparm1, upuparm1, hand1;
void setup(){
  size(400, 400, P3D);
  uparm1 = loadShape("uparm1.obj");
  upuparm1 = loadShape("upuparm1.obj");
  hand1 = loadShape("hand1.obj");
}
float [] angle = new float[20]; //準備20個關節
int ID = 0; // 關節的代碼 之後會用 angle[ID] 來改變值
void keyPressed(){
  if(key=='1') ID = 1;
  if(key=='2') ID = 2;
}
void mouseDragged(){
  angle[ID] += mouseX - pmouseX; // X方向的移動, 可改變某個關節的角度
}
void draw(){
  background(204);
  translate(200, 300);
  sphere(3); // 小一點, 比較清楚
  
  scale(10, -10, 10);
  
  shape(upuparm1, 0, 0); // 上上手臂
  pushMatrix();
    translate(-4.1, 19.9); // 再掛回去原本的位置
    rotateZ(radians(angle[1]));
    translate(4.1, -19.9); // 把物體的旋轉中心, 放到座標中心
    shape(uparm1, 0, 0); // 上手臂
    pushMatrix();
      translate(-4.5, +16.9);
      rotateX(radians(angle[2]));
      translate(4.5, -16.9); // 剛剛把手移到座標中心的移動量
      shape(hand1, 0, 0);
    popMatrix();
  popMatrix();
}
  • 執行結果:


作業5-2:
  • 程式碼:
// week09_5_gundam_uparm_upuparm_hand_keyboard_mouse_angle
PShape uparm1, upuparm1, hand1;
void setup(){
  size(400, 400, P3D);
  uparm1 = loadShape("uparm1.obj");
  upuparm1 = loadShape("upuparm1.obj");
  hand1 = loadShape("hand1.obj");
}
void draw(){
  background(204);
  translate(200, 300);
  sphere(3); // 小一點, 比較清楚
  
  scale(10, -10, 10);
  
  shape(upuparm1, 0, 0); // 上上手臂
  pushMatrix();
    translate(-4.1, 19.9); // 再掛回去原本的位置
    rotateZ(radians(mouseX));
    translate(4.1, -19.9); // 把物體的旋轉中心, 放到座標中心
    shape(uparm1, 0, 0); // 上手臂
    pushMatrix();
      translate(-4.5, +16.9);
      rotateX(radians(mouseY));
      translate(4.5, -16.9); // 剛剛把手移到座標中心的移動量
      //translate(mouseX/10.0, -mouseY/10.0); // 一邊移動、一邊找到數值
      //println(mouseX/10.0, -mouseY/10.0); // 印出適合的數值 ex. 4.5, -16.9
      shape(hand1, 0, 0);
    popMatrix();
  popMatrix();
}
  • 執行結果:


作業6:
  • 程式碼:
// week09_6_save_saveStrings_loadStrings
void setup(){
  size(300, 300);
  rect(10, 10, 80, 80);
  rect(110, 110, 80, 80);
  save("file.png"); // 這個範例, 可把某個畫面 save()存檔
  another = loadStrings("lines.txt"); // 如果順利讀到, 就有陣列了
}
int x = 10, y = 10, ID = 0;
void draw(){
  background(204);
  if(another != null){
    int [] now = int(split(another[ID], ' '));
    rect(now[0], now[1], 80, 80);
    ID = (ID+1) % another.length;
  }
  rect(x, y, 80, 80);
}
void mouseDragged(){
  x += mouseX - pmouseX;
  y += mouseY - pmouseY;
  String now = x + " " + y; // 現在座標的字串
  lines.add(now); //println(now); // 在小黑印出來
}
ArrayList<String> lines = new ArrayList<String>(); // ArrayList 資料結構
String [] another; // 另外一個讀到的字串的資料結構(一開始沒有東西)
void keyPressed(){
  String [] arr = new String[lines.size()]; // 傳統的 Java 陣列的資料結構
  lines.toArray(arr); // 把 ArrayList 轉換成傳統的 陣列, 以便存檔
  if(key=='s') saveStrings("lines.txt", arr); // 按下英文小寫 s 會存檔
}
  • 執行結果:


沒有留言:

張貼留言