2017年1月19日 星期四

80108003E_朱芳慶, Week11

2016-11-24 教學軟體設計 week11
l   本周的processing程式,有介紹如何用指令畫圓、讓圓旋轉等。

l   後來還有介紹圓的延伸:車子的停車模擬,有用指令控制輪胎方向,很神奇,感覺如果有個機器車子,放個這樣的程式進去,車子就可以自己跑一跑停車了。
指令:
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;
}

---------------------

void setup() {
  size(640, 480, P3D);
  rectMode(CENTER);
}
float carX=640/2, carY=320/2;
float carDir=0, 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();
}
---------------------

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);
}



沒有留言:

張貼留言