วันพุธที่ 16 ธันวาคม พ.ศ. 2558

Assignment 2

int []check={0, 0, 0};
Acontroller c;
color []status = {color(255), color(255), color(255), color(255), color(255)};
int run = 0;
int countFrame = 0;
void setup() {
  size(700, 380);
  Amaker m = new Amaker (18, 10);
  Aplayer p = new Aplayer (m);
  Aviewer v = new Aviewer (m);
  c = new Acontroller(m, v, p);
}
void draw() {
  c.window();
  c.button();
  stroke(0);
}
class Amaker {
  int w, h, tile, wide, count;
  String [][] size;
  String [][][] frame;
  Amaker(int width_x, int height_y) {  
    w = width_x;
    h = height_y;
    tile = 0;
    count = 0;
    wide = 30;
    size = new String [h][w];
    frame = new String [100][h][w];
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        size[i][j] = "#";
      }
    }
  }

  void add_tile(int x, int y) {
    size[y][x] = "0";
    tile = tile+1;
  }
  void remove_tile(int x, int y) {
    size[y][x] = "#";
    tile = tile-1;
  }

  void remove_all_tile() {
    for (int y = 0; y<h; y++) {
      for (int x = 0; x<w; x++) {
        size[y][x] = "#";
        tile = 0;
      }
    }
  }

  void move_tile(int x1, int y1, int x2, int y2) {
    if (size[y1][x1] == "0") {
      size[y1][x1] = "#";
      size[y2][x2] = "1";
    }
  }
  void add_frame() {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        frame[count][i][j] = size[i][j];
        if (size[i][j] == "1") {
          size[i][j] = "0";
        }
      }
    }
    count = count+1;
  }
  void remove_frame() {
    if (count <= 0) {
      count = 0;
    } else {
      count=count-1;
    }
    if (count>0) {
      for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
          size[i][j]=frame[count-1][i][j];
        }
      }
    }
  }
  int get_total_num_tile() {
    int num = 0;
    for (int y = 0; y<h; y++) {
      for (int x = 0; x<w; x++) {
        if (size[y][x] == "0" || size[y][x] == "1") {
          num = num+1;
        }
      }
    }
    return num;
  }
  int get_total_num_frame() {
    return count;
  }
  int get_width() {
    return w;
  }
  int get_height() {
    return h;
  }
  String [][] get_size() {
    return size;
  }
  int get_wide() {
    return wide;
  }
  String [][][] get_frame() {
    return frame;
  }
}
class Aviewer {
  int w, h, wide;
  String [][] size = new String [h][w];
  Amaker m;
  Aviewer(Amaker make) {
    m = make;
    w = m.get_width();
    h = m.get_height();
    size = m.get_size();
    wide = m.get_wide();
  }
  void display(int x, int y) {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        if (size[i][j] == "0" || size[i][j] == "1") {
          fill(255);
        } else if (size[i][j] == "#") {
          fill(156, 145, 135);
        }
        rect(x+(j*wide), y+(i*wide), wide, wide);
      }
    }
  }
}
class Acontroller {
  int w, h, wide, x1, x2, y1, y2;
  int posX = 20;
  int posY = 20;
  int posText = 425;
  int move = 0;
  String [][] size = new String [w][h];
  int mode = 0;
  Amaker m;
  Aviewer v;
  Aplayer p;
  Acontroller(Amaker make, Aviewer view, Aplayer play) {
    m = make;
    v = view;
    p = play;
    w = make.get_width();
    h = make.get_height();
    size = m.get_size();
    wide = m.get_wide();
  }
  void mouse_position() {
    //add frame
    if (positionMouseClick(575, 625, 245, 295)) {
      m.add_frame();
    }
    //remove frame
    if (positionMouseClick(575, 625, 310, 360)) {
      m.remove_frame();
    }
    //remove tile
    if (mouseButton == LEFT && positionMouseClick(575, 625, 95, 135)) {
      m.remove_all_tile();
    }
    //play
    if (mousePressed && positionMouseClick(430, 470, 330, 370) && m.get_total_num_frame() != 0) {
      run = 1;
    }
    //stop
    if (mousePressed && positionMouseClick(500, 540, 330, 370)) {
      run = 0;
      countFrame = 0;
    }
  }
  void window() {
    background(100);
    textAlign(CENTER);
    for (int y = 0; y < m.get_height(); y++) {
      for (int x = 0; x < m.get_width(); x++) {
        if (mousePressed && positionMouseClick(575, 625, 20, 70)) {
          mode = 1;
          check[0] = 1;
          check[1] = 0;
          check[2] = 0;
        } else if (mousePressed && positionMouseClick(575, 625, 95, 135)) {
          check[1] = 1;
          check[0] = 0;
          check[2] = 0;
        } else if (mousePressed && positionMouseClick(575, 625, 150, 200)) {
          check[2] = 1;
          check[1] = 0;
          check[0] = 0;
        }

        for (int i=0; i<3; i++) {
          if (check[i] == 1) {
            status[i] = #00FF00;
          } else {
            status[i] = 255;
          }
        }
        //add tile
        if (mousePressed && mouseButton == LEFT && run == 0 && check[0] ==1  && positionMouseClick(575,625,20,70)) {
          m.add_tile(x, y);
        }
        //remove tile
        else if (mousePressed && mouseButton == RIGHT && run == 0 && check[0] ==1 && positionMouseClick(575,625,95,135)) {
          m.remove_tile(x, y);
        }
        //move tile
        if (mousePressed && mouseButton == LEFT && run == 0 && check[2] ==1  && positionMouseClick(575,625,150,200)) {
          if (move == 0 && size[y][x] == "0") {
            x1 = x;
            y1 = y;
            move = 1;
          }
          if (move == 1) {
            x2 = x;
            y2 = y;
            if ((abs(y2-y1) == 1 && x2 == x1) || (abs(x2-x1) == 1 && y2 == y1) && size[y][x] != "0" && size[y][x] != "1") {
              m.move_tile(x1, y1, x2, y2);
              move = 0;
            }
          }
        }
      }
    }
    if (mousePressed && positionMouseClick(380, 505, 450, 490)) {
      status[3] = #00FF00;
    } else status[3]= 255;
    if (mousePressed && positionMouseClick(505, 630, 450, 490)) {
      status[4] = #00FF00;
    } else status[4]= 255;
    if (run == 0) {
      v.display(posX, posY);
      frameRate(120);
    } else {
      p.play_frame(posX, posY);
      frameRate(5);
    }
    if (mode == 0) {
      textSize(30);
    }  
    fill(0);
    textSize(12);
    text("add\ntile", 650, 40);
    text("remove\ntile", 650, 105);
    text("move\ntile", 650, 170);
    text("add\nframe", 650, 265);
    text("remove\nframe", 650, 330);
    textSize(15);
    fill(0);
    text("Tile(s) : "+m.get_total_num_tile(), 60, 350);
    text("Frame : "+m.get_total_num_frame(), 250, 350);
  }
  void button() {
    noStroke();
    //play
    fill(255);
    ellipse(450, 350, 40, 40);
    fill(#00FF00);
    triangle(440, 340, 440, 360, 460, 350);
    //stop
    fill(255);
    ellipse(520, 350, 40, 40);
    fill(#FF0000);
    rect(510, 340, 20, 20);
    fill(status[0]);
    ellipse(600, 45, 50, 50);  //add tile
    fill(status[1]);
    ellipse(600, 110, 50, 50); //remove tile
    fill(status[2]);
    ellipse(600, 175, 50, 50); //move tile
    fill(status[3]);
    ellipse(600, 270, 50, 50); //add frame
    fill(status[4]);
    ellipse(600, 335, 50, 50); //remove frame
    fill(0);
    //add tile
    rect(592.5, 25, 15, 40);
    rect(580, 37.5, 40, 15);
    //remove tile
    quad(580,100,590,90,620,120,610,130);
    quad(590,130,580,120,610,90,620,100);
    //move tile
    triangle(585,160,595,160,585,170);
    triangle(605,160,615,160,615,170);
    triangle(615,180,615,190,605,190);
    triangle(595,190,585,190,585,180);
    stroke(5);
    line(590,165,610,185);
    line(610,165,590,185);
    //add frame
    rect(592.5, 250, 15, 40);
    rect(580, 262.5, 40, 15);
    //remove frame
    quad(580,325,590,315,620,345,610,355);
    quad(590,355,580,345,610,315,620,325);
  }
}
class Aplayer {
  int w, h, wide;
  int posX = 215;
  int posY = 75;
  String [][] size = new String [w][h];
  String [][][] frame = new String [100][w][h];
  Amaker m;
  Aplayer(Amaker make) {
    m = make;
    w = m.get_width();
    h = m.get_height();
    size = m.get_size();
    wide = m.get_wide();
    frame = m.get_frame();
  }
  void play_frame(int x, int y) {
    for (int i = 0; i<h; i++) {
      for (int j = 0; j<w; j++) {
        if (frame[countFrame][i][j] == "0" || frame[countFrame][i][j] == "1") {
          fill(255);
        } else if (frame[countFrame][i][j] == "#") {
          fill(156, 145, 135);
        }
        rect(x+(j*wide), y+(i*wide), wide, wide);
      }
    }
    countFrame++;
    countFrame = countFrame%m.get_total_num_frame();
  }
}

boolean positionMouseClick(int morethanPosX, int lessthanPosX, int morethanPosY, int lessthanPosY) {
  boolean check = (mouseX >= morethanPosX && mouseX <= lessthanPosX && mouseY >= morethanPosY && mouseY <= lessthanPosY);
  return check;
}
void mousePressed() {
  c.mouse_position();
}

วันพุธที่ 25 พฤศจิกายน พ.ศ. 2558

Lab rasberry pi : class Student

class Student:
    def __init__(self,name,ID,score):
        self.name = name
        self.ID = ID
        self.score = score
    def get_score(self):
        return self.score
    def display(self):
        print('Name : ',self.name)
        print('ID : ',self.ID)
        print('Score : ',self.score)
    def set_score(self):
        return self.score
def setup():
    stu = [Student('A',123,69),
           Student('B',456,80),
           Student('C',789,18),
           Student('D',753,59),
           Student('E',951,27)]
    show_all_grade(stu)
   
def find_grade(stu):
    if(stu.get_score()>=80):
        return 'A'
    elif(stu.get_score()>=70 and stu.get_score()<80):
        return 'B'
    elif(stu.get_score()>=60 and stu.get_score()<70):
        return 'C'
    elif(stu.get_score()>=50 and stu.get_score()<60):
        return 'D'
    else:
        return 'F'

def count_grade(stu):
    n = 0
    i = 0
    s = stu[i].get_score()
    while(i<len(stu)):
        if(s>=80):
            n =n+1
        elif(s>=70 and s<80):
            n=n+1
        elif(s>=60 and s<70):
            n=n+1
        elif(s>=50 and s<60):
            n=n+1
        else:
            n=n+1
        i=i+1
    return n

def show_all_grade(stu):
   i = 0
   while(i<len(stu)):
       stu[i].display()
       print("Grade :",find_grade(stu[i]))
       print()
       i=i+1
setup()

วันอาทิตย์ที่ 25 ตุลาคม พ.ศ. 2558

Lab 6 : Multiply two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   multiply_matrix(matrix_1,matrix_2)
 
def multiply_matrix(matrix_1,matrix_2):
   i = 0
   i_i = 0
   k_i = 0
   while(i_i<len(matrix_1)):
      j = 0
      i_j = 0
      k_j = 0
      print("|",end="")
      while(i_j<len(matrix_2[i])):
         total_1=matrix_1[i+k_i][j]*matrix_2[i][j+k_j]
         total_2=matrix_1[i+k_i][j+1]*matrix_2[i+1][j+k_j]
         total_3=matrix_1[i+k_i][j+2]*matrix_2[i+2][j+k_j]
         print("",total_1+total_2+total_3,end="")
         k_j = k_j+1
         i_j = i_j+1
      print("|")
      k_i = k_i+1
      i_i = i_i+1
     
setup()

Lab 6 : Subtract two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]-matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Add two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]+matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Display the matrix

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   display_matrix(matrix_1)
 
def display_matrix(matrix_1):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Find indices of the room(s) with maximum number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   num = building_1[0][0]
   for i in range(0, len(building_1)):
      i_room = 0
      for i in range(0,len(building_1[i_floor])):
         if(building_1[i_floor][i_room] > num):
            num = building_1[i_floor][i_room]
         i_room = i_room+1
      i_floor = i_floor+1
   i_floor = 0
   for i in range(i_floor,len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         if(num==building_1[i_floor][i_room]):
            print("indices of the room(s) with maximum number of chairs :","[",i_floor,"][",i_room,"]")
         i_room = i_room+1
      i_floor = i_floor+1
     
setup()

Lab 6 : Find index of the floor(s) with maximum number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   total = 0
   num = 0
   i_max = 0
   for i in range(i_floor, len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         total = total+building_1[i_floor][i_room]
         i_room = i_room+1
      if(total>num):
         num = total
         i_max = i_floor
      i_floor = i_floor+1
   print("index of the floor(s) with maximum number of chairs :",i_max)
 
setup()

Lab 6 : Find total number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   total = 0
   for i in range(i_floor, len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         total = total+building_1[i_floor][i_room]
         i_room = i_room+1
      i_floor = i_floor+1
   print("total number of chairs (in the building) :",total)
 
setup()

Lab 6 : Find/count number of students with weight < 50

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with BMI < 50 :",count_weight(weights))

def count_weight(weight):
   i = 0
   num = 0
   for i in range(0, len(weight)):
      if(weight[i]<50):
         num = num+1
   return num

setup()

Lab 6 : Find minimum weight of students

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("minimum weight of students :",min(weights))
 
setup()

lab 6 : Find/count number of students with age < 30

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with age < 30 :",count_age(ages))

def count_age(age):
   i = 0
   num = 0
   for i in range(0, len(age)):
      if(age[i]<30):
         num = num+1
   return num

setup()

lab 6 : Find average age of students

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("average age of students :",find_average(ages))

def find_average(age):
   i = 0
   sum = 0
   for i in range(0, len(age)):
      sum = sum + age[i]
      aver = sum/len(age)
   return aver

setup()

Lab 6 : Display student records with BMI > 25

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   find_bmi(names,weights,heights)

def find_bmi(name,weight,height):
   i = 0
   num = 0
   for i in range(0, len(name)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      if(bmi>25):
         print("Name",name[i])
         print("BMI =",bmi)

setup()

Lab 6 : Find/count number of students with BMI > 25

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with BMI > 25 :",count_bmi(weights,heights))

def count_bmi(weight,height):
   i = 0
   num = 0
   for i in range(0, len(weight)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      if(bmi>25):
         num = num+1
   return num

setup()

Lab 6 : Find student BMI

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   find_bmi(names,weights,heights)

def find_bmi(name,weight,height):
   i = 0
   for i in range(0, len(name)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      print("*****Number",i,"*****")
      print("Name",name[i])
      print("BMI =",bmi)

setup()

Lab 6 : Display student records (data)

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   data(names,IDs,ages,weights,heights)

def data(name,ID,age,weight,height):
   i = 0
   for i in range(0, len(name)):
      print("*****Number",i,"*****")
      print("Name",name[i])
      print("ID",ID[i])
      print("Age",age[i])
      print("Weight",weight[i])
      print("Height",height[i])
   return data

setup()

lab 5 : Increase/decrease values in array (by fixed value or percentage)

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i1 = 0
   i2 = 0
   while(i1 < len(n)):
      #sum = sum + n[ i ]
      print("n[",i1,"] =", n[i1])
      i1 = i1 + 1
   while(i2 < len(n)):
      new_n = input("Put new value =")
      print("n[",i2,"] = ",new_n)
      i2 = i2 + 1

setup()

Lab 5 : Find average of values in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      sum = sum + n[ i ]
      i = i + 1
   aver = sum/len(n)
   print("average =", aver)

setup()

Lab 5 : Find/count number of positive values in array

def setup():
   n = [ -1, 8, -7, 12, 3, -6, 9 ]
   i = 0
   num = 0
   while(i < len(n)):
      if(n[i]>=0):
         num = num + 1
      i = i + 1
   print("number of positive values  =", num)

setup()

Lab 5 : Find sum of positive values in array

def setup():
   n = [ 1, 8, 7, 12, 3, -6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      if(n[i]>=0):
         sum = sum + n[ i ]
      i = i + 1
   print("sum =", sum)

setup()

Lab 5 : Find sum of values in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      sum = sum + n[ i ]
      i = i + 1
   print("sum =", sum)

setup()

Lab 5 : Find index of (the last) maximum value in array

def setup():
   n = [ 1, 9, 12, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   indexMax = 0
   n.reverse()
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
         indexMax = i
      i = i+1
   print("(Max)Index position from last :",indexMax)

setup()

Lab 5 : Find index of (the first) maximum value in array

def setup():
   n = [ 1, 9, 12, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   indexMax = 0
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
         indexMax = i
      i = i+1
   print("(Max)Index position from first :",indexMax)

setup()

Lab 5: Find the maximum value in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
      i = i+1
   print("Maximum :",numMax)

setup()

Lab 5 : Display elements (value) of array and its index

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   while(i < len(n)):
      print("n[",i,"] =", n[i])
      i = i + 1

setup()

วันพุธที่ 16 กันยายน พ.ศ. 2558

Lab 4x : Summary of prime numbers

def setup():
   print("Summary of prime numbers = ",sum_prime(10))

def sum_prime(number):
   first = 2
   sum = 0
   while (first <= number):
      if (prime_number(first)):
         sum += first
      first += 1
   
   return sum

def prime_number(p):
   count = 2
   if (p==1):
         return False
   while (count<p):
      if (p%count == 0):
         return False
      count += 1
   return True

setup()

Lab 4x Summary of integers

def setup():
   count=20
   print("summary = ",sum_int(count))

def sum_int(count):
   n = 1
   sum = 0
   while (n <= count):
      sum = sum+n
      n = n+1
   return sum

setup()

Lab 4x : Multiplication tables

def setup():
  n = 1
  m = 12
  x = 18
  print("Multiplication of ",x)
  while (n<=m):
    sum = x*n
    print(x," * ",n," = ",sum)
    n = n+1

setup()

Lab 4x : Leap year

def setup():
   print("Leap Year Calculator")
   calculate_year()

def calculate_year ():
   year = 1995
   mod4 = year%4
   mod100 = year%100
   mod400 = year%400
   if (mod4 == 0):
      if (mod100 != 0):
         print(year," : Leap Year")
      elif (mod100 == 0):
         if (mod400 != 0):
            print(year," : Not A Leap Year")
         elif (mod400 == 0):
            print(year," : Leap Year")
   elif (mod4 != 0):
      print(year," : Not A Leap Year")
     
setup()

Lab 4x : Grade

def setup():
  score=65.8
  show_score(score)
  calculate_grade(score)

def show_score(score):
  print("YOUR SCORE : ",score)

def calculate_grade(score):
  if (score >= 80 and score <= 100):
    print("YOU GET A")
  elif (score >= 70 and score <= 79):
    print("YOU GET B")
  elif (score >= 60 and score <= 69):
    print("YOU GET C")
  elif (score >= 50 and score <= 69):
    print("YOU GET D")
  elif (score >= 0 and score <= 49):
    print("YOU GET F")
  else:
    print("NOT WORKING...")
      
setup()

Lab 4x : Calculate circumference and area of a circle

def calculate_circle():
  dia=50
  rad=dia/2
  area=3.14*rad*rad
  cir=3.14*dia
  print("Diameter : ",dia)
  print("Area : ",area)
  print("Circumference : ",cir)

calculate_circle()

Lab 4x : Calculate body mass index (BMI)

def calculate_bmi():
  weight=39          #weight-kilogram
  Hcm=151           #height-centimeter
  Hm=Hcm/100
  bmi=weight/(Hm*Hm)
  print("Weight : ",weight)
  print("Height(centimeter) : ",Hcm)
  print("BMI : ",bmi)

calculate_bmi()

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

Lab 4 : Syntax error

1. the local variable "space" may not have been initialized.














cause of problem : forgot to set a value of space
solution : set a value to space   --->   int space=0;



2. Error on "::IdentifierOrNew"
          expecting SEMI, found 'draw_balloon'



















cause of problem : forgot a ; after y=(y+10)%height
solution : put a ; after that



3. Found one too many { characters without a } to match it






















cause of problem : forgot a } after return sum;
solution : put a } after that





วันเสาร์ที่ 12 กันยายน พ.ศ. 2558

Lab 4 : Loan payment

void setup() {
  loan_payment(5000, 0.05, 12);
}
void loan_payment(float amount, float rate, int month) {
  float rate_permonth = rate/month;
  float pay_permonth = amount*(rate_permonth/(1-pow(1+rate_permonth, -month)));
  float principal = pay_permonth;
  float remain = amount;
  int x = 1;
  println("No.  Beginning Balance    Interest      Principal     Ending Balance");
  while (x <= month) {
    print(nf(x, 2)); //Num
    print("     "+nf(remain, 4, 2)); //remain
    principal = pay_permonth-(rate_permonth*remain);
    remain -= principal;
    if (remain < 0) {
      remain = 0;
    }
    print("             "+nf(rate_permonth*remain, 2, 2)); //Principal
    print("         "+nf(principal, 3, 2)); //Unpaid
    print("         "+nf(remain, 4, 2)); //Total
    x++;
  }
}

Lab 4 : Summary of prime numbers

void setup() {
  println("Summary of prime numbers = "+sum_prime(3));
}
int sum_prime(int number) {
  int first_prime = 2;
  int sum = 0;
  while (first_prime<=number) {
    if (prime_number(first_prime)) {
      sum += first_prime;
    }
    first_prime++;
  }
  return sum;
}
boolean prime_number(int prime) {
  boolean result = true;
  int n=2;
  while (n<prime) {
    if (prime%n == 0) {
      result = false;
    }
    n++;
  }
  if(prime == 1) {
    result = false;
  }
  return result;
}

Lab 4 : Multiplication tables

void setup() {
  int n = 1;
  int m = 12;
  int x = 18;
  println("Multiplication of "+x);
  while (n<=m) {
    int sum = x*n;
    println(+x+" * "+n+" = "+sum);
    n++;
  }
}

Lab 4 : Summary of integers

void setup() {
  println("summary = "+sum_int(20));
}
int sum_int(int count) {
  int n = 0;
  int sum = 0;
  while (n <= count) {
    sum += n;
    n++;
  }
  return sum;
}

Lab 4 : favorite song (loop)


int colour1;
int colour2;
int colour3;
int colour4;
int colour_passenger;
int colour_LetHerGo;

void setup() {
  size(600, 360);
}
void draw() {
  background(#B7F8FF);
  text_PASSENGER(140, 60);
  text_LetHerGo(200, 170);
  draw_loop();
}
void draw_FerrisWheel(int x, int y) {
  //Ferris wheel
  noFill();
  stroke(255);
  strokeWeight(3);
  //wheel_1
  ellipse(100+x, 100+y, 200, 200);
  //wheel_2
  ellipse(100+x, 100+y, 180, 180);
  //wheel_3
  ellipse(100+x, 100+y, 100, 100);
  //wheel_4
  fill(255);
  ellipse(100+x, 100+y, 30, 30);
  //pole
  stroke(255);
  strokeWeight(6);
  //poleL
  line(85+x, 100+y, 0+x, 350+y);
  //poleR
  line(115+x, 100+y, 200+x, 350+y);
}
void draw_bench(int x, int y) {
  //bench
  noStroke();
  //bench-1
  fill(colour1);
  ellipse(50+x, 20+y, 10, 10);
  ellipse(50+x, 40+y, 40, 40);
  //bench-2
  fill(colour2);
  ellipse(180+x, 50+y, 10, 10);
  ellipse(180+x, 70+y, 40, 40);
  //bench-3
  fill(colour3);
  ellipse(160+x, 175+y, 10, 10);
  ellipse(160+x, 195+y, 40, 40);
  //bench-4
  fill(colour4);
  ellipse(20+x, 150+y, 10, 10);
  ellipse(20+x, 170+y, 40, 40);
}
void text_PASSENGER(int x, int y) {
  //text"PASSENGER"
  fill(colour_passenger);
  textSize(60);
  text("PASSENGER", x, y);
}
void text_LetHerGo(int x, int y) {
  fill(colour_LetHerGo);
  textSize(100);
  //text"LET"
  text("LET", x+10, y);
  //text"HER"
  text("HER", x, 80+y);
  //text"GO"
  text("GO", x+20, 160+y);
}
void keyPressed() {
  colour1 = color(random(0, 225), random(0, 225), random(0, 225));
  colour2 = color(random(0, 225), random(0, 225), random(0, 225));
  colour3 = color(random(0, 225), random(0, 225), random(0, 225));
  colour4 = color(random(0, 225), random(0, 225), random(0, 225));
  colour_passenger = color(random(0, 225), random(0, 225), random(0, 225));
  colour_LetHerGo = color(random(0, 225), random(0, 225), random(0, 225));
}
void draw_loop() {
  int n=0;
  int m=0;
  while (n<3) {
    draw_FerrisWheel(mouseX+m, mouseY);
    draw_bench(mouseX+m, mouseY);
    n++;
    m+=200;
  }
}

Lab 4 : favorite book (loop)


int wide=0;
int high=0;
int colour;
void setup() {
  size(350, 450);
}
void draw() {
  int posX=mouseX;
  int posY=mouseY;
  draw_wolf(posX,posY);
  draw_text();

}
void draw_wolf(int x,int y) {
  int n=0;
  int m=0;
  background(#a19d8c);
  while(n<3){
  //wolf :  begin at left to right
  fill(colour);
  noStroke();
  beginShape();
  vertex(x+wide, 165+y+m);   //01-no move
  //over
  vertex(70+x+wide, 96+y+m);  //02
  vertex(164+x+wide, 74+y+m);  //03
  vertex(188+x+wide, 50+y+m);  //04
  vertex(179+x+wide, 18+y+m);  //05
  vertex(195+x+wide, 30+y+m);  //06
  vertex(190+x+wide, 17+y+m);  //07
  vertex(205+x+wide, 30+y+m);  //08
  vertex(239+x+wide, y+m);  //09
  //under
  vertex(243+x+wide, 10+y+high+m);  //10
  vertex(235+x+wide, 40+y+high+m);  //11
  vertex(247+x+wide, 27+y+high+m);  //12
  vertex(250+x+wide, 35+y+high+m);  //13
  vertex(230+x+wide, 60+y+high+m);  //14
  vertex(195+x+wide, 142+y+high+m);  //15
  vertex(198+x+wide, 175+y+high+m);  //16
  vertex(215+x+wide, 188+y+high+m);  //17
  vertex(200+x+wide, 190+y+high+m);  //18
  vertex(187+x+wide, 168+y+high+m);  //19
  vertex(190+x+wide, 190+y+high+m);  //20
  vertex(205+x+wide, 205+y+high+m);  //21
  vertex(190+x+wide, 205+y+high+m);  //22
  vertex(165+x+wide, 145+y+high+m);  //23
  vertex(105+x+wide, 135+y+high+m);  //24
  vertex(85+x+wide, 150+y+high+m);  //25
  vertex(85+x+wide, 167+y+high+m);  //26
  vertex(105+x+wide, 180+y+high+m);  //27
  vertex(85+x+wide, 180+y+high+m);  //28
  vertex(75+x+wide, 150+y+high+m);  //29
  vertex(60+x+wide, 160+y+high+m);  //30
  vertex(55+x+wide, 185+y+high+m);  //31
  vertex(70+x+wide, 197+y+high+m);  //32
  vertex(55+x+wide, 198+y+high+m);  //33
  vertex(45+x+wide, 160+y+high+m);   //34
  vertex(50+x+wide, 150+y+high+m);  //35
  endShape();
  n++;
  m -= 200;
  }
}
void draw_text() {
  fill(#2f4940);
  textSize(80);
  text("WHITE", 40, 75);
  text("FANG", 60, 150);
  textSize(30);
  text("JACK LONDON", 70, 425);
}
void mouseClicked() {
  colour = color(random(0, 225), random(0, 225), random(0, 225));
}

Lab 4 : favorite movie (loop)


int swW=0;
int swH=0;
void setup() {
  size(450, 300);
}
void draw() {
  int n=0;
  int m=0;
  background(#ffd800);
  text_BILL();
  while(n<5) {
  draw_sword(mouseX+m,mouseY);
  n++;
  m-=100;
  }
  text_KILL();

}
void draw_sword(int x,int y) {
  //sword
  noStroke();
  fill(0);
  //black0-1
  rect(350+x, 0+y, 50+swW, 140+swH);
  //black0-2
  rect(348+x, 140+y+swH, 54+swW, 30+swH);
  //black0-3
  rect(330+x, 170+y+swH, 90+swW, 8+swH);
  //black0-4
  rect(348+x, 178+y+swH, 54+swW, 3+swH);
  //white0-5
  fill(255);
  rect(350+x, 181+y+swH+swH, 50+swW, 10+swH);
  //black0-6
  fill(0);
  rect(348+x, 191+y+swH, 54+swW, 3+swH);
  //black0-7
  rect(350+x, 194+y+swH, 50+swW, 106+swH);
  //shadow
  //white1-1
  fill(255);
  rect(350+x, 0+y, 30+swW, 140+swH);
  //white1-2
  triangle(360+x, 194+swH+y, 390+swW+x, 194+swH+y, 375+x+(swW/2), 204+swH+y);
  //white1-3
  quad(360+x, 219+swH+y, 375+(swW/2)+x, 209+swH+y, 390+swW+x, 219+swH+y, 375+x, 229+swH+y);
  //white1-4
  quad(360+x, 244+swH+y, 375+(swW/2)+x, 234+swH+y, 390+swW+x, 244+swH+y, 375+x, 254+swH+y);
  //white1-5
  quad(360+x, 269+swH+y, 375+(swW/2)+x, 259+swH+y, 390+swW+x, 269+swH+y, 375+x, 279+swH+y);
  //white1-6
  quad(360+x, 294+swH+y, 375+(swW/2)+x, 284+swH+y, 390+swW+x, 294+swH+y, 375+x, 304+swH+y);
}
void text_KILL() {
  textSize(150);
  //text-KILL
  fill(#ca0101);
  text("KILL", 10, 140);
}
void text_BILL() {
  textSize(150);
  fill(0);
  text("BILL", 10, 270);
  //cut
  stroke(#ffd800);
  strokeWeight(3);
  line(0, 260, 330, 180);
}
void draw_cut() {
  fill(255);
  strokeWeight(10);
  line(random(0,width),0, random(0,width),300);
}
void keyPressed(){
  frameRate(10);
  fill(0,0,0,10);
  rect(0,0,width,height);
  fill(#ca0101);
  ellipse(random(0,width), random(0,height), 150, 150);
}

Lab 4 : flocks of birds


int birdSize=50;
int colour=250;
int wingMove;
void setup(){
size(300,300);
}
void draw() {
  background(#BBEEFA);
  //flying_bird();
  draw_flocks(mouseX, mouseY);
  flying_bird();
}
void draw_bird(int x, int y) {
  strokeWeight(0);
  //body
  fill(colour);
  ellipse(x,y,birdSize,birdSize);
  //eyes
  fill(#675143);
  ellipse(x-(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
  ellipse(x+(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
  //mouth
  fill(#FFFF00);
  triangle(x-(birdSize/4), y+(birdSize/6), x+(birdSize/4), y+(birdSize/6), x, y+birdSize/3);
    //wings
  strokeWeight(10);
  stroke(colour);
  line(x-(birdSize/2), y, x-birdSize, wingMove);
  line(x+(birdSize/2), y, x+birdSize, wingMove);
}
void flying_bird() {
  wingMove = mouseY;
  if (frameCount%30 > 15) {
    wingMove += birdSize/3;
  } else {
    wingMove -= birdSize/3;
  }
}
void draw_flocks(int x,int y) {
  int n=0;
  int posX=0;
  while (n<3) {
    fill(colour);
    draw_bird(mouseX+posX,mouseY);
    n++;
    posX += 100;
 
  }
}
void keyPressed() {
  colour = color(random(0, 225), random(0, 225), random(0, 225));
}

Lab 4 : balloons (loop)


int radius = 50;
int string_length = radius*2;
int posY=0;
int colour;
void setup() {
  size(300, 300);
}
void draw() {
  background(#BBEEFA);
  frameRate(20);
  int posX = radius/2;
  draw_balloon(posX, 300-posX);
  posY=(posY-10)%(height+125);
}
void draw_balloon(int x, int y) {
  //int radius = 50;
  int n=0;
  int space=0;
  while (n<7) {
    fill(colour);
    ellipse(x+space, y+posY, radius, radius);
    stroke(0);
    line(x+space, y+(radius/2)+posY, x+space, y + string_length+posY);
    n++;
    space+=radius;
  }
}
void mouseMoved() {
  colour = color(random(0, 225), random(0, 225), random(0, 225));
}

Lab 3 : favorite song (change colour)


int moveX=0;
int moveY=0;
int count=2;
int result;
int colour1;
int colour2;
int colour3;
int colour4;


void setup() {
  size(600, 360);
}
void draw() {
  frameRate(2);
  result=count%2;
  count=count+1;
  draw_FerrisWheel();
  draw_bench();
  if (result==0) {
   fill(0);
   text_PASSENGER();
   fill(#7a92be);
   text_LetHerGo();
  }
  if (result==1) {
   fill(#7a92be);
   text_PASSENGER();
   fill(0);
   text_LetHerGo();
  }

}
void draw_FerrisWheel() {
  background(#7a92be);
  //Ferris wheel
  fill(#7a92be);
  stroke(255);
  strokeWeight(3);
  //wheel_1
  ellipse(100+moveX, 100+moveY, 200, 200);
  //wheel_2
  ellipse(100+moveX, 100+moveY, 180, 180);
  //wheel_3
  ellipse(100+moveX, 100+moveY, 100, 100);
  //wheel_4
  fill(255);
  ellipse(100+moveX, 100+moveY, 30, 30);
  //pole
  stroke(255);
  strokeWeight(6);
  //poleL
  line(85+moveX, 100+moveY, 0+moveX, 350+moveY);
  //poleR
  line(115+moveX, 100+moveY, 200+moveX, 350+moveY);
}
void text_PASSENGER() {
  //text"PASSENGER"
  textSize(60);
  text("PASSENGER", 210+moveX, 70+moveY);
}
void text_LetHerGo() {
  textSize(100);
  //text"LET"
  text("LET", 400+moveX, 170+moveY);
  //text"HER"
  text("HER", 300+moveX, 250+moveY);
  //text"GO"
  text("GO", 200+moveX, 330+moveY);
}
void draw_bench(){
  //bench
  noStroke();
  //bench-1
  fill(colour1);
  ellipse(50+moveX, 20+moveY, 10, 10);
  ellipse(50+moveX, 40+moveY, 40, 40);
  //bench-2
  fill(colour2);
  ellipse(180+moveX, 50+moveY, 10, 10);
  ellipse(180+moveX, 70+moveY, 40, 40);
  //bench-3
  fill(colour3);
  ellipse(160+moveX, 175+moveY, 10, 10);
  ellipse(160+moveX, 195+moveY, 40, 40);
  //bench-4
  fill(colour4);
  ellipse(20+moveX, 150+moveY, 10, 10);
  ellipse(20+moveX, 170+moveY, 40, 40);
}
void keyPressed(){
  colour1 = color(random(0, 225), random(0, 225), random(0, 225));
  colour2 = color(random(0, 225), random(0, 225), random(0, 225));
  colour3 = color(random(0, 225), random(0, 225), random(0, 225));
  colour4 = color(random(0, 225), random(0, 225), random(0, 225));
}

lab 3 : favorite movie (mouseMoved)


int swW=0;
int swH=0;
int moveX=0;
int moveY=0;
void setup() {
  size(450, 300);
}
void draw() {
  background(#ffd800);
  text_BILL();
  draw_sword();
  text_KILL();
  frameRate(30);
  moveX=(moveX-10)%width;

}
void draw_sword() {
  //sword
  noStroke();
  fill(0);
  //black0-1
  rect(350+moveX, 0+moveY, 50+swW, 140+swH);
  //black0-2
  rect(348+moveX, 140+moveY+swH, 54+swW, 30+swH); 
  //black0-3
  rect(330+moveX, 170+moveY+swH, 90+swW, 8+swH);
  //black0-4
  rect(348+moveX, 178+moveY+swH, 54+swW, 3+swH);
  //white0-5
  fill(255);
  rect(350+moveX, 181+moveY+swH+swH, 50+swW, 10+swH);
  //black0-6
  fill(0);
  rect(348+moveX, 191+moveY+swH, 54+swW, 3+swH);
  //black0-7
  rect(350+moveX, 194+moveY+swH, 50+swW, 106+swH);
  //shadow
  //white1-1
  fill(255);
  rect(350+moveX, 0+moveY, 30+swW, 140+swH);
  //white1-2
  triangle(360+moveX, 194+swH+moveY, 390+swW+moveX, 194+swH+moveY, 375+moveX+(swW/2), 204+swH+moveY);
  //white1-3
  quad(360+moveX, 219+swH+moveY, 375+(swW/2)+moveX, 209+swH+moveY, 390+swW+moveX, 219+swH+moveY, 375+moveX, 229+swH+moveY);
  //white1-4
  quad(360+moveX, 244+swH+moveY, 375+(swW/2)+moveX, 234+swH+moveY, 390+swW+moveX, 244+swH+moveY, 375+moveX, 254+swH+moveY);
  //white1-5
  quad(360+moveX, 269+swH+moveY, 375+(swW/2)+moveX, 259+swH+moveY, 390+swW+moveX, 269+swH+moveY, 375+moveX, 279+swH+moveY);
  //white1-6
  quad(360+moveX, 294+swH+moveY, 375+(swW/2)+moveX, 284+swH+moveY, 390+swW+moveX, 294+swH+moveY, 375+moveX, 304+swH+moveY);
}
void text_KILL() {
  textSize(150);
  //text-KILL
  fill(#ca0101);
  text("KILL", 10, 140);
}
void text_BILL() {
  textSize(150);
  fill(0);
  text("BILL", 10, 270);
  //cut
  stroke(#ffd800);
  strokeWeight(3);
  line(0, 260, 330, 180);
}
void draw_cut() {
  fill(255);
  strokeWeight(10);
  line(random(0,width),0, random(0,width),300);
}
void mouseMoved(){
  frameRate(5);
  fill(0,0,0,10);
  rect(0,0,width,height);
  fill(#ca0101);
  ellipse(random(0,width), random(0,height), 100, 100);
}

lab 3 : favorite book (change colour)


int wide=0;
int high=0;
int colour;
void setup() {
  size(350, 450);
}
void draw() {
  draw_wolf(0-243,180);
  frameRate(10);
  wide=(wide+10)%(width+250);
  draw_text();

}
void draw_wolf(int x,int y) {
  background(#a19d8c);

  //wolf :  begin at left to right
  fill(colour);
  noStroke();
  beginShape();
  vertex(x+wide, 165+y);   //01-no move
  //over
  vertex(70+x+wide, 96+y);  //02
  vertex(164+x+wide, 74+y);  //03
  vertex(188+x+wide, 50+y);  //04
  vertex(179+x+wide, 18+y);  //05
  vertex(195+x+wide, 30+y);  //06
  vertex(190+x+wide, 17+y);  //07
  vertex(205+x+wide, 30+y);  //08
  vertex(239+x+wide, y);  //09
  //under
  vertex(243+x+wide, 10+y+high);  //10
  vertex(235+x+wide, 40+y+high);  //11
  vertex(247+x+wide, 27+y+high);  //12
  vertex(250+x+wide, 35+y+high);  //13
  vertex(230+x+wide, 60+y+high);  //14
  vertex(195+x+wide, 142+y+high);  //15
  vertex(198+x+wide, 175+y+high);  //16
  vertex(215+x+wide, 188+y+high);  //17
  vertex(200+x+wide, 190+y+high);  //18
  vertex(187+x+wide, 168+y+high);  //19
  vertex(190+x+wide, 190+y+high);  //20
  vertex(205+x+wide, 205+y+high);  //21
  vertex(190+x+wide, 205+y+high);  //22
  vertex(165+x+wide, 145+y+high);  //23
  vertex(105+x+wide, 135+y+high);  //24
  vertex(85+x+wide, 150+y+high);  //25
  vertex(85+x+wide, 167+y+high);  //26
  vertex(105+x+wide, 180+y+high);  //27
  vertex(85+x+wide, 180+y+high);  //28
  vertex(75+x+wide, 150+y+high);  //29
  vertex(60+x+wide, 160+y+high);  //30
  vertex(55+x+wide, 185+y+high);  //31
  vertex(70+x+wide, 197+y+high);  //32
  vertex(55+x+wide, 198+y+high);  //33
  vertex(45+x+wide, 160+y+high);   //34
  vertex(50+x+wide, 150+y+high);  //35
  endShape();
}
void draw_text() {
  fill(#2f4940);
  textSize(80);
  text("WHITE", 40, 75);
  text("FANG", 60, 150);
  textSize(30);
  text("JACK LONDON", 70, 425);
}
void mouseClicked() {
  colour = color(random(0, 225), random(0, 225), random(0, 225));
}

Lab 3 : flying bird



int birdSize=100;
int x=30;
int colour=250;
int wingMove;
void setup(){
size(300,300);
}
void draw() {
  background(#BBEEFA);
  wingMove = mouseY;
  if (frameCount%x > x/2) {
    wingMove += birdSize/3;
  } else {
    wingMove -= birdSize/3;
  }
  draw_bird(mouseX, mouseY);
}
void draw_bird(int x, int y) {
  strokeWeight(0);
  //body
  fill(colour);
  ellipse(x,y,birdSize,birdSize);
  //eyes
  fill(#675143);
  ellipse(x-(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
  ellipse(x+(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
  //mouth
  fill(#FFFF00);
  triangle(x-(birdSize/4), y+(birdSize/6), x+(birdSize/4), y+(birdSize/6), x, y+birdSize/3);
    //wings
  strokeWeight(10);
  stroke(colour);
  line(x-(birdSize/2), y, x-birdSize, wingMove);
  line(x+(birdSize/2), y, x+birdSize, wingMove);
}
void keyPressed() {
  colour = color(random(0, 225), random(0, 225), random(0, 225));
}

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

Lab 3 : Delivery charge

// letter(oz)-->(1) || box(pound)-->(2)
int packaging = 2;
// Next Day Priority(1) || Next Day Standard(2) || 2-Day(3)
int service = 3;
// Weight
float weight = 3 ;
float charge;
void setup() {
  size(300, 300);
}
void draw() {
  background(255);
  fill(0);
  textSize(25);
  text("DELIVERY SERVICE", 45, 20);
  textSize(15);
  text("Types of packaging", 10, 60);
  text("letter || box", 10, 80);
  text("Types of service", 10, 120);
  textSize(12);
  text("Next Day Priority || Next Day Standard || 2-Day", 10, 140);
  //call funftion
  textSize(20);
  calculate_charge();
  text("CHARGE : $ "+charge, 20, 260);
}
void calculate_charge() {
  // *****letter***** //
  if (packaging == 1) {
    text("PACKAGING : letter", 20, 180);
    text("WEIGHT : "+weight+" oz", 20, 240);
    // *****Next Day Priority***** //
    if (service == 1) {
      text("SERVICE : Next Day Priority", 20, 200);
      if (weight >= 8) {
        charge = 12;
      } else {
        charge = 0;
      }
    }
    // *****Next Day Standard***** //
    if (service == 2) {
      text("SERVICE : Next Day Standard", 20, 200);
      if (weight >= 8) {
        charge = 10.5;
      } else {
        charge = 0;
      }
    }
    // *****2-Day***** //
    if (service == 3) {
      text("SERVICE : 2-Day", 20, 200);
      text("Not available", 20, 290);
    }
  }
  // *****box***** //
  if (packaging == 2) {
    text("PACKAGING : box", 20, 180);
    text("WEIGHT : "+weight+" pound", 20, 240);
    // *****Next Day Priority***** //
    if (service == 1) {
      text("SERVICE : Next Day Priority", 20, 200);
      if (weight == 1) {
        charge = 15.75;
      } else if (weight>=2) {
        charge = 15.75+((weight-1)*1.25);
      } else {
        charge = 0;
      }
    }
    // *****Next Day Standard***** //
    if (service == 2) {
      text("SERVICE : Next Day Standard", 20, 200);
      if (weight == 1) {
        charge = 13.75;
      } else if (weight>=2) {
        charge = 13.75+((weight-1)*1);
      } else {
        charge = 0;
      }
    }
    // *****2-Day***** //
    if (service == 3) {
      text("SERVICE : 2-Day", 20, 200);
      if (weight == 1) {
        charge = 7;
      } else if (weight>=2) {
        charge = 7+((weight-1)*0.5);
      } else {
        charge = 0;
      }
    }
  }
}



lab 3 : Leap years

int year = 1995;
void setup() {
  size(300, 300);
}
void draw() {
  background(255);
  frameRate(1);
  fill(0);
  textSize(30);
  text("Leap Year Calculator", 0, 50);
  year=year+1;
  calculate_year();
  textSize(50);
  text("Year : "+year, 5, 150);
}
void calculate_year () {
  int mod4 = year%4;
  int mod100 = year%100;
  int mod400 = year%400;
  textSize(35);
  if (mod4 == 0) {
    if (mod100 != 0) {
      fill(84, 129, 230);
      text("Leap Year", 60, 200);
    } else if (mod100 == 0) {
      if (mod400 != 0) {
        fill(241, 95, 116);
        text("Not A Leap Year", 10, 200);
      } else if (mod400 == 0) {
        fill(84, 129, 230);
        text("Leap Year", 60, 200);
      }
    }
  } else if (mod4 != 0) {
    fill(241, 95, 116);
    text("Not A Leap Year", 10, 200);
  }
}



Lab 3 : Power of 10

int power;
String number;
void setup() {
  size(300, 300);
}
void draw() {
  background(255);
  frameRate(1);
  power_of_ten(5, 100);
  display_message(5, 200);
}
void power_of_ten(int x, int y) {
  fill(0);
  textSize(35);
  text("Power of 10 : "+power, x, y);
}
void display_message(int x, int y) {
  fill(0);
  textSize(18);
  text("Number : "+number, x, y);
  power=power+1;
  if (power==6) {
    number="Million";
  } else if (power==9) {
    number="Billion";
  } else if (power==12) {
    number="Thillion";
  } else if (power==15) {
    number="Quadrillion";
  } else if (power==18) {
    number="Quintillion";
  } else if (power==21) {
    number="Sextillion";
  } else if (power==30) {
    number="Nonillion";
  } else if (power==100) {
    number="Googol";
  } else {
    number="No corresponding word";
  }
}



Lab 3 : Battery charged

int scale;
int energy=1;
int colour=0;
void setup() {
  size(300, 300);
}
void draw() {
  background(255);
  frameRate(10);
  //call function
  draw_battery(65, 80);
  draw_positive(245, 95);
  draw_minus(5, 110);
  draw_energy();
  percent_battery(100, 250);
}
void draw_battery(int x, int y) {
  int batW = 160;
  int batH = batW/2;
  //battery
  fill(255);
  rect(x, y, batW, batH);
  //battery's polar
  fill(0);
  rect(batW+x, (batH/3)+y, 10, batH/3);
  //energy
  fill(colour);
  rect(x, y, scale, batH);
}
void draw_positive(int x, int y) {
  float sizePosi = 50;
  //positive
  fill(0);
  //vertical
  rect(x+(sizePosi/3), y, sizePosi/3, sizePosi);
  //horizon
  rect(x, y+(sizePosi/3), sizePosi, sizePosi/3);
}
void draw_minus(int x, int y ) {
  int sizeMinus = 50;
  fill(0);
  rect(x, y, sizeMinus, sizeMinus/3);
}
void text_full(int x, int y) {
  fill(#00FF00);
  textSize(30);
  text("FULL BATTERY", x, y);
}
void text_low(int x, int y) {
  fill(#DD0000);
  textSize(30);
  text("LOW BATTERY", x, y);
}
void draw_energy () {
  //energy
  scale = scale+energy;
  if (scale==160) {
    energy = 0;
    energy = energy - 1;
    text_full(60, 40);
  }
  if (scale == 0 && energy < 0) {
    energy = 0;
    energy = energy + 1;
    text_low(60, 40);
  }
}
void percent_battery(int x, int y) {
  //percent battery
  int perBat = (scale*100)/160;
  if (perBat >= 15) {
    colour = #00FF00;
  } else if (perBat <= 5) {
    colour = #DD0000;
  }
  fill(0);
  textSize(50);
  text((int)(perBat)+"%", x, y);
}



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

Lab 3 : Calculate grade from score

float score=65.8;
void setup() {
  size(300, 300);
}
void draw() {
  show_score(5,100);
  calculate_grade(70,200);
}
void show_score(int x,int y){
  fill(0);
  textSize(30);
  text("YOUR SCORE : "+score,x,y);
}
void calculate_grade(int x,int y) {
  fill(0);
  textSize(30);
  if (score >= 80 && score <= 100) {
    text("YOU GET A",x,y);
  } else if (score >= 70 && score <= 79) {
    text("YOU GET B",x,y);
  } else if (score >= 60 && score <= 69) {
    text("YOU GET C",x,y);
  } else if (score >= 50 && score <= 69) {
    text("YOU GET D",x,y);
  } else if (score >= 0 && score <= 49) {
    text("YOU GET F",x,y);
  } else {
    text("NOT WORKING...",x,y);
  }
}


Lab 3 : Balloon

void setup(){
  size(300,300);
}
void draw() {
  background(#BFEBF0);
  int x = mouseX;
  int y = mouseY;
  draw_balloon(x, y);
  if (y < 100) {
    fill(#DD0000);     //red
  }else if(y > 100 && y < 200){
    fill(#FFF300);     //yellow
  }
  else{
    fill(#00DD00);     //green
  }
}
void draw_balloon(int x, int y) {
  int radius = 100;
  int string_length = 150;
  line(x, y, x, y + string_length);
  ellipse(x, y, radius, radius);
}



Lab 2 : Syntax error

1. Missing a semicolon
     สาเหตุ ลืมใส่สัญลักษณ์ semicolon (;) ด้านหลังคำสั่ง
     วิธีแก้ ใส่ semicolon (;) ตรงที่มีแจ้งเตือนแถบสีเหลือง

2. The variable "..." does not exist
     สาเหตุ ใส่ชื่อตัวแปรไม่ตรงตามที่เราได้กำหนดไว้
                ประกาศตัวแปรแบบ local ทำให้ฟังก์ชันที่ต้องใช้ค่าตัวแปรหาค่าตัวแปรไม่เจอ
     วิธีแก้ แก้ไขชื่อตัวแปรให้ตรงกัน
              แก้ไขการประกาศตัวแปรเป็นแบบ global

3. The function "..." does not exist 
     สาเหตุ ใส่ชื่อฟังก์ชันไม่ตรงตามชื่อฟังก์ชันของ System function
     วิธีแก้ แก้ไขชื่อฟังก์ชันให้ถูกต้อง