2023年10月12日 星期四

week05 Ray

 

WEEK 5


step 1-1 做一個會跟著滑鼠動的立體方形




step 1-2





void setup(){
  size(400,400,P3D);//Prossessing的3D功能
}
void draw(){
  background(#FFFFF2);
  pushMatrix();//備份矩陣
    translate(mouseX,mouseY);//移動
    rotateY(radians(mouseX));//對Y軸旋轉
    fill(0,255,0);//面填成綠色
    box(100);//3D的盒子
    
    noFill();//面不要填
    scale(2);//放大2倍
    box(100);//雖是100的盒子,但上面有放大,他就放大了
   popMatrix();//還原矩陣
}

step 2-1


size(400,400,P3D);
beginShape();
fill(255,0,0);vertex(200,0);
fill(0,255,0);vertex(0,400);
fill(0,0,255);vertex(400,400);
endShape();


step 2-2


size(400,400,P3D);
noFill();
beginShape();
vertex(200,0);
vertex(0,400);
vertex(400,400);
endShape(CLOSE);

step 3-1

step 3-2


step 3-3


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){//特殊的for迴圈,得小資料結構
      vertex(p.x,p.y);
    }  
    endShape();
  popMatrix();
}
void mousePressed(){
  if(mouseButton==LEFT){
    pt.add( new PVector(mouseX,mouseY));
  }
}//大的資料結構裡,加入小的PVictor的物件

step 4-1


step 5-1

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




沒有留言:

張貼留言