2025年5月29日 星期四

12751036-徐詩淳_week15









 // week15_01_multiple_windows

// google: processing multiple windows

// File > Example > Demos > Tests > MultipleWindows 

void setup(){

  size(300, 200);

  background(255, 0, 0);

  WindowB child = new WindowB();

}

void draw(){

}

//以下獨立執行

class WindowB extends PApplet{

  public WindowB(){

    super();

    PApplet.runSketch(new String[]{this.getClass().getName()}, this);

  }

  public void settings(){

    size(300, 200);

  }

  public void setup(){

    // size(300, 200);

    background(0, 255, 0);

  }

  public void draw(){ //空白 要放!

  }

}









// week15_02_multiple_window_PGraphics

PGraphics pg; // 外面宣告 不同人都可使用

void setup(){

  size(400, 400, P3D);

  pg = createGraphics(200, 200, P3D);

}

void draw(){

 background(255, 0, 0); 

 pg.beginDraw();

 pg.background(0, 255, 0);

 pg.translate(100, 100);

 pg.rotateY(radians(frameCount));

 pg.box(100);

 pg.endDraw();

 image(pg, 0, 0);

}









// week15_03_mutiple_windows_pg_pg2_pg3_pg4

// 修改自 week15_02_multiple_window_PGraphics

PGraphics pg, pg2, pg3, pg4; // 外面宣告 不同人都可使用

void setup(){

  size(400, 400, P3D);

  pg = createGraphics(200, 200, P3D);

  pg2 = createGraphics(200, 200, P3D);

  pg3 = createGraphics(200, 200, P3D);

  pg4 = createGraphics(200, 200, P3D);

}

void draw(){

 background(255, 0, 0); 

 pg.beginDraw();

 pg.background(0, 255, 0);

 pg.translate(100, 100);

 pg.rotateY(radians(frameCount));

 pg.box(100);

 pg.endDraw();

 

 pg2.beginDraw();

 pg2.background(255, 255, 0);

 pg2.translate(100, 100);

 pg2.rotateY(radians(frameCount));

 pg2.box(100);

 pg2.endDraw();

 

 pg3.beginDraw();

 pg3.background(255, 0, 0);

 pg3.translate(100, 100);

 pg3.rotateY(radians(frameCount));

 pg3.box(100);

 pg3.endDraw();

 

 pg4.beginDraw();

 pg4.background(255, 0, 255);

 pg4.translate(100, 100);

 pg4.rotateY(radians(frameCount));

 pg4.box(100);

 pg4.endDraw();

 

 image(pg, 0, 0);

 image(pg2, 200, 0);

 image(pg3, 0, 200);

 image(pg4, 200, 200);

}









// week15_04_Arcball_rotation_PGraphics_pg

// 修改自 week15_03_mutiple_windows_pg_pg2_pg3_pg4

// 又從 File > Example > Demos > Tests > MultipleWindows 

PGraphics pg, pg2, pg3, pg4; // 外面宣告 不同人都可使用

Arcball arcball; // 用偷來的 宣告小寫的

void setup(){

  size(400, 400, P3D);

  arcball = new Arcball(this, 300);

  pg = createGraphics(200, 200, P3D);

  pg2 = createGraphics(200, 200, P3D);

  pg3 = createGraphics(200, 200, P3D);

  pg4 = createGraphics(200, 200, P3D);

}

void mousePressed(){

  arcball.mousePressed();

}

void mouseDragged(){

  arcball.mouseDragged();

}

void draw(){

 background(255, 0, 0); 

 pg.beginDraw();

 pg.background(0, 255, 0);

 arcball.run();

 //pg.translate(100, 100);

 //pg.rotateY(radians(frameCount));

 pg.box(100);

 pg.endDraw();

 

 pg2.beginDraw();

 pg2.background(255, 255, 0);

 pg2.translate(100, 100);

 pg2.rotateY(radians(frameCount));

 pg2.box(100);

 pg2.endDraw();

 

 pg3.beginDraw();

 pg3.background(255, 0, 0);

 pg3.translate(100, 100);

 pg3.rotateY(radians(frameCount));

 pg3.box(100);

 pg3.endDraw();

 

 pg4.beginDraw();

 pg4.background(255, 0, 255);

 pg4.translate(100, 100);

 pg4.rotateY(radians(frameCount));

 pg4.box(100);

 pg4.endDraw();

 

 image(pg, 0, 0);

 image(pg2, 200, 0);

 image(pg3, 0, 200);

 image(pg4, 200, 200);

}









// week15_05_postman_mouseDragged

// 重新來一次, 讓程式的架構變更清楚

PImage postman, head, body, uparm1, hand1, uparm2, hand2, foot1, foot2;

void setup(){ // 把上面的圖, 都拉近新專案裡

  size(560, 560);

  postman = loadImage("postman.png");

  head = loadImage("head.png");

  body = loadImage("body.png");

  uparm1 = loadImage("uparm1.png");

  uparm2 = loadImage("uparm2.png");

  hand1 = loadImage("hand1.png");

  hand2 = loadImage("hand2.png");

  foot1 = loadImage("foot1.png"); // 增加腳 旋轉中心 220,375

  foot2 = loadImage("foot2.png"); // 增加腳 旋轉中心 260,372

}

float [] angleX = new float[10]; // 在 3D 的世界裡, 我們的旋轉

float [] angleY = new float[10];

int ID = 0; // 一開始是頭的關節

ArrayList<String> lines = new ArrayList<String>();

void keyPressed(){

  if(key=='s'){ // 現在動作的所有關節,都存起來

    String now = ""; // 空字串

    for(int i=0; i<10; i++){

      now += angleX[i] + " "; // 後面有空格

      now += angleY[i] + " "; // 後面有空格

    }

    lines.add(now); // 把現在的動作的這行, 加到 lines 裡

    String [] arr = new String[lines.size()];

    lines.toArray(arr);

    saveStrings("angles.txt", arr);

    println("現在存檔:" + now);

  }

  if(key=='r'){

    String [] file = loadStrings("angles.txt");

    if(file != null){

      for(int i=0; i<file.length; i++){

        lines.add(file[i]);

        println("現在讀檔:" + file[i]);

      }

    }

  }

  if(key=='p') playing = !playing; // 播動畫 <=> 不播動畫

 

  if(key=='1') ID = 1; // 左邊臂

  if(key=='2') ID = 2; // 左邊手

  if(key=='3') ID = 3; // 右邊臂

  if(key=='4') ID = 4; // 右邊手

  if(key=='5') ID = 5; // 左邊腳

  if(key=='6') ID = 6; // 右邊腳

  if(key=='0') ID = 0; // 頭

}

boolean playing = false; // 一開始,不播放動畫, 按下 'p'可切換

void mouseDragged(){

  float dx = mouseX - 236, dy = mouseY - 231;

  angleX[0] = degrees(atan2(dx, dy)) + 90;

  

  // angleX[ID] += mouseX - pmouseX;

  // angleY[ID] += mouseY - pmouseY; // 多了這一行

}

int R = 0;

void myInterpolate(){ // 請不要剪貼, 老師重寫

  if(lines.size()>=2){ // 要有2行以上, 才能做內插

    float alpha = (frameCount%30)/30.0; // 介於 0.0~1.0中間的值

    if(alpha==0.0) R = (R+1)%lines.size(); // 如果變到0.0就換下一組

    int R2 = (R+1)%lines.size();

    float [] oldAngle = float(split( lines.get( R ), ' '));

    float [] newAngle = float(split( lines.get( R2 ), ' '));

    for(int i=0; i<10; i++){

      angleX[i] = oldAngle[i*2+0]*(1-alpha) + newAngle[i*2+0]*alpha;

      angleY[i] = oldAngle[i*2+1]*(1-alpha) + newAngle[i*2+1]*alpha;

    }

  }

}

void draw(){

  background(#FFFFF2);

  if(playing) myInterpolate();

  image(body, 0, 0); // 先畫身體

 

  pushMatrix();

    translate(+236, +231); // 再放回去正確的位置

    rotate(radians(angleX[0]));

    translate(-236, -231); // 把頭的旋轉中心, 放到(0,0)

    image(head, 0, 0); // 再畫頭

  popMatrix();


  pushMatrix(); // 腳 foot1

    translate(220, 375);

    rotate(radians(angleX[5]));

    translate(-220, -375);

    image(foot1, 0, 0);

  popMatrix();


  pushMatrix(); // 腳 foot2

    translate(260, 372);

    rotate(radians(angleX[6]));

    translate(-260, -372);

    image(foot2, 0, 0);

  popMatrix();

 

  pushMatrix(); // 要畫左邊的上手臂、手肘

    translate(+185, +261);

    rotate(radians(angleX[1]));

    translate(-185, -261);

    image(uparm1, 0, 0); // 上手臂

    pushMatrix();

      translate(+116, +265);

      rotate(radians(angleX[2]));

      translate(-116, -265);

      image(hand1, 0, 0); // 手肘

    popMatrix();

  popMatrix();  

 

  pushMatrix(); // 要畫右邊的上手臂、手肘

    translate(290, 262);

    rotate(radians(angleX[3]));

    translate(-290, -262);

    image(uparm2, 0, 0);

    pushMatrix();

      translate(357, 259);

      rotate(radians(angleX[4]));

      translate(-357, -259);

      image(hand2, 0, 0);

    popMatrix();

  popMatrix();

}









// week15_06_postman_mouseDragged_posX_posY_ID_angleX_ID_atan2

// 修改自 week15_05_postman_mouseDragged

// 重新來一次, 讓程式的架構變更清楚

PImage postman, head, body, uparm1, hand1, uparm2, hand2, foot1, foot2;

void setup(){ // 把上面的圖, 都拉近新專案裡

  size(560, 560);

  postman = loadImage("postman.png");

  head = loadImage("head.png");

  body = loadImage("body.png");

  uparm1 = loadImage("uparm1.png");

  uparm2 = loadImage("uparm2.png");

  hand1 = loadImage("hand1.png");

  hand2 = loadImage("hand2.png");

  foot1 = loadImage("foot1.png"); // 增加腳 旋轉中心 220,375

  foot2 = loadImage("foot2.png"); // 增加腳 旋轉中心 260,372

}

float [] angleX = new float[10]; // 在 3D 的世界裡, 我們的旋轉

float [] angleY = new float[10];

int ID = 0; // 一開始是頭的關節

ArrayList<String> lines = new ArrayList<String>();

void keyPressed(){

  if(key=='s'){ // 現在動作的所有關節,都存起來

    String now = ""; // 空字串

    for(int i=0; i<10; i++){

      now += angleX[i] + " "; // 後面有空格

      now += angleY[i] + " "; // 後面有空格

    }

    lines.add(now); // 把現在的動作的這行, 加到 lines 裡

    String [] arr = new String[lines.size()];

    lines.toArray(arr);

    saveStrings("angles.txt", arr);

    println("現在存檔:" + now);

  }

  if(key=='r'){

    String [] file = loadStrings("angles.txt");

    if(file != null){

      for(int i=0; i<file.length; i++){

        lines.add(file[i]);

        println("現在讀檔:" + file[i]);

      }

    }

  }

  if(key=='p') playing = !playing; // 播動畫 <=> 不播動畫

 

  if(key=='1') ID = 1; // 左邊臂

  if(key=='2') ID = 2; // 左邊手

  if(key=='3') ID = 3; // 右邊臂

  if(key=='4') ID = 4; // 右邊手

  if(key=='5') ID = 5; // 左邊腳

  if(key=='6') ID = 6; // 右邊腳

  if(key=='0') ID = 0; // 頭

}

boolean playing = false; // 一開始,不播放動畫, 按下 'p'可切換

float [] posX = {+236, +185, +116, 290, 357, 220, 260};

float [] posY = {+231, +261, +265, 262, 259, 375, 372};

float [] posAngle = {90, 180, 180, 0, 0, -90, -90};

void mouseDragged(){

  float dx = mouseX - 236, dy = mouseY - 231;

  angleX[0] = degrees(atan2(dx, dy)) + 90;

  

  // angleX[ID] += mouseX - pmouseX;

  // angleY[ID] += mouseY - pmouseY; // 多了這一行

}

int R = 0;

void myInterpolate(){ // 請不要剪貼, 老師重寫

  if(lines.size()>=2){ // 要有2行以上, 才能做內插

    float alpha = (frameCount%30)/30.0; // 介於 0.0~1.0中間的值

    if(alpha==0.0) R = (R+1)%lines.size(); // 如果變到0.0就換下一組

    int R2 = (R+1)%lines.size();

    float [] oldAngle = float(split( lines.get( R ), ' '));

    float [] newAngle = float(split( lines.get( R2 ), ' '));

    for(int i=0; i<10; i++){

      angleX[i] = oldAngle[i*2+0]*(1-alpha) + newAngle[i*2+0]*alpha;

      angleY[i] = oldAngle[i*2+1]*(1-alpha) + newAngle[i*2+1]*alpha;

    }

  }

}

void draw(){

  background(#FFFFF2);

  if(playing) myInterpolate();

  image(body, 0, 0); // 先畫身體

 

  pushMatrix();

    translate(+236, +231); // 再放回去正確的位置

    rotate(radians(angleX[0]));

    translate(-236, -231); // 把頭的旋轉中心, 放到(0,0)

    image(head, 0, 0); // 再畫頭

  popMatrix();


  pushMatrix(); // 腳 foot1

    translate(220, 375);

    rotate(radians(angleX[5]));

    translate(-220, -375);

    image(foot1, 0, 0);

  popMatrix();


  pushMatrix(); // 腳 foot2

    translate(260, 372);

    rotate(radians(angleX[6]));

    translate(-260, -372);

    image(foot2, 0, 0);

  popMatrix();

 

  pushMatrix(); // 要畫左邊的上手臂、手肘

    translate(+185, +261);

    rotate(radians(angleX[1]));

    translate(-185, -261);

    image(uparm1, 0, 0); // 上手臂

    pushMatrix();

      translate(+116, +265);

      rotate(radians(angleX[2]));

      translate(-116, -265);

      image(hand1, 0, 0); // 手肘

    popMatrix();

  popMatrix();  

 

  pushMatrix(); // 要畫右邊的上手臂、手肘

    translate(290, 262);

    rotate(radians(angleX[3]));

    translate(-290, -262);

    image(uparm2, 0, 0);

    pushMatrix();

      translate(357, 259);

      rotate(radians(angleX[4]));

      translate(-357, -259);

      image(hand2, 0, 0);

    popMatrix();

  popMatrix();

}

float [] posX = {+236, +185, +116, 290, 357, 220, 260};

float [] posY = {+231, +261, +265, 262, 259, 375, 372};

float [] posAngle = {90, 180, 180, 0, 0, -90, -90};


沒有留言:

張貼留言