วันอาทิตย์ที่ 7 กันยายน พ.ศ. 2557

Lab 2 - Bicycle

ผมฟังเพลงๆนึงมาครับ เลยเป็นแรงบันดาลใจให้ผมวาดจักรยานคันนี้




void setup()

{
  //Initialization
  size(800,400);
  background(255);
  strokeWeight(1);
  frameRate(60);
}
float spin = 0;
void draw(){
  background(255);
  //variable
  float RefX,RefY;
  float ThickBody;
  float LHand;
  float HBody,LBody;
  float WSit,HSit,LSit;
  //assign
  RefX = mouseX;
  RefY = mouseY;
  ThickBody = 10;
  LHand = 25;
  HBody = 75;
  LBody = 150;
  WSit = 10;
  HSit = 20;
  LSit = 60;
  strokeWeight(ThickBody);
  //draw handle
  stroke(0);
  line(RefX,RefY,RefX+LHand,RefY);
  //draw front
  stroke(255,0,0);
  line(RefX,RefY,RefX-LHand,RefY);
  line(RefX-LHand,RefY,RefX-LHand-(LBody/4),RefY+(2*HBody));
  //draw body
  triangle(RefX-LHand-(LBody/28),RefY+HBody,RefX-LHand-(LBody/28)+LBody,RefY+HBody,RefX-LHand-(LBody/28)+((3*LBody)/4),RefY+(2*HBody));
  triangle(RefX-LHand-(LBody/28)+LBody,RefY+HBody,RefX-LHand-(LBody/28)+((3*LBody)/4),RefY+(2*HBody),RefX-LHand-(LBody/28)+((3*LBody)/4)+((2*LBody)/4),RefY+(2*HBody));
  //draw seat
  stroke(0);
  line(RefX-LHand-(LBody/28)+LBody,RefY+HBody,RefX-LHand-(LBody/28)+LBody+WSit,RefY+HBody-HSit);
  noStroke();
  fill(196,124,0);
  rect(RefX-LHand-(LBody/28)+LBody+WSit-(LSit/2),RefY+HBody-HSit-(LSit/4),LSit,LSit/4);
  noFill();
  stroke(0);
  //draw wheel
  DrawWheel(RefX-LHand-(LBody/28)+((3*LBody)/4)+((2*LBody)/4),RefY+(2*HBody),50);
  DrawWheel(RefX-LHand-(LBody/4),RefY+(2*HBody),50);
  spin = spin+0.5;
}

void DrawWheel(float CenterX,float CenterY,float R){
  int n=12;
  float dTire=8;
  float rad;
  rad = TWO_PI/n;
  //draw tire
  strokeWeight(dTire);
  noFill();
  ellipse(CenterX,CenterY,2*R,2*R);
  strokeWeight(2);
  for(int i=0;i<n;i=i+1){
    line(CenterX,CenterY,CenterX+(R*cos((rad*i)-spin)),CenterY-(R*sin((rad*i)-spin)));
  }
}