作業1:P3D_translate_rotateY_radians_box
作業2:2D_point_line_rect_ellipse
作業3:想要知道rect做甚麼的
利用mousePressed的函式來實現現在滑鼠點的是甚麼顏色,就畫什麼顏色
//week03-4b_mousePressed_stroke_line
void setup(){
size(500,500);
}
void draw(){
//background(255);//先刪掉
stroke(0);
fill(255,0,0);//紅色
rect(0,0,50,50);
fill(255,255,0);//黃色
rect(0,50,50,50);
fill(0,255,0);//綠色
rect(0,100,50,50);
fill(0,0,255);//藍色
rect(0,150,50,50);
stroke(myStroke);
if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
}
color myStroke;
void mousePressed(){
if(mouseX<50){
if(mouseY<50) myStroke=color(255,0,0);
else if(mouseY<100) myStroke=color(255,255,0);
else if(mouseY<150) myStroke=color(0,255,0);
else if(mouseY<200) myStroke=color(0,0,255);
}
}
作業5:
想要做出像貓抓老鼠的程式
//week03-5_mouse_cat_x_y
void setup(){
size(400,400);
}
float x,y;//貓的座標
void draw(){
background(255);
ellipse(mouseX,mouseY,40,20);//老鼠
ellipse(x,y,40,40);
x=(x*14+mouseX)/15;
y=(y*14+mouseY)/15;
}//新的座標,舊的座標*14,目標*1
作業6:想要做動作內插,先做一顆球球,讓他可以跑到終點
//week03-6_lerp_frameCount
void setup(){
size(400,400);
}
float startX=0,startY=0;
float stopX=400,stopY=400;
void draw(){
ellipse(startX,startY,10,10);
ellipse(stopX,stopY,10,10);
//lerp()可以做內插,要給他0.0-1.0之間的數
float midX=lerp(startX,stopX,frameCount/200.0);
float midY=lerp(startY,stopY,frameCount/200.0);
//frameCount是「第幾個frame」 1小時=60分,1分60秒 1秒=60frame
ellipse(midX,midY,10,10);
}
//week03_07_bezier_curve
size(400,400);
int x1 = 340, x2 = 40, x3 = 360, x4 = 60;
int y1 = 80, y2 = 40, y3 = 360, y4 = 320;
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
bezier(x1, y1, x2, y2, x3, y3, x4, y4); //貝氏曲線
作業8:畫顆球,在上面跑
//week03_08_bezier_equatoin
void setup(){
size(400,400);
}
int x1 = 340, x2 = 40, x3 = 360, x4 = 60;
int y1 = 80, y2 = 40, y3 = 360, y4 = 320;
void draw(){
background(255);
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
bezier(x1, y1, x2, y2, x3, y3, x4, y4); //貝氏曲線
float t = frameCount / 200.0 %1;
float t2 = 1-t;
float x = x1*t2*t2*t2 + 3*x2*t*t2*t2 + 3*x3*t*t*t2 + x4*t*t*t;
float y = y1*t2*t2*t2 + 3*y2*t*t2*t2 + 3*y3*t*t*t2 + y4*t*t*t;
ellipse(x,y,10,10);
}
//week03_08b_bezier_equatoin
void setup(){
size(400,400);
}
int x1 = 120, x2 = 320, x3 = 320, x4 = 120; //只改控制點的座標
int y1 = 80, y2 = 20, y3 = 300, y4 = 300;
void draw(){
background(255);
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
bezier(x1, y1, x2, y2, x3, y3, x4, y4); //貝氏曲線
float t = frameCount / 200.0 %1;
float t2 = 1-t;
float x = x1*t2*t2*t2 + 3*x2*t*t2*t2 + 3*x3*t*t*t2 + x4*t*t*t;
float y = y1*t2*t2*t2 + 3*y2*t*t2*t2 + 3*y3*t*t*t2 + y4*t*t*t;
ellipse(x,y,10,10);
}
//week03_09_3D_random_random_point
float [] x = new float[1000];
float [] y = new float[1000];
void setup(){
size(400,400,P3D);
for(int i=0;i<1000;i++){
x[i] = random(400);
y[i] = random(400);
}
}
void draw(){
background(0); //黑色背景
stroke(255); //白色線條
for(int i=0;i<1000;i++){
point(x[i], y[i]); //現在是2D的點, 還差z座標
}
}
作業10:現在是3D的,移動滑鼠可以看到
//week03_10_3D_random_random_random_point_translate
float [] x = new float[5000];
float [] y = new float[5000];
float [] z = new float[5000];
void setup(){
size(400,400,P3D);
for(int i=0;i<5000;i++){
x[i] = random(400);
y[i] = random(400);
z[i] = random(-400,400);
}
}
void draw(){
background(0); //黑色背景
stroke(255); //白色線條
translate(0, 0, mouseY);
for(int i=0;i<5000;i++){
point(x[i], y[i], z[i]); //現在是3D的點
}
}
作業11:畫一顆自轉的球
//week03_11_3D_sphare_translate_rotateY
void setup(){
size(400,400,P3D);
}
void draw(){
background(128);
translate(mouseX, mouseY);
rotateY(radians(frameCount));
sphere(200);
}
作業12:加上打光
//week03_12_3D_sphare_translate_rotateY_lights
void setup(){
size(400,400,P3D);
}
void draw(){
background(128);
lights(); //加上打光
translate(mouseX, mouseY);
rotateY(radians(frameCount));
sphere(200);
}
作業13:複製一顆一樣的球球,用備份還原的方式
//week03_13_3D_pushMatrix_translate_shpere_popMatrix
void setup(){
size(600,400,P3D);
}
void draw(){
background(128);
lights(); //加上打光
pushMatrix(); //備份矩陣
translate(300,100); //習慣上,要往右再縮
sphere(100);
popMatrix(); //還原矩陣
//要備份矩陣、還原矩陣,就不會出錯
pushMatrix();
translate(100,100);
sphere(100);
popMatrix(); //還原矩陣
}













沒有留言:
張貼留言