2023年10月12日 星期四

jason1230 week05

 1-1

列印3D模型,用P3D還有PushMatrix、popMatrix、mouseX、mouseY來用

void setup(){
  size(400,400,P3D);//3D功能
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    translate(mouseX,mouseY);
    box(100);//3D的方塊
  popMatrix();
}

1-2

增加旋轉和縮放,新增一個小方塊增加顏色並縮小

void setup(){
  size(400,400,P3D);//3D功能
}
void draw(){
  background(#FFFFF2);
  pushMatrix();
    translate(mouseX,mouseY);
    rotateY(radians(mouseX));//對Y軸轉
    fill(0,255,0);
    box(100);//3D的方塊
    
    noFill();
    scale(2);
    box(100);
  popMatrix();
}

1-3

運用點來連成一個三角形,beginSape、endShape,因為endShape最後沒有把最後一條線連上,所以用endShape(CLOSE)
 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(CLOSE);




2-1

ArrayList 大的資料結構,PVector加入小的資料結構

ArrayList<PVector>pt;//大的資料結構ArrayList

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));
}//加入小的資料結構PVector

2-2-1

運用beginShape、endShape,來做連線,前面點擊出現的橢圓拿來當點做連線

ArrayList<PVector>pt;//大的資料結構ArrayList

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(CLOSE);
}
void mousePressed(){
  pt.add(new PVector(mouseX,mouseY));
}//加入小的資料結構PVector

2-2-2

新增滑鼠按左鍵畫圖,按右鍵時化好的圖形會旋轉

ArrayList<PVector>pt;//大的資料結構ArrayList

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(CLOSE);
  popMatrix();
}
void mousePressed(){
  if(mouseButton==LEFT){
    pt.add(new PVector(mouseX,mouseY));
  }
}//加入小的資料結構PVector



3-1


用for迴圈印出20個頂點,讓點擊圓圈後圓圈加粗
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;
    }
  }
}

3-2


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;
      }
    }
  }
}

3-3

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);//刪的時候,會把大資料結構右邊都往左一點
      }
    }
  }
}

沒有留言:

張貼留言