2023年10月12日 星期四

LS._. Week05

P3D-1

  •  下載最新版Processing

  • 加上程式碼
  • P3D為Processing的3D功能
  • 做出一個box

  • 加上push/pop
  • 寫出程式讓滑鼠可以移動box
  • 程式碼
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    translate(mouseX,mouseY);
    box(100);
  popMatrix();
}

P3D-2

  • 備份剛剛程式碼
  • 加上rotate(對Z軸轉)
  • 改為rotateY(對Y軸轉)
  • 加上fill填充顏色
  • 加上一個大box,fill不填充顏色
  • 程式碼
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    translate(mouseX,mouseY);
    rotateY(radians(mouseX));
    fill(0,255,0);
    box(100);
    
    noFill();
    scale(2);
    box(100);
  popMatrix();
}

P3D-3

  • 加上程式碼做出頂點
  • 畫出彩色
  • 做出不同執行畫面
  • 將線畫好
  • 程式碼
size(410,410,P3D);
beginShape();
fill(255,0,0);  vertex(200,0);
fill(0,255,0);  vertex(0,400);
fill(0,0,255);  vertex(400,400);
endShape();
  • 程式碼2
size(410,410,P3D);
noFill();
beginShape();
vertex(200,0);
vertex(0,400);
vertex(400,400);
endShape(CLOSE);

P3D摺紙-1

  • 打上程式碼,讓滑鼠點擊的地方出現圓圈
  • ArrayList為大的資料結構
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
}
void draw(){
  background(#FFFFF2);
  for(PVector p:pt){
    ellipse(p.x,p.y,10,10);
  }
}
void mousePressed(){
  pt.add(new PVector(mouseX,mouseY));
}

P3D摺紙-2

  • 修改程式碼,讓程式自動連線
  • 修改程式碼,讓程式連線的好一點(三頂點後開始連線)
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
}
void draw(){
  background(#FFFFF2);
  for(PVector p:pt){
    ellipse(p.x,p.y,10,10);
  }
  beginShape();
  for(PVector p:pt){
    vertex(p.x,p.y);
  }
  endShape();
}
void mousePressed(){
  pt.add(new PVector(mouseX,mouseY));
}

P3D摺紙-3

  • 修改程式碼
  • 左鍵標點
  • 右鍵旋轉
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    if(mousePressed&&mouseButton==RIGHT){
      translate(200,200);
      rotateY(radians(frameCount));
      translate(-200,-200);
    }
    for(PVector p:pt){
      ellipse(p.x,p.y,10,10);
    }
    beginShape();
    for(PVector p:pt){
      vertex(p.x,p.y);
    }
    endShape();
  popMatrix();
}
void mousePressed(){
  if(mouseButton==LEFT){
    pt.add(new PVector(mouseX,mouseY));
  }
}

P3D摺紙-4

  • 修改程式碼,使滑鼠點左右中鍵會是不同顏色
  • 加上程式碼,讓滑鼠點按時執行下面的文字輸出
  • 程式碼
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(255);
  if(mousePressed&&mouseButton==LEFT)background(255,0,0);
  if(mousePressed&&mouseButton==CENTER)background(0,255,0);
  if(mousePressed&&mouseButton==RIGHT)background(0,0,255);
}
void mousePressed(){
  background(255);
  if(mouseButton==LEFT) println("LEFT");
  if(mouseButton==CENTER)println("CENTER");
  if(mouseButton==RIGHT)println("RIGHT");
}

P3D摺紙-5

  • 修改程式碼,使滑鼠點擊圈圈會選起來
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
  for(int i=0;i<20;i++){
    pt.add(new PVector(i*20,20));
  }
}
void draw(){
  background(255);
  for(PVector p:pt){
    ellipse(p.x,p.y,10,10);
  }
  if(ans!=null)ellipse(ans.x,ans.y,15,15);  
}
PVector ans=null;
void mousePressed(){
  for(PVector p:pt){
    if(dist(p.x,p.y,mouseX,mouseY)<5){
      ans=p;
    }
  }
}

P3D摺紙-6

  • 修改程式碼,使滑鼠左鍵可以製作頂點
  • 滑鼠中鍵可以拖曳頂點
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
  for(int i=0;i<37;i++){
    pt.add(new PVector(random(400),random(400)));
  }
}
void draw(){
  background(255);
  for(PVector p:pt){
    ellipse(p.x,p.y,10,10);
  }
  if(ans!=null)ellipse(ans.x,ans.y,15,15);  
}
PVector ans=null;
void mouseDragged(){
  if(mouseButton==CENTER&&ans!=null){
    ans.x=mouseX;
    ans.y=mouseY;
  }
}
void mousePressed(){
  if(mouseButton==LEFT)pt.add(new PVector(mouseX,mouseY));
  else if(mouseButton==CENTER)
  for(PVector p:pt){
    if(dist(p.x,p.y,mouseX,mouseY)<5){
      ans=p;
    }
  }
}

P3D摺紙-7

  • 修改程式碼,使滑鼠右鍵可以消失
  • 程式碼
ArrayList<PVector>pt;
void setup(){
  size(400,400,P3D);
  pt=new ArrayList<PVector>();
  for(int i=0;i<37;i++){
    pt.add(new PVector(random(400),random(400)));
  }
}
void draw(){
  background(255);
  for(PVector p:pt){
    ellipse(p.x,p.y,10,10);
  }
  if(ans!=null)ellipse(ans.x,ans.y,15,15);  
}
PVector ans=null;
void mouseDragged(){
  if(mouseButton==CENTER&&ans!=null){
    ans.x=mouseX;
    ans.y=mouseY;
  }
}
void mousePressed(){
  if(mouseButton==LEFT)pt.add(new PVector(mouseX,mouseY));
  else if(mouseButton==CENTER)
    for(PVector p:pt){
      if(dist(p.x,p.y,mouseX,mouseY)<5){
        ans=p;
      }
    }
  
  else if(mouseButton==RIGHT){
    for(int i=0;i<pt.size();i++){
      PVector p=pt.get(i);
      if(dist(p.x,p.y,mouseX,mouseY)<5){
        pt.remove(i);
      }
    }
  }
}

檔案上傳

  • 將程式上傳至GitHub


沒有留言:

張貼留言