กันยายน 01, 2558

Lab2_book

int wink=1;
void setup(){
  size(300,400);
  frameRate(3);
}
void draw(){
  background(0);  //colorBlack
  draw_book();
}
void draw_book(){
  int posX=155, posY=150;
  wink %= 2;
  //bones
  fill(255);  //colorWhite
  noStroke();
  beginShape();
  vertex(posX-90,posY-110);
  vertex(posX+110,posY+90);
  vertex(posX+110,posY+110);
  vertex(posX+90,posY+110);
  vertex(posX-110,posY-90);
  vertex(posX-110,posY-110);
  endShape();
  beginShape();
  vertex(posX+90,posY-110);
  vertex(posX-110,posY+90);
  vertex(posX-110,posY+110);
  vertex(posX-90,posY+110);
  vertex(posX+110,posY-90);
  vertex(posX+110,posY-110);
  endShape();
  ellipse(posX-100,posY-120,30,30);
  ellipse(posX-120,posY-100,30,30);
  ellipse(posX+100,posY-120,30,30);
  ellipse(posX+120,posY-100,30,30);
  ellipse(posX+120,posY+100,30,30);
  ellipse(posX+100,posY+120,30,30);
  ellipse(posX-100,posY+120,30,30);
  ellipse(posX-120,posY+100,30,30);
  //skull & hat
  stroke(0);
  strokeWeight(4);
  ellipse(posX,posY+90,80,90);
  fill(145,55,25);  //colorBrown
  arc(posX,posY,170,170,PI+PI/16,TWO_PI-PI/16);
  fill(255,210,50); //colorYellow
  arc(posX,posY,170,170,PI+PI/7,TWO_PI-PI/7);
  arc(posX,posY+30,320,115,PI+PI/5,TWO_PI-PI/5);
  fill(255); //colorWhite
  arc(posX,posY,170,170,0,PI);
  arc(posX,posY,170,30,PI,TWO_PI);
  //detail
  fill(0);
  ellipse(posX,posY+60,20,20); //nose
  ellipse(posX+35,posY+25,50,50*wink); //R-eye
  ellipse(posX-35,posY+25,50,50*wink); //L-eye
  noFill();
  arc(posX,posY,195,195,HALF_PI-PI/8,HALF_PI+PI/8);
  arc(posX,posY,220,220,HALF_PI-PI/8+PI/48,HALF_PI+PI/8-PI/48);
  line(posX-20,posY+85,posX-23,posY+105);
  line(posX,posY+85,posX,posY+110);
  line(posX+20,posY+85,posX+23,posY+105);
  //book
  strokeWeight(3);
  stroke(100); //lncolorGray
  arc(5,400,16,50,HALF_PI,3*PI/2);
  line(8,0,8,375);
  line(8,375,300,375);
  line(7,380,300,380);
  line(6,385,300,385);
  line(5,390,300,390);
  line(4,395,300,395);
  wink++;
}

Lab2_movie

int posX=100,posY=100;
int speed=1,assist=1;
void draw(){
  size(250,250);
  background(#FA2B2B); //red
  draw_movie();
}
void draw_movie(){
  strokeWeight(3);
  stroke(100); //lnColorGray
  fill(0); //colorBlack
  quad(posX-95,posY+115,posX+145,posY+115,posX+109,posY+19,posX-59,posY+19); //wing
  arc(posX+25,posY-90,550,455,HALF_PI-PI/7,HALF_PI+PI/7); //wing
  quad(posX-53,posY+15,posX+103,posY+15,posX+75,posY+125,posX-25,posY+125); //face
  arc(posX+25,posY-10,160,160,PI-PI/8,TWO_PI+PI/8); //head
  quad(posX+37,posY+21,posX+90,posY+21,posX+145,posY+115,posX+112,posY+15);
  quad(posX+13,posY+21,posX-40,posY+21,posX-95,posY+115,posX-63,posY+15);
  arc(posX+75,posY+20,75,60,PI,TWO_PI); //R-eyebrow
  arc(posX-25,posY+20,75,60,PI,TWO_PI);  //L-eyebrow
  fill(255); //colorWhite
  ellipse(posX+55,posY+45,70,35); //R-eye
  ellipse(posX-5,posY+45,70,35); //L-eye
  fill(0);
  triangle(posX-25,posY+125,posX+75,posY+125,posX+25,posY+65); //mouth
  stroke(255); //lnColorWhite
  line(posX-9,posY+108,posX-9,posY+125);
  line(posX+8,posY+88,posX+8,posY+125);
  line(posX+25,posY+80,posX+25,posY+125);
  line(posX+42,posY+88,posX+42,posY+125);
  line(posX+59,posY+108,posX+59,posY+125);
  stroke(100);
  fill(255);
  rect(posX+13,posY+65,25,15); //nose
  fill(0);
  rect(posX+15,posY-92,20,145);
  quad(posX+15,posY+53,posX+35,posY+53,posX+38,posY+65,posX+13,posY+65);
  arc(posX+25,posY+125,100,40,0,PI); //chin
  fill(255);
  ellipse(posX+70,posY+125,10,10);
  ellipse(posX-20,posY+125,10,10);
  posX+=speed;
  posX %= 250;
}
void mouseClicked(){
  assist*=(-1);
  speed+=assist;
}

Lab2_song

int limit=0;
int white=255;
int light_brown=#F7DEA7;
color screen;
color line=255;
void draw(){
  size(350,185);
  background(screen);
  limit++;
  limit %= 180;
  draw_song();
}
void draw_song(){
  int posX=100;
  posX+=limit;
  noStroke();
  //draw"THE"
  fill(line);
  rect(posX-25,10,70,30); //blank"THE"
  fill(screen);
  rect(posX-25,15,7,25);
  rect(posX-13,15,7,25); //T
  rect(posX,10,15,12);
  rect(posX,27,15,13); //H
  rect(posX+20,10,3,30);
  rect(posX+28,15,17,7);
  rect(posX+28,27,17,8); //E
  triangle(posX+33,10,posX+45,45,posX+45,10);
  //draw"SCRIPT"
  fill(line);
  arc(45,75,60,60,HALF_PI,TWO_PI);
  arc(45,131,70,70,HALF_PI+PI,TWO_PI+HALF_PI); //S
  arc(130,105,110,110,QUARTER_PI+PI/32,TWO_PI-QUARTER_PI-PI/32); //C
  rect(165,50,20,10);
  rect(165,105,20,10);
  arc(185,82.5,60,65,3*PI/2,5*PI/2);
  quad(170,115,185,115,215,147,215,163); //R
  rect(220,50,10,120); //I
  rect(235,50,10,120);
  rect(245,50,20,10);
  rect(245,105,20,10);
  arc(265,82.5,60,65,3*PI/2,5*PI/2); //P
  quad(265,50,330,50,335,60,265,60);
  rect(300,50,10,120); //T
  fill(screen);
  arc(45,76,40,40,HALF_PI,TWO_PI);
  arc(45,131,50,50,HALF_PI+PI,TWO_PI+HALF_PI); //S
  arc(130,105,90,90,QUARTER_PI,TWO_PI-QUARTER_PI); //C
  triangle(147,50,155,80,155,50);
  arc(185,82.5,40,45,3*PI/2,5*PI/2); //R
  quad(220,65,230,60,230,65,220,70); //I
  arc(265,82.5,40,45,3*PI/2,5*PI/2); //P
  triangle(220,170,310,130,310,170);
  fill(line);
  rect(155,50,10,97); //R
}
void mouseClicked(){
  //line
  white*=-1;
  line += white;
  //screen
  screen += light_brown;
  light_brown*=-1;
}

สิงหาคม 31, 2558

Lab2_Generic exercise

/*Calculate body mass index (BMI)*/
void setup(){
  int weight= 60;  //weight(kg)
  int tall= 175;   //height(cm)
  float BMI=bodyMassCal(weight,tall);  //body mass index
  println("Weight = " + weight);
  println("Height = " + tall);
  println("BMI = " + BMI);
}
float bodyMassCal(float w,float h){
  float result;
  result = w/((h/100)*(h/100));
  return result;
}

/*Calculate circumference and area of a circle from its diameter*/
void setup(){
  int diam=10;  //diameter value
  float circum = circumCal(diam);
  float areaCir = areaCal(diam);
  println("Diameter = " + diam);
  println("Circumference = " + circum);
  println("area of a circle = " + areaCir);
}
float circumCal(float d){
  float result;
  result = 2*PI*(d/2);
  return result;
}
float areaCal(float d){
  float result;
  result=PI*(d/2)*(d/2);
  return result;
}

สิงหาคม 30, 2558

Lab2_clock

void draw(){
  int posX=width/2;
  int posY=height/2;
  float s;
  float m;
  float h;
  size(220,220);
  background(30);
  //clock
  strokeWeight(2);
  fill(#22ED48); //colorGreen
  ellipse(posX,posY,200,200);
  fill(255);
  ellipse(posX,posY,180,180);
  fill(0);
  ellipse(posX,posY-75,10,10);
  ellipse(posX+75,posY,10,10);
  ellipse(posX,posY+75,10,10);
  ellipse(posX-75,posY,10,10);
  //pin
  s=radians(second()*6);
  m=radians(minute()*6);
  h=radians(hour()*30);
  strokeWeight(1);
  line(posX,posY,posX+(70*sin(s)),posY-(70*cos(s)));
  strokeWeight(2);
  line(posX,posY,posX+(50*sin(m)),posY-(50*cos(m)));
  strokeWeight(4);
  line(posX,posY,posX+(25*sin(h)),posY-(24*cos(h)));
}

 
errors: Missing right parenthesis ")"
cause: เกิดจากการใส่วงเล็บปิดไม่ครบ
how to fix: เติมวงเล็บเพิ่มให้ครบจำนวน