WEEK05
Step01
打開processing
利用程式做出3D正方體,且跟者滑鼠移動
void setup(){
size(400,400,P3D); } void draw(){ background(#FFFFF2); pushMatrix(); translate(mouseX,mouseY); box(100); popMatrix(); }
把3D正方體旋轉、放大
void setup(){ size(400,400,P3D);//processing的3D功能 } void draw(){ background(#FFFFF2); pushMatrix(); translate(mouseX,mouseY); rotateY(radians(mouseX)); fill(0,255,0); box(100); noFill(); scale(2); box(100); popMatrix(); }
利用程式做出平面三角形,填上彩色
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();
Step02
可以用滑鼠點出小白點
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)); }
讓小白點用線連起來
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)); }
讓線的圖案旋轉起來(按滑鼠右鍵M2旋轉)
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)); } }//大的資料結構裡,加入小的PVector的物件a
Step03
按滑鼠按鍵改變顏色
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(){ if(mouseButton==LEFT) println("LEFT"); if(mouseButton==CENTER) println("CENTER"); if(mouseButton==RIGHT) println("RIGHT"); }
做出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)); }//假設畫面中,點好了20個頂點! } void draw(){ background(255); for(PVector p: pt){//畫出20個頂點 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; } } }
可以隨便在畫面中新增小白點
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))); }//假設畫面中,點好了20個頂點! } void draw(){ background(255); for(PVector p: pt){//畫出20個頂點 ellipse(p.x,p.y,10,10); } if(ans!=null) ellipse(ans.x,ans.y,15,15); } PVector ans =null; void mouseDragged(){ if(mouseButton==ENTER && 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; } } } }
可以隨便M1在畫面中新增小白點,M2刪除小白點
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)) ); }//假設畫面中,點好了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 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
沒有留言:
張貼留言