2025年5月1日 星期四

12750432

 //week11-1-postman-saveStrings-loadStrings

PImage postman, head,body,uparm1,hand1,uparm2,hand2;
float [] angle = new float[20];//prepare 20 關節的變數
int ID = 0;//現 在要處理的關節
void mouseDragged(){
  angle[ID] += mouseX-pmouseX;
}
void keyPressed(){
  if(key=='1') ID = 1;//leftarm
  if(key=='2') ID = 2;//lefthand
  if(key=='3') ID = 3;//rightarm
  if(key=='4') ID = 4;//righthand
  if(key=='5') ID = 5;
  if(key=='6') ID = 6;
  if(key=='0') ID = 0;//head
  
  if(key=='s'){//begin here每按一次 就存一組
    String now = "";//要放現在全部關節的值
    for(int i=0;i<20;i++){
      now+=angle[i]+" ";//全部塞到now 裡
    }
    lines.add(now);//這行加到line 裡
    String [] arr = new String[lines.size()];
    lines.toArray(arr);
    saveStrings("angles.txt",arr);
  }
  if(key=='r'){//讀入
    if(R==0){//如果目前沒檔案
            String [] file = loadStrings("angles.txt");
            if (file != null){//如果有讀到檔案
              for(int i=0;i<file.length;i++){//就檔案內容逐行
                lines.add(file[i]);//加到lines資料結構裡
              }
            }
        }
      if(R<lines.size()){
          float[]now = float(split(lines.get(R),' '));
          for(int i=0;i<20;i++) angle[i] = now[i];
          R=(R+1)%lines.size();
      }
  }
}
int R=0;
ArrayList<String> lines = new ArrayList<String>();//put the result of move
void setup(){
  size(560,560);
  postman = loadImage("postman.png");
  head = loadImage("head.png");
  body = loadImage("body.png");
  uparm1 = loadImage("right-arm.png");
  hand1 = loadImage("right-hand.png");
  uparm2 = loadImage("left-arm.png");
  hand2 = loadImage("left-hand.png");
  
}
void draw(){
  background(#FFFFF2);
  image(postman,0,0);//基礎郵差
  fill(255,0,255,128);//半透明紫色
  rect(0,0,560,560);//蓋上去
  pushMatrix();
    translate(+197,+262);//放回正確位置
    rotate(radians(angle[1]));
    translate(-197,-262);//arm的旋轉中心放到(0,0)
    image(uparm1,0,0);
    pushMatrix();
      translate(+119,+265);//放回正確位置
      rotate(radians(angle[2]));
      translate(-119,-265);//arm的旋轉中心放到(0,0)
      image(hand1,0,0);
    popMatrix();
  popMatrix();
   pushMatrix();
    translate(+289,+260);//放回正確位置
    rotate(radians(angle[3]));
    translate(-289,-260);//arm的旋轉中心放到(0,0)
    image(uparm2,0,0);
    pushMatrix();
      translate(+355,+258);//放回正確位置
      rotate(radians(angle[4]));
      translate(-355,-258);//arm的旋轉中心放到(0,0)
      image(hand2,0,0);
    popMatrix();
  popMatrix();
  pushMatrix();
    translate(+233,+230);//放回正確位置
    rotate(radians(angle[0]));
    translate(-233,-230);//頭的旋轉中心放到(0,0)
    image(head,0,0);//在畫頭
  popMatrix();
  image(body,0,0);//在畫身體
}
//week11-2-postman-alpha-interpolation
PImage postman, head,body,uparm1,hand1,uparm2,hand2;
float [] angle = new float[20];//prepare 20 關節的變數
int ID = 0;//現 在要處理的關節
void mouseDragged(){
  angle[ID] += mouseX-pmouseX;
}
void keyPressed(){
  if(key=='1') ID = 1;//leftarm
  if(key=='2') ID = 2;//lefthand
  if(key=='3') ID = 3;//rightarm
  if(key=='4') ID = 4;//righthand
  if(key=='5') ID = 5;
  if(key=='6') ID = 6;
  if(key=='0') ID = 0;//head
  
  if(key=='s'){//begin here每按一次 就存一組
    String now = "";//要放現在全部關節的值
    for(int i=0;i<20;i++){
      now+=angle[i]+" ";//全部塞到now 裡
    }
    lines.add(now);//這行加到line 裡
    String [] arr = new String[lines.size()];
    lines.toArray(arr);
    saveStrings("angles.txt",arr);
  }
  
  if(key=='r'){//讀入
    if(R==0){//如果目前沒檔案
            String [] file = loadStrings("angles.txt");
            if (file != null){//如果有讀到檔案
              for(int i=0;i<file.length;i++){//就檔案內容逐行
                lines.add(file[i]);//加到lines資料結構裡
              }
            }
        }
      if(R<lines.size()){
          float[]now = float(split(lines.get(R),' '));
          for(int i=0;i<20;i++) angle[i] = now[i];
          R=(R+1)%lines.size();
      }
  }
  if(key=='p'){
    float [] oldAngle = float(split(lines.get(R),' '));
    float [] newAngle = float(split(lines.get((R+1)%lines.size()),' '));
    float alpha = (frameCount%30)/30.0;
    for(int i=0; i<20;i++) angle[i] = oldAngle[i]*(1-alpha)+newAngle[i]*alpha;
   }
}
int R=0;
ArrayList<String> lines = new ArrayList<String>();//put the result of move
void setup(){
  size(560,560);
  postman = loadImage("postman.png");
  head = loadImage("head.png");
  body = loadImage("body.png");
  uparm1 = loadImage("right-arm.png");
  hand1 = loadImage("right-hand.png");
  uparm2 = loadImage("left-arm.png");
  hand2 = loadImage("left-hand.png");
  
}
void draw(){
  background(#FFFFF2);
  image(postman,0,0);//基礎郵差
  fill(255,0,255,128);//半透明紫色
  rect(0,0,560,560);//蓋上去
  pushMatrix();
    translate(+197,+262);//放回正確位置
    rotate(radians(angle[1]));
    translate(-197,-262);//arm的旋轉中心放到(0,0)
    image(uparm1,0,0);
    pushMatrix();
      translate(+119,+265);//放回正確位置
      rotate(radians(angle[2]));
      translate(-119,-265);//arm的旋轉中心放到(0,0)
      image(hand1,0,0);
    popMatrix();
  popMatrix();
   pushMatrix();
    translate(+289,+260);//放回正確位置
    rotate(radians(angle[3]));
    translate(-289,-260);//arm的旋轉中心放到(0,0)
    image(uparm2,0,0);
    pushMatrix();
      translate(+355,+258);//放回正確位置
      rotate(radians(angle[4]));
      translate(-355,-258);//arm的旋轉中心放到(0,0)
      image(hand2,0,0);
    popMatrix();
  popMatrix();
  pushMatrix();
    translate(+233,+230);//放回正確位置
    rotate(radians(angle[0]));
    translate(-233,-230);//頭的旋轉中心放到(0,0)
    image(head,0,0);//在畫頭
  popMatrix();
  image(body,0,0);//在畫身體
}
//week11-2-postman-alpha-interpolation-better
PImage postman, head,body,uparm1,hand1,uparm2,hand2;
float [] angle = new float[20];//prepare 20 關節的變數
int ID = 0;//現 在要處理的關節
void mouseDragged(){
  angle[ID] += mouseX-pmouseX;
}
void keyPressed(){
  if(key=='1') ID = 1;//leftarm
  if(key=='2') ID = 2;//lefthand
  if(key=='3') ID = 3;//rightarm
  if(key=='4') ID = 4;//righthand
  if(key=='5') ID = 5;
  if(key=='6') ID = 6;
  if(key=='0') ID = 0;//head
  
  if(key=='s'){//begin here每按一次 就存一組
    String now = "";//要放現在全部關節的值
    for(int i=0;i<20;i++){
      now+=angle[i]+" ";//全部塞到now 裡
    }
    lines.add(now);//這行加到line 裡
    String [] arr = new String[lines.size()];
    lines.toArray(arr);
    saveStrings("angles.txt",arr);
  }
  
  if(key=='r'){//讀入
    if(R==0){//如果目前沒檔案
            String [] file = loadStrings("angles.txt");
            if (file != null){//如果有讀到檔案
              for(int i=0;i<file.length;i++){//就檔案內容逐行
                lines.add(file[i]);//加到lines資料結構裡
              }
            }
        }
      if(R<lines.size()){
          float[]now = float(split(lines.get(R),' '));
          for(int i=0;i<20;i++) angle[i] = now[i];
          R=(R+1)%lines.size();
      }
  }
  /*if(key=='p'){
    float alpha = (frameCount%30)/30.0;
    if(alpha==0.0) R=(R+1)%line.size();
    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<20;i++) angle[i] = oldAngle[i]*(1-alpha)+newAngle[i]*alpha;
   }*/
}
int R=0;
ArrayList<String> lines = new ArrayList<String>();//put the result of move
void setup(){
  size(560,560);
  postman = loadImage("postman.png");
  head = loadImage("head.png");
  body = loadImage("body.png");
  uparm1 = loadImage("right-arm.png");
  hand1 = loadImage("right-hand.png");
  uparm2 = loadImage("left-arm.png");
  hand2 = loadImage("left-hand.png");
  
}
void myInterpolate(){
  if(lines.size()>0){
    float alpha = (frameCount%30)/30.0;
    if(alpha==0.0) R=(R+1)%lines.size();
    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<20;i++) angle[i] = oldAngle[i]*(1-alpha)+newAngle[i]*alpha;
   
 }
}
void draw(){
  myInterpolate();
  background(#FFFFF2);
  image(postman,0,0);//基礎郵差
  fill(255,0,255,128);//半透明紫色
  rect(0,0,560,560);//蓋上去
  pushMatrix();
    translate(+197,+262);//放回正確位置
    rotate(radians(angle[1]));
    translate(-197,-262);//arm的旋轉中心放到(0,0)
    image(uparm1,0,0);
    pushMatrix();
      translate(+119,+265);//放回正確位置
      rotate(radians(angle[2]));
      translate(-119,-265);//arm的旋轉中心放到(0,0)
      image(hand1,0,0);
    popMatrix();
  popMatrix();
   pushMatrix();
    translate(+289,+260);//放回正確位置
    rotate(radians(angle[3]));
    translate(-289,-260);//arm的旋轉中心放到(0,0)
    image(uparm2,0,0);
    pushMatrix();
      translate(+355,+258);//放回正確位置
      rotate(radians(angle[4]));
      translate(-355,-258);//arm的旋轉中心放到(0,0)
      image(hand2,0,0);
    popMatrix();
  popMatrix();
  pushMatrix();
    translate(+233,+230);//放回正確位置
    rotate(radians(angle[0]));
    translate(-233,-230);//頭的旋轉中心放到(0,0)
    image(head,0,0);//在畫頭
  popMatrix();
  image(body,0,0);//在畫身體
}
//week11-3-postman-again
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("right-arm.png");
  uparm2 = loadImage("left-arm.png");
  hand1 = loadImage("right-hand.png");
  hand2 = loadImage("left-hand.png");
  foot1 = loadImage("right-leg.png");
  foot2 = loadImage("left-leg.png");
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    translate(220,375);
    rotate(radians(frameCount));
    translate(-220,-375);
    image(foot1,0,0);
  popMatrix();
  pushMatrix();
    translate(260,372);
    rotate(radians(frameCount));
    translate(-260,-372);
    image(foot2,0,0);
  popMatrix();
 }

//week11-3-postman-again
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("right-arm.png");
  uparm2 = loadImage("left-arm.png");
  hand1 = loadImage("right-hand.png");
  hand2 = loadImage("left-hand.png");
  foot1 = loadImage("right-leg.png");
  foot2 = loadImage("left-leg.png");
}
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);
    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;//play <=>not play
  if(key=='1') ID = 1;//leftarm
  if(key=='2') ID = 2;//lefthand
  if(key=='3') ID = 3;//rightarm
  if(key=='4') ID = 4;//righthand
  if(key=='5') ID = 5;
  if(key=='6') ID = 6;
  if(key=='0') ID = 0;//head
}
boolean playing = false;//if p pressed,the animate will start
void mouseDragged(){
  angleX[ID] += mouseX-pmouseX;
  angleY[ID] += mouseY-pmouseY;
}
int R=0;
void myInterpolate(){
  if(lines.size()>=2){
    float alpha = (frameCount%30)/30.0;
    if(alpha==0.0) R=(R+1)%lines.size();
    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[1*2+0]*(1-alpha) +newAngle[i*2+0]*alpha;
      angleY[i] = oldAngle[1*2+1]*(1-alpha) +newAngle[i*2+1]*alpha;
    }
  }
}
void draw(){
  background(#FFFFF2);
  if(playing) myInterpolate();
  image(body,0,0);//先畫身體
  pushMatrix();
    translate(+233,+230);//放回正確位置
    rotate(radians(angleX[0]));
    translate(-233,-230);//頭的旋轉中心放到(0,0)
    image(head,0,0);//在畫頭
  popMatrix();
  pushMatrix();
    translate(220,375);
    rotate(radians(angleX[5]));
    translate(-220,-375);
    image(foot1,0,0);
  popMatrix();
  pushMatrix();
    translate(260,372);
    rotate(radians(angleX[6]));
    translate(-260,-372);
    image(foot2,0,0);
  popMatrix();
  pushMatrix();
    translate(+197,+262);//放回正確位置
    rotate(radians(angleX[1]));
    translate(-197,-262);//arm的旋轉中心放到(0,0)
    image(uparm1,0,0);
    pushMatrix();
      translate(+119,+265);//放回正確位置
      rotate(radians(angleX[2]));
      translate(-119,-265);//arm的旋轉中心放到(0,0)
      image(hand1,0,0);
    popMatrix();
  popMatrix();
   pushMatrix();
    translate(+289,+260);//放回正確位置
    rotate(radians(angleX[3]));
    translate(-289,-260);//arm的旋轉中心放到(0,0)
    image(uparm2,0,0);
    pushMatrix();
      translate(+355,+258);//放回正確位置
      rotate(radians(angleX[4]));
      translate(-355,-258);//arm的旋轉中心放到(0,0)
      image(hand2,0,0);
    popMatrix();
  popMatrix();
  
  
 }





沒有留言:

張貼留言