week 11 教學軟體筆記 呂郁欣
- 畫盤子
- 旋轉盤子
- Car
- Car parking
_____________________________________________________________________________________________
1.畫盤子
PVector [] dish=new PVector[4];
void setup (){
size(640,480);
dish[0]=new PVector(100,100);
dish[1]=new PVector(540,100);
dish[2]=new PVector(100,380);
dish[3]=new PVector(540,380);
}
void draw (){
background(255);
ellipse(320,240,400,400);//rotating circle
ellipse(100,100,100,100);
ellipse(540,100,100,100);
ellipse(100,380,100,100);
ellipse(540,380,100,100);
for(int i=0;i<4;i++){
ellipse(dish[i].x,dish[i].y,90,90);
}
}
int nowDish=-1;
void mousePressed(){
for(int i=0;i<4;i++){
nowDish=i;
}
}
}
void mouseDragged(){
if(nowDish!= -1){
dish[nowDish].x=mouseX;
dish[nowDish].y=mouseY;
}
}
void mouseReleased(){
nowDish= -1;
}
2.旋轉盤子
PVector [] dish=new PVector[4];
float [] r = {0, 0, 0, 0};
float [] angle = {0, 0, 0, 0};
void setup() {
size(640, 480);
dish[0]=new PVector(100, 100);
dish[1]=new PVector(540, 100);
dish[2]=new PVector(100, 380);
dish[3]=new PVector(540, 380);
}
void draw() {
background(255);
ellipse(320, 240, 400, 400);//rotating circle
ellipse(100, 100, 100, 100);
ellipse(540, 100, 100, 100);
ellipse(100, 380, 100, 100);
ellipse(540, 380, 100, 100);
for (int i=0; i<4; i++) {
ellipse(dish[i].x, dish[i].y, 90, 90);
line(dish[i].x, dish[i].y, 320, 240);
}
if (keyPressed) {
for (int i=0; i<4; i++) {
angle[i] -= 0.01;
dish[i].x= r[i] * cos(angle[i]) + 320;
dish[i].y= r[i] * sin(angle[i]) + 240;
}
}
}
int nowDish=-1;
void mousePressed() {
for (int i=0; i<4; i++) {
if ( dist(dish[i].x, dish[i].y, mouseX, mouseY) <45 ) {
nowDish=i;
}
}
}
void mouseDragged() {
if ( nowDish!= -1) {
dish[nowDish].x=mouseX;
dish[nowDish].y=mouseY;
r[nowDish] = sqrt( (mouseX-320)*(mouseX-320) + (mouseY-240)*(mouseY-240) );
angle[nowDish] = atan2(mouseY-240, mouseX-320);
}
}
void mouseReleased() {
nowDish = -1;
}
3.Car parking
void setup() {
size(640, 480, P3D);
rectMode(CENTER);
}
float carX=640/2, carY=320/2;
float carDir=PI, carWheel=0;
void drawCar() {
pushMatrix();
translate(carX, carY);
rotateZ(carDir);
rect(0, 0, 187, 89);
drawWheel(127/2, 89/2, 1);
drawWheel(127/2, -89/2, 1);
drawWheel(-127/2, 89/2, 0);
drawWheel(-127/2, -89/2, 0);
if (keyPressed && keyCode==UP) {
carX+= cos(carDir+carWheel);
carY+=sin(carDir+carWheel);
carDir+=carWheel/50;
} else if (keyPressed && keyCode==DOWN) {
carX-= cos(carDir+carWheel);
carY-=sin(carDir+carWheel);
carDir-=carWheel/50;
} else if (keyPressed && keyCode==RIGHT) {
carWheel+=0.01;
if (carWheel > 0.3) carWheel=0.3;
} else if (keyPressed && keyCode==LEFT) {
carWheel-=0.01;
if (carWheel < -0.3) carWheel=-0.3;
}
popMatrix();
}
void drawWheel(float x, float y, int front) {
pushMatrix();
translate(x, y);
if (front==1) rotateZ(carWheel);
rect(0, 0, 40, 20);
popMatrix();
}
void draw() {
background(100);
drawCar();
rect(0+187/2, 0+89/2, 187, 89);
rect(450+187/2, 0+89/2, 187, 89);
}
沒有留言:
張貼留言