วันอาทิตย์ที่ 30 สิงหาคม พ.ศ. 2558

Lab3_Example_Balloon

ตัวอย่าง บอลลูน

void draw() {
  size(ุ600, 300);
  frameRate(20);
  background(random(200), random(200), random(200));//random RGB background
  int x=mouseX;
  int y=100;
  if (x<100) {
    x=100;
    fill(#DD0000);
  } else {
    if (x>500) {
      x=500;
    }
    if (x>100) {
      fill(#0000DD);
    }
    if (x<500) {
      fill(#00DD00);
    }
  }
  draw_balloon(x, y);
}


//balloon funnction
void draw_balloon(int x, int y) {
  int radius = 100;
  int string_length = 150;
  strokeWeight(13);
  line(x, y, x, y + string_length);
  ellipse(x, y, radius, radius);
}

Lab2_อาการ Syntax Error ต่างๆ

SYNTAX ERROR

Syntax error เกิดจากการที่เราเขียนโค้ดไม่ตรงตามหลักการเขียนที่โปรแกรมกำหนดเอาไว้ การเกิด syntax error ที่เกิดขึ้นได้บ่อยๆ มีดังนี้

1.Expecting EOF,found "}" หรือการใส่ปีกกาเกิน
-เกิดจากที่เราใส่ปีกกาเกินตัวฟังก์ชันของมัน
-แก้ไขโดยการลบปีกกาตัวที่เกินออกไป

2.Missing a semicolon ( ; ) หรือการลืมใส่ semicolon ;
- เกิดจากที่เราลืมใส่ semicolon ต่อท้ายฟังก์ชันบางตัว
-แก้ไขว่าลืมใส่ตรงไหน โดยดูที่แถบด้านขวาที่ขึ้นเป็นขีดๆแดงๆ จากนั้นก็ใส่ให้เรียบร้อย

3.Unexpected token: fffff หรือ การไม่พบสัญลักษณ์ที่เกิดขึ้น
-เกิดจากเราเขียนตัวหนังสือลงไปเลย โดยที่ไม่ใช่การคำนวณ ไม่ใช่ฟังก์ชัน ไม่ใช่สัญลักษณ์ของโปรแกรม
-แก้ไขโดยการลบตัวหนังสือนั้นออก

4.Cannot convert from float to int หรือ การที่ไม่สามารถเปลี่ยนชนิด class ได้
-เกิดจากการใช้ class ผิด Syntax ของมัน เช่น คำสั่ง random นั้น syntax ของมันคือ random(float,float) แต่เราใช้ไปเก็บค่าไว้ในตัวแปร int หรือ เรานำข้อมูล float ไปใส่ใน int มันจึงเก็บค่าไม่ได้
-แก้ไข้โดยการเปลี่ยนชนิดตัวแปรที่ไว้เก็บค่า ให้เป็นชนิด float จึงจะเก็บค่าได้

5. The variable "posX" does not exist หรือ การไม่พบตัวแปร
-เกิดจากเราไม่ได้กำหนดตัวแปรนั้น หรือ เราพิมชื่อตัวแปรนั้นผิด
-แก้ไขโดยเช็คชื่อตัวแปรนั้น หรือกำหนดตัวแปรนั้นขึ้นมา

6.การใส่ค่าไม่ครบใน Parameter
-เกิดจากเราใส่ค่าใน Parameter ไม่ครบจำนวนตามที่เรากำหนดไว้ เช่น void Positive(int posX , int posY); เรากำหนดตัว Argument ไว้เป็น posX และ posY เวลาเราเรียกใช้งานฟังก์ชัน เราใส่ Positive(50);  แบบนี้จะขึ้น syntax error เพราะว่าเรากำหนดไว้ให้ใส่ไป 2 ค่า แต่เราใส่แค่ค่าเดียว
-แก้ไขโดยการเช็คตัว Argument ว่าเรากำหนดไว้กี่ค่า จากนั้นก็ใส่ค่า Parameter ให้ครบตามที่เรากำหนดไว้

Lab2_Positive_Sign

Positvie Sign by Function

//Positive Sign
int colors=0;

void setup() {
  size(500, 500);
}
void draw() {
  background(#FF0D0D);//red
  positive();
}
void positive() {
  int dX=120;//diameterX
  int dY=160;//diameterY
  int w=100;//weight
  int h=100;//height
  //rectangular area
  stroke(255);
  strokeWeight(0);
  fill(colors);
  rect(dX, dY, w*3, h);//horizontal rect
  rect(dX+100, dY-100, w, h*3);//vertical rect
}
void mousePressed() {  

  if (colors == 0) {
    colors = 255;
  } else {
    colors = 0;
  }
}

Lab2_Battery_Function

Battery follow the Cursor!!

void setup() {
  size(500, 500);
}
void draw() {
  background(255);//white
  battery(mouseX, mouseY);
}
void battery(int countX, int countY) {
  int space=40;
  int w=180;//width
  int h=90;//height
  stroke(#0041E0);//dark blue
  strokeWeight(8);
  fill(#0393FF);//blue

  //big square
  rect(countX, countY, w, h);
  fill(255);//white
  //batt head
  rect(countX+180, countY+35, w/9, h-70);

  //battery quntity
  fill(#FF0011);//red
  rect(countX, countY, space, h);//#1 batt
  rect(countX+space, countY, space, h);//#2 batt
  rect(countX+space*2, countY, space, h);//#3 batt
  rect(countX+space*3, countY, space, h);//#4 batt
}

Lab2_BMI_Function

BMI's Calculation by Function

//BMI Function
void setup() {
  size(300,300);
  background(#FF8B8B);//pink
  calculation(80,175);
}
void calculation(int w,int h){
  float h_2=(h/100.0)*(h/100.0);//Height^2
  float BMI=w/h_2;//value of BMI
  //terminal output
  print("Value Of BMI="+BMI);
  //explaining text
  textSize(24);//size of text
  text("Weight = "+w+" Kg.",30,100);
  text("Height = "+h+" cm.",30,140);
  text("BMI="+BMI+"",30,180);
}

Lab2_Circle'sCalculation_Function

Circle's Calculation by Function

//Circle's Calculation
int posx=100;
int posy=100;
int dm=160 ;//diameter
void setup() {
  size(500, 500);
  circle(100, 100);
  calculation();
}


void circle(int posx, int posy) {
  background(#FC66C1);//pink <3
  ellipse(posx, posy, dm, dm);//example

}
void calculation() {
  float r=dm/2;//radius
  float r_2=r*r;//radius square
  float A=3.14*r_2;//area
  float cf=2*(3.14)*r;//circumference
  //terminal output
  println("Value of circle's area = "+A);
  print("Value of circumference's circle = "+cf);
  textSize(24);//text size
  text("Circumference's circle = "+cf+" m.", 30, 400);
  text("Area's circle = "+A+" m^2.", 30, 350);
  text("Diameter's circle = "+dm+" m.", 30, 250);
  text("Radius's circle = "+r+" m.", 30, 300);

}

Lab2_Book_Function

Book use Function


//FAV Book Function
//int posX=0; //fill value to move(X)
//int posY=0;//fill value to move(Y)
void setup() {
  size(500, 500);
  background(255);//white
  book(0, 0);
}
void book(int posX, int posY) {//function
  fill(#B3CFF0);//blue
  strokeWeight(8);//thick line
  rect(80+posX, 130+posY, 340, 220);//body of book
  strokeWeight(6);//thin line
  fill(#78B2F7);//dark blue
  rect(230+posX, 130+posY, 40, 220);//spine 
  rect(290+posX, 150+posY, 110, 60);//title's frame
  fill(0);//black
  textSize(30);
  text("Conan", 300+posX, 190+posY);//Title
  strokeWeight(3);//Thinest Line
  line(250+posX, 130+posY, 120+posX, 100+posY);//pages1 r
  line(250+posX, 130+posY, 110+posX, 108+posY);//pages2 r
  line(250+posX, 130+posY, 100+posX, 115+posY);//pages3 r
  line(120+posX, 100+posY, 80+posX, 130+posY);//pages border r
  line(250+posX, 130+posY, 380+posX, 100+posY);//pages1 l
  line(250+posX, 130+posY, 390+posX, 108+posY);//pages2 l
  line(250+posX, 130+posY, 400+posX, 115+posY);//pages3 l
  line(380+posX, 100+posY, 420+posX, 130+posY);//pages border l
}