1 . 製作3D方塊
void setup() { size(400, 400, P3D); //processing 的 3D功能 } void draw() { background(#FFFFF2);//設定背景顏色 pushMatrix();//備份矩陣 translate(mouseX, mouseY);//移動 box(100);//3D的盒子、方塊 popMatrix();//還原矩陣 }
2 . 放大
void setup() { size(600, 600, P3D); //processing 的 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);//雖然是100的盒子但是放大兩倍 popMatrix();//還原矩陣 }
3 . 比較
4 . 資料結構
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的物件
5 . 做出形狀
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(); } void mousePressed() { pt.add( new PVector(mouseX, mouseY) ); }//大的資料結構裡,加入小的PVector的物件
6 . 加入形狀旋轉
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)S { vertex(p.x, p.y); } endShape(); popMatrix(); } void mousePressed() { if (mouseButton==LEFT) { pt.add( new PVector(mouseX, mouseY) ); } }//大的資料結構裡,加入小的PVector的物件
7 . 利用滑鼠改變背景顏色
void setup() { size(400, 400, P3D); } void draw() { background(255); if(mousePressed && mouseButton==LEFT) background(255,0,0); if(mousePressed && mouseButton==RIGHT) background(0,255,0); if(mousePressed && mouseButton==CENTER) background(0,0,255); }
8 . 檢查點點
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) { 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; } } }9 . 隨機點
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; } } } }
10 . 刪除點
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); } } } }
沒有留言:
張貼留言