สิงหาคม 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: เติมวงเล็บเพิ่มให้ครบจำนวน

Lab2_battery

int posX=35, posY=30;
float resize=150;      //changeable
float charge=0;
void setup(){
  size(350,250);
  frameRate(15);
}
void draw() {
  background(130,120,240); //light_blue
  batteryTank();
  positive();
  negative();
  stroke(0);
}
void batteryTank(){
  float chargeW;
  fill(255);
  textSize(resize/10);
  text("Battery: "+charge+"%",posX+resize/100*25*2.25,posY+resize/100*125);
  //tank
  strokeWeight(3);
  fill(130); //gray
  rect(posX,posY,resize/100*80*2.25,resize/100*100,resize/100*5*2.25);
  rect(posX+resize/100*80*2.25,posY+resize/100*30,resize/100*3*2.25,resize/100*40);
  fill(150);
  rect(posX+resize/100*2.5*2.25,posY,resize/100*75*2.25,resize/100*100,resize/100*2*2.25);
  //power
  chargeW=charge/100*resize/100*75*2.25;
  fill(#38E82C); //green
  rect(posX+resize/100*2.5*2.25,posY,chargeW,resize/100*100,resize/100*2*2.25);
  charge++;
  charge %= 101;
}
void positive(){
  noStroke();
  fill(#DE1F1F); //red
  rect(posX+1.75*resize,posY+resize,resize/10,resize*0.4);
  rect(posX-75*resize/500+1.75*resize,posY+75*resize/500+resize,resize*0.4,resize/10);
}
void negative(){
  noStroke();
  fill(#1752CE); //blue
  rect(posX-75*resize/500+0.1*resize,posY+75*resize/500+resize,resize*0.4,resize/10);
}

Lab2_positive

int red=255, green=255 ,blue=255;
int posX=0;
void draw() {
  size(300,300);
  background(240,30,35);
  positive();
}
void positive(){
  int resize=100;
  int posY=50;
  noStroke();
  fill(red,green,blue);
  rect(posX-resize*1.25,posY,resize/2,resize*2);
  rect(posX-resize*1.25-75*resize/100,posY+75*resize/100,resize*2,resize/2);
  posX++;
  posX%=resize*5;
}
void mouseMoved(){
  red+=4;
  green+=2;
  blue++;
  red %= 256;
  green %= 256;
  blue %= 256;
}
 
errors: The variable "resize" does not exist
cause: เกิดจากการประกาศตัวแปรผิด function [ใส่ไว้ใน void draw()]
how to fix: ย้ายคำสั่งประกาศตัวแปรมาใส่ใน function ที่มีการใช้ตัวแปรนั้นๆ [ต้องใส่ใน void positive()]

Lab1_song

void setup(){
  int posX=280;
  size(350,185);
  background(#F7DEA7); //colorLight_Brown
  noStroke();
  //draw"THE"
  fill(0); //colorBlack
  rect(posX-25,10,70,30); //blank"THE"
  fill(#F7DEA7);
  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(0);
  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(#F7DEA7); //colorLight_Brown
  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(0);
  rect(155,50,10,97); //R
}