WEEK05
1.讓畫出來的3D方塊跟著滑鼠移動
void setup(){
size(400,400,P3D);
}
void draw(){
background(#FFFFF2);
pushMatrix();
translate(mouseX,mouseY);
box(100);
popMatrix();
}
2.綠色方塊跟著Y軸轉 再加入一個兩倍大的方塊
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();
}
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();
4.

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));
}
5.點選位置可連起來一個圖形
ArrayList<PVector>pt;
void setup(){
size(400,400,P3D);
pt=new ArrayList<PVector>();
}
void draw(){
pushMatrix();
if(mousePressed && mouseButton==RIGHT){
translate(200,200);
rotate(radians(frameCount));
translate(-200,-200);
}
background(#FFFFF2);
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));
}
pt.add(new PVector(mouseX,mouseY));
}
6.滑鼠點下去會有顏色(左鍵中鍵右鍵顏色不同)
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);
}
7.畫出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;
}
}
}
8.左鍵中鍵要做區隔 右鍵移動 左鍵加點點
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;
}
}
}
}
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)));
}
}
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);
}
}
}
END








沒有留言:
張貼留言