วันจันทร์ที่ 21 กันยายน พ.ศ. 2558

LAB 5 Generic 1-16 (Array & String)


Lab 5 Generic 1 ( Value of array and its index )
def setup():
  value = [19,18,17,16,15,14]
  ValueAndIndex(value)
   
def ValueAndIndex(value):  
  index = 0
  while(index < len(value)):
    print("Value = ",value[index]," Index = ",index)
    index = index + 1
 
setup()    

Lab 5 Generic 2 ( Maximum value )
def setup():
  value = [19,18,17,21,15,32]
  max_value(value)

def max_value(value):
  index = 0
  max_num = value[0]
  while(index < len(value)):
    if(value[index] > max_num):
      max_num = value[index]
    index = index + 1
  print("Max value = ",max_num)

setup()

Lab 5 Generic 3 ( Index of the first maximum)
def setup():
  value = [19,17,21,21,15,14]
  first_max_value_index(value)

def first_max_value_index(value):
  index = 0
  max_num = value[0]
  while(index < len(value)):
    if(value[index] > max_num):
      max_num = value[index]
      first_index = index
    index = index + 1
  print(" First Maximum =",max_num)
  print(" First Maximum index =",first_index)


setup()  

Lab 5 Generic 4 ( Index of the last maximum)
def setup():
  value = [19,17,21,21,15,14,21]
  last_max_value_index(value)  

def last_max_value_index(value):
  index = 0
  max_num = value[0]
  while(index < len(value)):
    current = value[index]
    if(current >= max_num):
      max_num = current
      last_index = index
    index = index + 1
  print(" Last Maximum index =",last_index)


setup()    

Lab 5 Generic 5 ( Sum of values in array )
def setup():
  value = [19,17,21,21,15,14,21]
  sum_array(value)  

def sum_array(value):
  index = 0
  sum_num = 0
  while(index < len(value)):
    sum_num = sum_num + value[index]
    index = index + 1
  print(" Sum of array value = ",sum_num)

setup()    

Lab 5 Generic 6 ( Sum of positive values  )
def setup():
  value = [19,-17,21,-21,15,14,-21]
  sum_array(value)    
  
def sum_array(value):  
  index = 0
  sum_num = 0
  while(index < len(value)): 
    if(value[index] > 0):  
      sum_num = sum_num + value[index]
    index = index + 1  
  print(" Sum of array value = ",sum_num)
  
setup()   

Lab 5 Generic 7 (  Find/count number of positive values  )
def setup():
  value = [19,-17,21,-21,15,14,-21]
  sum_array(value)    
  
def sum_array(value):  
  index = 0
  count = 0
  while(index < len(value)): 
    if(value[index] > 0):  
      count = count + 1
    index = index + 1  
  print(" Number of positive values = ",count)
  
setup()      

Lab 5 Generic 8 (  average of values  )
def setup():
  value = [19,17,21,21,15,14]
  sum_array(value)    
  
def sum_array(value):  
  index = 0
  sum_num = 0
  while(index < len(value)): 
    sum_num = sum_num + value[index]  
    index = index + 1  
  avg_value = sum_num/len(value)    
  print("Sum of values = ",sum_num)
  print("Average = ",avg_value)
  
setup()      

Lab 5 Generic 9 ( Increase/decrease values )
def setup():
  value = [19,17,21,21,15,14]
  show_array(value)
  value = change_array_value(value)  
  show_array(value)
 
def change_array_value(value):
  index = 0
  Change = int(input("Value Change = "))
  while(index < len(value)):
    value[index] = value[index] + Change
    index = index + 1
  return value
   
def show_array(value):
  index = 0
  print("Value = [",end ="")
  while(index < len(value)):
    if(index < (len(value)-1)):
      print(value[index],",",end = "")
    else:
      print(value[index],"]")
    index = index + 1
   
setup()    

Lab 5 Generic 10 ( convert a number from base 10 to base 2 )
def setup():
  num = 2
  print("Number base 10 = ",num)
  new_num = convert_10_to_2(num)
  print("Number base 2 = ",new_num)

def convert_10_to_2(num):
  temp = 0
  while(num > 0):
    count = 0
    while(num/(pow(2,count)) >= 1):
      count = count + 1
    count = count - 1
    num = num - pow(2,count)
    temp = temp + pow(10,count)
    if(num == 1):
      num = 0
      temp = temp + 1
    assert (count >= 0)
  return temp

setup()

Lab 5 Generic 11 ( my_count()  function )
def setup():
  string1 = "Thailand"
  ref_alphabet = "a"
  print("String : ",string1)
  print("Reference alphabet : ",ref_alphabet,)
  num_alphabet = my_count(string1,ref_alphabet)
  print("Alphabet number(s) = ",num_alphabet)

def my_count(string,alphabet):
  num_alphabet = 0
  index = 0
  while(index < len(string)):
    if(string[index] == alphabet):
      num_alphabet = num_alphabet + 1
    index = index + 1
  return num_alphabet
  assert num_alphabet == count(string,alphabet)


setup()

Lab 5 Generic 12 ( my_find()  function )
def setup():
  string1 = "Thailand"
  alphabet = "a"
  print("String : ",string1)
  print("Alphabet : ",alphabet)
  index = my_find(string1,alphabet)
  print("Alphabet index : ",index)
  assert string1.find(alphabet) == my_find(string1,alphabet) 

def my_find(string,alphabet):
  place_alphabet = 0
  index = 0
  finsh = False
  while(index < len(string) and (finsh == False)):
    if(string[index] == alphabet):
      finsh = True
      return index 
    index = index + 1
  

setup()

Lab 5 Generic 13 ( my_replace()  function )
def setup():
  string1 = "Thailand"
  string2 = "land"
  string3 = "thai"
  print("string1 : ",string1)
  print("Referent String : ",string2)
  print("Replacement : ",string3)
  string1 = my_replace(string1,string2,string3)
  assert my_replace(string1,string2,string3) == string1.replace(string2,string3)
  print("Result : ",string1)

def my_replace(string,refStr,replacementStr):
  strIndex = 0
  refIndex = 0
  replaceIndex = 0
  stringTemp = ""
  stringTemp2 = ""
  replace = False
  while(strIndex < len(string)):
    stringTemp = stringTemp + string[strIndex]   
    if(string[strIndex] == refStr[refIndex]):
      while(refIndex < len(refStr)):
        if(string[strIndex] == refStr[refIndex]):
          replace = True
        else:
          replace = False
        strIndex += 1
        refIndex += 1
      if(replace):
        temp = 0 
        while(temp < strIndex - len(refStr)): 
          stringTemp2 = stringTemp2 + stringTemp[temp]
          temp += 1
        stringTemp = stringTemp2
        stringTemp = stringTemp + replacementStr
        stringTemp2 = ""
      else:     
        strIndex = strIndex - len(refStr)
        refIndex = 0       
    strIndex += 1    
  return stringTemp

setup()

Lab 5 Generic 14 ( my_strip() function )
def setup():
  string1 = "  Thailand    "
  print("string1 {",string1,"}")
  string1 = my_strip(string1)
  assert my_strip(string1) == string1.rstrip("")
  print("Result {",string1,"}")

def my_strip(string):
  strIndex = 0
  Refstring = " "
  stringTemp = ""
  replace = True
  while(strIndex < len(string) and replace):
    if(string[strIndex] == Refstring):
      strIndex += 1
      replace = True
    else:
      replace = False
  while(strIndex < len(string)):     
    stringTemp = stringTemp + string[strIndex]   
    strIndex +=1
  string = stringTemp
  strIndex = len(string) - 1
  replace = True
  stringTemp = ""
  while(strIndex > 0 and replace):
    if(string[strIndex] == Refstring):
      strIndex -= 1
      replace = True
    else:
      replace = False
  temp = strIndex
  strIndex = 0
  while(strIndex <= temp):     
    stringTemp = stringTemp + string[strIndex]   
    strIndex +=1
  string = stringTemp
  return string

setup()


Lab 5 Generic 15 ( my_startswith() function )
def setup():
  string1 = "Thailand"
  string2 = "Sun"
  print("String : ",string1)
  print("Referent String : ",string2)
  assert my_startswith(string1,string2) == string1.startswith(string2)
  print("String starts with referent string :",my_startswith(string1,string2))

def my_startswith(string,referentString):
  Index = 0
  startsWithRef = True
  while(Index < len(referentString) and startsWithRef):
    if(string[Index] == referentString[Index]):
      Index += 1
      startsWithRef = True
    else:
      startsWithRef = False
  return startsWithRef       

setup()



Lab 5 Generic 16 ( my_endswith() function )
def setup():
  string1 = "Thailand"
  string2 = "land"
  print("String : ",string1)
  print("Referent String : ",string2)
  assert my_endswith(string1,string2) == string1.endswith(string2)
  print("String ends with referent string :",my_endswith(string1,string2))

def my_endswith(string,referentString):
  strIndex = len(string) - 1
  refIndex = len(referentString) - 1
  endsWithRef = True
  while(strIndex >= len(string) - len(referentString) and endsWithRef):
    if(string[strIndex] == referentString[refIndex]):
      strIndex -= 1
      refIndex -= 1
      endsWithRef = True
    else:
      endsWithRef = False
  return endsWithRef       

setup()

 

ปัญหาและวิธีแก้ :
 1) ฟังก์ชั่น print error เพราะ ไม่สามารถหาตัวค่าตัวแปร array ที่ตำแหน่งที่ฟังก์ชั่น len() วัดค่าได้ 
(เปลี่ยนเงื่อนไขจาก while(index <= len(value)) เป็น  while(index < len(value)) เพราะ array ที่มีจำนวนค่า 6 ค่า จะมีเลขประจำตำแหน่งเป็น 0-5) 
 2) ฟังก์ชั่น input() error เพราะไม่ได้ใส่เครื่องหมาย " " ในวงเล็บ(ใส่เครื่องหมาย" "ล้อมข้อความที่ต้องการให้แสดงในฟังก์ชั่น input() )
 3) ไม่สามารถประกาศตัวแปรได้ (กำหนดค่าให้ตัวแปรเวลาประกาศตัวแปรเลย เช่น sum_num = 0 แทน การประกาศ sum_num โดดๆ)








วันจันทร์ที่ 14 กันยายน พ.ศ. 2558

Lab 4.5 Python generic


Lab 4.5 Python generic 1 (Sum 1 to N )
def setup():
  N = 10
  print("N = ",N)
  sum_1_to_N(N)
 
def sum_1_to_N(n):
  sum_num = 0
  while(n>0):
    if(n>1):
      print(n,"+",end="")
    else:
      print("1 = ",end="")      
    sum_num = sum_num + n
    n = n - 1    
  print(sum_num)
  print("Sum 1 to N = ",sum_num)

setup()    

Lab 4.5 Python generic 2 (Grade Calculation Program )
def setup():
  score = 80
  grade_Calculate(score)
   
def grade_Calculate(score):
  print("*******Grade Calculation Program*******")
  print("score = ",score);
  print("Grade : ",end = "")
  if(score >= 80):
    print("A")
  else:
    if(score >= 70):
      print("B")
    else:
      if(score >= 60):
        print("C")
      else:
        if(score >= 45):
          print("D")
        else:
          print("F")
  print("***************************************")
   
setup()

Lab 4.5 Python generic 3 (Power Of Ten)
def setup():
  number = 2*pow(10,12)
  power_of_ten(number)
   
def power_of_ten(number):
  print("*************Power of ten**************");
  print("Value = ",number);
  print("Power of 10 = ",end = "");
  if(number >= 1.0e30):
    print("30")
    print("Number : Nonillion")
  else:
    if(number >= 1.0e21):
      print("21")
      print("Number : Sextillion")
    else:
      if(number >= 1.0e18):
        print("18")
        print("Number : Quintillion")
      else:
         if(number >= 1.0e15):
           print("15")
           print("Number : Quadrillion")
         else:
           if(number >= 1.0e12):
             print("12")
             print("Number : Trillion")
           else:
             if(number >= 1.0e9):
               print("9")
               print("Number : Billion")
             else:
               if(number >= 1.0e6):
                 print("6")
                 print("Number : Million")
  print("**************************************")
               
setup()

Lab 4.5 Python generic 4 (Leap Year)
def setup():
  year = 1952
  leap_year(year)
   
def leap_year(year):
  print("*************Leap Year*****************")
  print("Year = ",year)
  if((year%4 == 0 and (year%100 != 0) ) or (year%400 == 0) ):
    print("Leap Year")
  else:
    print("Not a Leap Year")      
  print("***************************************")
 
setup()

Lab 4.5 Python generic 5 (Delivery Charge)
def setup():
  typePackage = 1
  typeService = 2
  weight = 6.00
  delivery_charge(typePackage,typeService,weight)
     
def delivery_charge(typePackage,typeService,weight):
  cost = 0.00
  errorCost = False
  print("*************Expressimo Delivery Service*****************")
  print("*******************Delivery Charge***********************")
  print(" Type Of Package | Type Of Service             ")
  print(" 1) Letter (1)   | 1) Next Day Priority (1)    ")
  print(" 2) Box (2)      | 2) Next Day Standard (2)    ")
  print("                 | 3) 2-Day (3)                ")
  print("")
  if(typePackage == 1):
    print("Type Of Package : Letter")
  elif(typePackage == 2):
    print("Type Of Package : Box")
  else:
    print("Type Of Package : error")    
  if(typeService == 1):
    print("Type Of Service : Next Day Priority")
  elif(typeService == 2):
    print("Type Of Service : Next Day Standard")
  elif(typeService == 3):
    print("Type Of Service : 2-Day")
  else:
    print("Type Of Service : error")
  print("Weight : ",weight,end = "");  
  if(typePackage == 1):
    print(" oz")
    if(typeService == 1):
      cost = 12.00
    if(typeService == 2):
      cost = 10.50
    if(typeService == 3 or weight > 8.00):
      errorCost = True      
  if(typePackage == 2):
    print(" pound")
    if(typeService == 1):
      if(weight <= 1):
        cost = 15.75
      else:
        cost = 15.75+((weight-1)*1.25)
    if(typeService == 2):
      if(weight <= 1):
        cost = 13.75
      else:
        cost = 13.75+((weight-1)*1.00)                    
    if(typeService == 3):
      if(weight <= 1):
        cost = 7.00
      else:
        cost = 7.00+((weight-1)*0.5)
  if(errorCost):
    print("Cost : Service not alivable")
  else:
    print("Cost : $",cost)
  print("*********************************************************")

setup()  

Lab 4.5 Python generic 6 (BMI calculator)
def setup():
 bmi_cal(65,170)

def bmi_cal(Weight,Height):
  bmi = 0
  bmi = Weight/(pow(Height/100,2))
  print("***BMI calculator***");
  print("Weight = ",Weight," Kg")
  print("Height = ",Height," cm")
  print("BMI = ",bmi)

setup()

Lab 4.5 Python generic 7 (Circumference and area of a circle calculation program)
PI = 3.1415926535897932384626433832795
def setup():
  cal_circle(8.00)

def cal_circle(Diameter):
  circum = 0
  area = 0
  circum = 2*PI*(Diameter/2)
  area = PI*(pow((Diameter/2),2))
  print("***Circumference and area of a circle calculation program***")
  print("Diameter = ",Diameter," m")
  print("Circumference = ",circum," m")
  print("Area = ",area," m^2")

setup()  

Lab 4.5 Python Generic 8 (  Multiplication table )
def setup():
  start_num = 12
  end_num = 15
  draw_multiplication_table(start_num,end_num)
   
def draw_multiplication_table(current,end_num):
  n = 1
  result = 0
  while(current <= end_num):
    print("  *",current,"* ")
    while(n <= 12):
      result = current*n
      print(current,"X",n," = ",result)
      n = n + 1
    print(" ")
    n = 1
    current = current + 1
   
setup()     
    

Lab 4.5 Python Generic 9 ( Sum of prime numbers from 1 to N )
def setup():
  end_num = 100
  sum_prime_num(end_num)
   
def sum_prime_num(end_num):
  current = 2
  sum = 0
  n = 1
  print("Prime number : ",end = "")
  while(current<=end_num):
    ref_num = 2
    prime_num = True
    while(ref_num<current and prime_num):
      if(current%ref_num==0):
        prime_num = False
      ref_num = ref_num + 1       
    if(prime_num):
      sum = sum + current
      print(current,"  ",end ="")
      if(n%15 == 0):
        print(" ")
      n = n + 1       
    current = current + 1     
  print(" ")
  print("Sum of Prime number = ",sum)
 
setup()
   
Lab 4.5 Python Generic 10 (Loan payment)
def setup():
  loan = 5000
  interestRate = 12
  monthsTerm = 12
  loan_cal(loan,interestRate,monthsTerm)

def loan_cal(loan,interestRate,monthsTerm):
  monthlyPayment = 0
  effectiveInterest = 0
  interest = 0
  principal = 0
  Balance = 0
  n = 1
  term = 1
  totalInterest = 0
  effectiveInterest = interestRate/(100*12)
  monthlyPayment = loan*(effectiveInterest/(1-pow(1+effectiveInterest,-monthsTerm))) 
  Balance = loan
  print("Loan = ",loan)
  print("Interest rate = ",interestRate,"%")
  print("Loan term = ",monthsTerm," mounth(s) (",monthsTerm/12," year(s))")
  print("Payment per mounth = ",monthlyPayment)
  print("Payment No. : Interest : Principal : Unpaid Balance : Total Interest to Date")
  while(term<=monthsTerm):
    interest = effectiveInterest*Balance
    principal = monthlyPayment-interest
    Balance = Balance-principal
    totalInterest = totalInterest+interest
    print("    ",round(term),"            ",end = "")
    print(round(interest),"         ",round(principal),"           ",end ="")
    print(round(Balance),"            ",round(totalInterest)) 
    term = term + 1
    if(n%12 == 0):
      print(" ")
    n = n + 1
     
setup()
    


วันศุกร์ที่ 11 กันยายน พ.ศ. 2558

LAB 4 Generic 1-4


LAB 4 Generic 1 ( Calculate sum of integers from 1 to N )
    void setup(){
      int N = 10;
      println("N = "+N);
      sum_1_to_N(N);
    }

    void sum_1_to_N(int n){
      int sum = 0;
      while(n>0){
        if(n>1){
          print(n+"+");
        }else{
          print("1 = ");
        }
        sum = sum + n;
        n--;
      }
      println(sum);
      println("Sum 1 to N = "+sum);
    }

LAB 4 Generic 2 (  Multiplication table )
    void setup(){
      int start_num = 12;int end_num = 15;
      draw_multiplication_table(start_num,end_num);
    }

    void draw_multiplication_table(int current,int end_num){
      int n = 1;int result;
      while(current <= end_num){
        println("  *"+current+"* ");
        while(n <= 12){
          result = current*n;
          println(current+"X"+n+" = "+result);
          n++;
        }
        println(" ");
        n = 1;
        current ++;
      }
    }

LAB 4 Generic 3 ( Sum of prime numbers from 1 to N )
    void setup(){
      int end_num = 100;
      sum_prime_num(end_num);
    }

    void sum_prime_num(int end_num){
      int current = 2;int sum = 0;int n = 1;
      print("Prime number : ");
      while(current<=end_num){
        int ref_num = 2;boolean prime_num = true;
        while(ref_num<current && prime_num){
          if(current%ref_num==0)
            prime_num = false;
          ref_num++;
        }
        if(prime_num){
          sum += current;
          print(current+"  ");
          if(n%15 == 0)
            println(" ");
          n++;
        }
        current ++;
      }
      println(" ");
      println("Sum of Prime number = "+sum);
    }

LAB 4 Generic 4 ( Loan payment)
     void setup(){
      float loan = 5000;float interestRate = 12;int monthsTerm = 12;
      loan_cal(loan,interestRate,monthsTerm);
    }

    void loan_cal(float loan,float interestRate,int monthsTerm){
      float monthlyPayment;float effectiveInterest;float interest;float principal;
      float Balance;int n = 1;int term = 1;float totalInterest = 0;
      effectiveInterest = interestRate/(100*12);
      monthlyPayment = loan*(effectiveInterest/(1-pow(1+effectiveInterest,-monthsTerm)));  
      Balance = loan;
      println("Loan = "+loan);
      println("Interest rate = "+interestRate+"%");
      println("Loan term = "+monthsTerm+" mounth(s) ("+monthsTerm/12+" year(s))");
      println("Payment per mounth = "+monthlyPayment);
      println("Payment No. : Interest : Principal : Unpaid Balance : Total Interest to Date");
      while(term<=monthsTerm){
        interest = effectiveInterest*Balance;
        principal = monthlyPayment-interest;
        Balance = Balance-principal;
        totalInterest = totalInterest+interest;
        println("    "+round(term)+"            "+round(interest)+"         "+round(principal)+"           "+round(Balance)+"            "+round(totalInterest));    
        term++;
        if(n%12 == 0)
          println(" ");
        n++;
      }
    }

ปัญหาทีพบและวิธีแก้ :
 1) ประกาศตัวแปรซ้ำกัน 2 ที่ ( ลบคำสั่งประกาศตัวแปรให้เหลือ แค่ 1 คำสั่ง)
 2) ตัวแปรแบบ int ไม่สามารถรับค่า ที่เป็นทศนิยมได้ (กำหนดใช้ตัวแปรชนิด float แทน)
 3) loop while ค้างเพราะไม่ได้กำหนดคำสั่งบวกตัวแปรกำหนดจำนวนรอบ n (เพิ่มคำสั่ง n++; ไว้หลังสุด )
 4) เครื่องหมาย } หลังจบฟังก์ชั่นหายไป (เติมเคื่องหมาย } หลังฟังก์ชั่นที่หายไป )

LAB 4 Song


Lab 4 Song
    float seta = 0;float movX = 0; 
    void setup(){
      size(470,300);
    }

    void draw(){
      float posY = 0;float posX = 0;int n =0;
      posX += 500;
      if(mousePressed && (mouseButton == LEFT)){
        background(#826478);
      }else{
        background(#4e7987);
      }
      movX = (movX+5)%1200;
      seta = (seta+(PI/30))%TWO_PI;
      posY += (70*sin(seta));
      while( (140*n)<width ){
        draw_mountain(0+(140*n),width-60,color(0+(30*n),30+(30*n),76+(30*n)));
        n++;
      }
      if(seta >0.3*PI && seta <1.3*PI){
        draw_wing_down(-movX,posX,posY,color(0,30,76));//backWing
      }else{
        draw_wing_up(-movX,posX-10,posY,color(0,30,76));
      }
      draw_Dragon(-movX,posX,posY);
      if(seta >0.3*PI && seta <1.3*PI) {//frontWing
        draw_wing_down(-movX,posX-10,posY,color(47,81,132));
      }else{
        draw_wing_up(-movX,posX-10,posY,color(47,81,132));
      }
      if(mousePressed && (mouseButton == LEFT))
        draw_fire(-movX,posX,posY);
    } 

    void draw_Dragon(float movX,float posX,float posY){
      noStroke();
      fill(#1b4077);//bodyColor
      beginShape();//body
      curveVertex(45+movX+posX,90+posY);//nose
      curveVertex(45+movX+posX,90+posY);
      curveVertex(50+movX+posX,75+posY);
      curveVertex(65+movX+posX,80+posY);
      curveVertex(70+movX+posX,70+posY);
      vertex(100+movX+posX,75+posY);//back
      vertex(117+movX+posX,65+posY);
      vertex(110+movX+posX,75+posY);
      vertex(130+movX+posX,60+posY);
      vertex(125+movX+posX,70+posY);
      vertex(145+movX+posX,58+posY);
      vertex(138+movX+posX,75+posY);
      vertex(180+movX+posX,59+posY);
      vertex(172+movX+posX,73+posY);
      vertex(220+movX+posX,55+posY);
      vertex(210+movX+posX,65+posY);
      vertex(240+movX+posX,53+posY);
      vertex(245+movX+posX,68+posY);
      curveVertex(245+movX+posX,68+posY);
      curveVertex(245+movX+posX,68+posY);
      curveVertex(247+movX+posX,65+posY);
      curveVertex(260+movX+posX,58+posY);
      curveVertex(260+movX+posX,58+posY);//endBack
      vertex(270+movX+posX,75+posY);
      vertex(300+movX+posX,110+posY);
      vertex(350+movX+posX,140+posY);//tail
      vertex(360+movX+posX,142+posY);
      vertex(430+movX+posX,125+posY);
      vertex(440+movX+posX,110+posY);
      vertex(438+movX+posX,135+posY);
      curveVertex(438+movX+posX,135+posY);
      curveVertex(438+movX+posX,135+posY);
      curveVertex(360+movX+posX,160+posY);
      curveVertex(300+movX+posX,160+posY);
      curveVertex(235+movX+posX,150+posY);
      curveVertex(195+movX+posX,120+posY);
      curveVertex(120+movX+posX,110+posY);//lowwerNeck
      curveVertex(100+movX+posX,130+posY);
      curveVertex(95+movX+posX,93+posY);
      curveVertex(70+movX+posX,90+posY);
      curveVertex(45+movX+posX,90+posY);
      curveVertex(45+movX+posX,90+posY);
      endShape(CLOSE);
      fill(#2F5184);//frontWingColor
    }

    void draw_wing_up(float movX,float posX,float posY,color c1){
      noStroke();
      fill(c1);//backWingColor
      beginShape();//backWing
      vertex(210+movX+posX,69+posY);   
      vertex(240+movX+posX,71+posY);
      vertex(255+movX+posX,86+posY);
      vertex(300+movX+posX,98+posY);
      vertex(270+movX+posX,96+posY);
      vertex(260+movX+posX,103+posY);
      vertex(250+movX+posX,95+posY);
      vertex(200+movX+posX,129+posY);
      vertex(175+movX+posX,109+posY);
      vertex(170+movX+posX,114+posY);
      vertex(165+movX+posX,103+posY);
      vertex(190+movX+posX,96+posY);    
      endShape(CLOSE);
    }

    void draw_wing_down(float movX,float posX,float posY,color c1){
      noStroke();
      fill(c1);//backWingColor
      beginShape();//backWing
      vertex(210+movX+posX,69+posY);   
      vertex(240+movX+posX,75+posY);
      vertex(255+movX+posX,120+posY);
      vertex(300+movX+posX,155+posY);
      vertex(270+movX+posX,149+posY);
      vertex(260+movX+posX,170+posY);
      vertex(250+movX+posX,147+posY);
      vertex(200+movX+posX,250+posY);
      vertex(175+movX+posX,190+posY);
      vertex(170+movX+posX,205+posY);
      vertex(165+movX+posX,170+posY);
      vertex(190+movX+posX,150+posY);    
      endShape(CLOSE);
    }

    void draw_fire(float movX,float posX,float posY){
      strokeWeight(3);
      stroke(#D3C23E);
      fill(#D3903E);
      beginShape();
      vertex(80+movX+posX,100+posY);
      vertex(80+movX+posX,100+posY);
      vertex(65+movX+posX,95+posY);
      vertex(68+movX+posX,97+posY);
      vertex(55+movX+posX,98+posY);
      vertex(58+movX+posX,100+posY);
      vertex(45+movX+posX,110+posY);
      vertex(48+movX+posX,113+posY);
      vertex(30+movX+posX,130+posY);
      vertex(34+movX+posX,128+posY);
      vertex(28+movX+posX,170+posY);
      vertex(35+movX+posX,180+posY);
      vertex(42+movX+posX,172+posY);
      vertex(52+movX+posX,178+posY);
      vertex(60+movX+posX,165+posY);
      vertex(65+movX+posX,168+posY);
      vertex(70+movX+posX,150+posY);
      vertex(73+movX+posX,170+posY);
      vertex(75+movX+posX,130+posY);
      vertex(79+movX+posX,135+posY);
      vertex(85+movX+posX,125+posY);
      vertex(90+movX+posX,120+posY);
      vertex(90+movX+posX,120+posY);
      endShape(CLOSE); 
    }

    void draw_mountain(float posX,float posY,color c1){
      noStroke();
      fill(c1);
      triangle(posX,height,70+posX,height-60,140+posX,height); 

    }






ปัญหาที่พบและวิธีแก้ :
 1) เมื่อคลิ๊กซ้ายแล้วจุดไม่เปลียนสี เพราะไม่ได้กำหนดตัวแปรสี ให้กับฟังก์ชั่นวาดจุด (กำหนดตัวแปร เปลียนสี c1 ให้กับฟังก์ชั่นวาดจุด)
 2) เครื่องหมาย } หลังจบฟังก์ชั่นหายไป (เติมเคื่องหมาย } หลังฟังก์ชั่นที่หายไป )
 3) loop while ค้างเพราะ ไม่ได้กำหนดเงื่อนไขกำหนดจำนวนรอบการทำงาน (กำหนดเงื่อนไขให้ loop while  ทำงานจนกว่าจะวาดภูเขาจนถึงขอบด้านขวา)



LAB 4 Book


Lab 4 Book
    float movX = 0;float movY = 0;float seta = 0;
    void setup(){
      size(400,270);
      } 

    void draw(){  
      background(#34acd1);//#E5E543
      color shadow = color(45,110,130);int n = 0;
      int radius = 50;
      movX = radius*cos(seta);
      movY = radius*sin(seta);
      if((seta > 0.3*PI && seta < 0.7*PI )||(seta < -1.3*PI && seta > -1.7*PI )){
        background(#E5E543);//yellow
        shadow = color(129,128,36);
        }
      if((seta > 1.2*PI && (seta) < 1.6*PI)||(seta < -0.4*PI && (seta) > -0.8*PI)){
        background(#3C53E3);//purple
        shadow = color(20,28,75);
        }
        if(keyPressed){
          if(key == 'd'||keyCode == RIGHT){
            seta = (seta+(PI/60))%TWO_PI; 
          }else if(key == 'a'||keyCode == LEFT){
            seta = (seta-(PI/60))%TWO_PI;
            }
          }  
      while( movX+(n*50*pow(-1,n)) < 1.5*width){
        draw_book(movX+(n*150*pow(-1,n)),movY,shadow);
        n++;
      }
      //println(seta/PI+"PI");
      }

    void draw_book(float movX,float movY,color shadow){
      fill(shadow);//shadowColor
      noStroke();
      quad(120+movX,175+movY,300+movX,175+movY,305+movX,200+movY,175+movX,200+movY);//shadow
      stroke(6);
      fill(#42b751);//coverColor
      quad(165+movX,35+movY,295+movX,35+movY,295+movX,70+movY,175+movX,70+movY);//backCover
      fill(#d3d3d3);//paperColor
      quad(168+movX,38+movY,290+movX,38+movY,298+movX,40+movY,175+movX,40+movY);//paper
      fill(#42b751);//coverColor
      quad(175+movX,40+movY,305+movX,40+movY,305+movX,200+movY,175+movX,200+movY);//cover
      fill(#4d9155);//bookShadow
      quad(165+movX,35+movY,175+movX,40+movY,175+movX,200+movY,165+movX,195+movY);//saddle
      fill(#c4d170);//coverColor2
      quad(190+movX,70+movY,290+movX,70+movY,290+movX,180+movY,190+movX,180+movY);//magin
      fill(#c440c2);//doorColor
      quad(220+movX,100+movY,260+movX,100+movY,260+movX,150+movY,220+movX,150+movY);//door
      fill(#d3d3d3);//doorKnobColor
      ellipse(255+movX,125+movY,5,5);//doorKnob  
      }




ปัญหาทีพบ และวิธีแก้ :
 1) ประกาศตัวแปรซ้ำกัน 2 รอบ (ลบคำสั่งประกาศตัวแปรออก 1 คำสั่ง)
 2) ไม่ได้ใส่เงื่อนไขให้ loop while (ใส่เงื่อนไขให้ loop while)
 3) เงื่อนไขที่ใส่ใน ฟังก์ชั่น while เป็นค่าไม่ใช่เงื่อนไข (เปลี่ยนเครื่องหมาย = เป็น ==)


LAB 4 Movie


Lab 4 Movie
    float seta=0.00;color c1 = color(198,91,62);
    void setup(){
      size(750,350);  
    }
  
    void draw(){
      int posX = 0;int posY = 50;int dot_posX = mouseX;int dot_posY = mouseY;int n = 0;
      seta = (PI/50)+seta;
      background(#F4F593); 
      draw_ak(posX,posY);   
      while( dot_posX+(20*n) < width ){
        draw_moveDot(dot_posX+(20*n),dot_posY,10,(PI/6)*n);
        n++;
      }
    }

    void draw_ak(int posX,int posY){
      fill(#B78934);
      noStroke();
      quad(38+posX,130+posY,213+posX,129+posY,215+posX,162+posY,52+posX,215+posY);//back
      quad(228+posX,113+posY,403+posX,114+posY,403+posX,162+posY,213+posX,129+posY); //body
      triangle(212+posX,128+posY,403+posX,161+posY,212+posX,162+posY); //body2
      quad(215+posX,162+posY,249+posX,161+posY,228+posX,223+posY,201+posX,213+posY); //hand_grib
      quad(405+posX,105+posY,540+posX,105+posY,543+posX,157+posY,371+posX,163+posY);//front
      quad(369+posX,150+posY,439+posX,155+posY,439+posX,164+posY,371+posX,169+posY);//low_front
      quad(359+posX,96+posY,392+posX,94+posY,395+posX,115+posY,358+posX,115+posY);//ironSight
      quad(540+posX,118+posY,608+posX,118+posY,609+posX,153+posY,541+posX,153+posY);//tube
      quad(605+posX,109+posY,626+posX,108+posY,647+posX,159+posY,608+posX,161+posY);//junction
      quad(614+posX,142+posY,724+posX,141+posY,724+posX,152+posY,646+posX,153+posY);//F_tube
      quad(691+posX,93+posY,703+posX,93+posY,704+posX,142+posY,695+posX,142+posY);//F_Sight
      noFill();
      stroke(#B78934);
      strokeWeight(3);
      beginShape();//triger_protect
      curveVertex(282+posX,160+posY);
      curveVertex(282+posX,160+posY);
      curveVertex(281+posX,177+posY);
      curveVertex(270+posX,185+posY);
      curveVertex(243+posX,181+posY);
      curveVertex(243+posX,181+posY);
      endShape();
      fill(#B78934);
      stroke(#B78934);
      strokeWeight(1);
      beginShape();//magazine
      vertex(302+posX,160+posY);
      vertex(302+posX,160+posY);
      vertex(304+posX,181+posY);
      vertex(308+posX,200+posY);
      vertex(312+posX,215+posY);
      vertex(319+posX,227+posY);
      vertex(325+posX,239+posY);
      vertex(331+posX,249+posY);
      vertex(337+posX,260+posY);
      vertex(385+posX,242+posY);
      vertex(374+posX,226+posY);
      vertex(370+posX,220+posY);
      vertex(364+posX,207+posY);
      vertex(359+posX,195+posY);
      vertex(356+posX,182+posY);
      vertex(354+posX,175+posY);
      vertex(352+posX,160+posY);
      vertex(305+posX,160+posY);
      endShape();
      fill(#F4F593);
      stroke(#B78934);
      quad(541+posX,129+posY,608+posX,129+posY,608+posX,141+posY,542+posX,142+posY);//space_between_tube
      fill(#B78934);
      ellipse(220+posX,121+posY, 10, 10);//back_botton
      strokeWeight(3);
      line(254+posX,162+posY,257+posX,175+posY);//triger
      line(257+posX,175+posY,262+posX,181+posY);
      line(262+posX,181+posY,261+posX,165+posY);
      line(261+posX,165+posY,263+posX,159+posY);
      quad(725+posX,145+posY,736+posX,144+posY,737+posX,149+posY,725+posX,149+posY);//front_cap
    }

    void draw_moveDot(int posX,int posY,int diameter,float delay){ //dotFunction   
      noStroke();
      fill(c1);
      ellipse(posX,posY+(125*sin(seta+delay)),diameter,diameter); 
    }
  
    void mouseClicked(){
      if(c1 == color(198,91,62)){
        c1 = color(46,147,37);
      }else{
        c1 = color(198,91,62);
      }
    }


ปัญหาทีพบและวิธีแก้ :
 1) เมื่อคลิ๊กซ้ายแล้วจุดไม่เปลียนสี เพราะไม่ได้กำหนดตัวแปรสี ให้กับฟังก์ชั่นวาดจุด (กำหนดตัวแปร เปลียนสี c1 ให้กับฟังก์ชั่นวาดจุด)
 2) เครื่องหมาย } หลังจบฟังก์ชั่นหายไป (เติมเคื่องหมาย } หลังฟังก์ชั่นที่หายไป )
 3) เครื่องหมาย ; หลังบรรทัดคำสั่งหายไป (เติมเครื่องหมาย ; หลังบรรทัดคำสั่ง)
 4) ตัวแปรที่ใช้กำที่กำหนดไม่ตรงกัน (แก้ชื่อตัวแปรให้เหมือนกัน)



LAB 4 Bird


Lab 4 Bird
   int wingMove = 0;int wingDelay;
    void setup(){
      size(800,400); 
      }

    void draw(){
      int posX = mouseX;int posY = mouseY;float newSizeX = 50;float newSizeY = 50;
      float pixelX;float pixelY;int n = 0;
      draw_background();
      quad(0,height-20,width,height-20,width,height,0,height);
      pixelX = newSizeX/50;
      pixelY = newSizeY/50;
      posX -= 100;//setBirdPos
      posY -= 100;
      if(posY>=224){
        posY = 224; 
        }
      while(posX+(n*50) < width){
        draw_wings(posX+(n*(newSizeX+130)),posY,pixelX,pixelY);
        draw_bird(posX+(n*(newSizeX+130)),posY,pixelX,pixelY);
        n++;
      }
      //println(posY);
      if (posY < 80){
        wingDelay = 10; 
      }else if(posY >= 80 && posY < 190){
        wingDelay = 6;   
      }else{
        wingDelay = 3; 
        }
      if (frameCount%wingDelay == 2){
        wingMove = 30;
      }else{
        wingMove = 0;
        }
      if(mousePressed&&(mouseButton == LEFT)){
        n = 0;
        while(posX+(n*50) < width){
          draw_eye_2(posX+(n*(newSizeX+130)),posY,pixelX,pixelY);
          draw_mouth(posX+(n*(newSizeX+130)),posY,pixelX,pixelY);
          n++;
        }
      }else{
        n = 0;
        while(posX+(n*50) < width){
          draw_eye_1(posX+(n*(newSizeX+130)),posY,pixelX,pixelY);
          n++;
        }
      }  
      println(pixelX+" "+pixelY);
    }

    void draw_bird(float posX,float posY,float pixelX,float pixelY){
      fill(#00D60F);//hairCol
      curve((150*pixelX)+posX,(80*pixelY)+posY,(85*pixelX)+posX,(90*pixelY)+posY,(90*pixelX)+posX,(75*pixelY)+posY,(200*pixelX)+posX,(250*pixelY)+posY);//hair
      curve((90*pixelX)+posX,(150*pixelY)+posY,(90*pixelX)+posX,(79*pixelY)+posY,(100*pixelX)+posX,(75*pixelY)+posY,(100*pixelX)+posX,(150*pixelY)+posY); 
      curve((90*pixelX)+posX,(150*pixelY)+posY,(100*pixelX)+posX,(75*pixelY)+posY,(110*pixelX)+posX,(80*pixelY)+posY,(110*pixelX)+posX,(150*pixelY)+posY); 
      fill(#ACD302);//bodyCol
      ellipse((pixelX*100)+posX,(pixelY*100)+posY,50*pixelX,50*pixelY);//body
      }

    void draw_wings(float posX,float posY,float newSizeX,float newSizeY){
      noStroke();
      fill(#2262B9);
      triangle((newSizeX*120)+posX,(newSizeY*90)+posY,(newSizeX*120)+posX,(newSizeY*110)+posY,(newSizeX*170)+posX,(newSizeY*(100+wingMove))+posY);//rightWing 
      triangle((newSizeX*80)+posX,(newSizeY*90)+posY,(newSizeX*80)+posX,(newSizeY*110)+posY,(newSizeX*30)+posX,(newSizeY*(100+wingMove))+posY);//leftWing   
      }

    void draw_mouth(float posX,float posY,float newSizeX,float newSizeY){
      strokeWeight(1);
      fill(255);
      quad((newSizeX*90)+posX,(newSizeY*100)+posY,(newSizeX*110)+posX,(newSizeY*100)+posY,(newSizeX*110)+posX,(newSizeY*150)+posY,(newSizeX*90)+posX,(newSizeY*150)+posY);
      fill(#D30B33); 
      quad((newSizeX*90)+posX,(newSizeY*105)+posY,(newSizeX*110)+posX,(newSizeY*105)+posY,(newSizeX*110)+posX,(newSizeY*145)+posY,(newSizeX*90)+posX,(newSizeY*145)+posY);
      }

    void draw_background(){
      fill(#168109);//floor
      stroke(1);
      background(#50350D); 
      }

    void draw_eye_2(float posX,float posY,float newSizeX,float newSizeY){
      stroke(3);
      fill(255);//leftEye
      ellipse((newSizeX*90)+posX,(newSizeY*90)+posY,15*newSizeX,15*newSizeY);
      fill(0);
      ellipse((newSizeX*87)+posX,(newSizeY*93)+posY,5*newSizeX,5*newSizeY);
      fill(255);//rightEye
      ellipse((newSizeX*110)+posX,(newSizeY*90)+posY,15*newSizeX,15*newSizeY);
      fill(0);
      ellipse((newSizeX*113)+posX,(newSizeY*87)+posY,2*newSizeX,2*newSizeY); 
      }

    void draw_eye_1(float posX,float posY,float newSizeX,float newSizeY){
      stroke(3);
      fill(255);//leftEye
      ellipse((newSizeX*90)+posX,(newSizeY*90)+posY,15*newSizeX,15*newSizeY);
      fill(0);
      ellipse((newSizeX*90)+posX,(newSizeY*90)+posY,5*newSizeX,5*newSizeY);
      fill(255);//rightEye
      ellipse((newSizeX*110)+posX,(newSizeY*90)+posY,15*newSizeX,15*newSizeY);
      fill(0);
      ellipse((newSizeX*110)+posX,(newSizeY*90)+posY,5*newSizeX,5*newSizeY); 
      }


ปัญหาที่พบและวิธีแก้ :
 1) ประกาศตัวแปรตัวเดียวกันซ้ำกัน 2 รอบ ( ลบคำสั่งประกาศตัวแปร 1 คำสั่งให้เหลือเพียงคำสั่งเดียว)
 2) loop while ไม่ทำงานเพราะไม่ตรงเงื่อนไข (แก้เครื่งหมาย > เป็น <)




LAB 4 Balloon


Lab 4 Balloon
    color col1 = color(190,145,242);color col2 = color(113,81,27);
    void setup(){
      size(400,400);
    }

    void draw(){
      background(#FEFFC9);
      int posX = mouseX;int posY = mouseY;int balloonSize = 50;int string_lenght = 100;
      draw_balloon(posX,posY,balloonSize,string_lenght);
    }

    void draw_balloon(int posX,int posY,int balloonSize,int string_lenght){
      int count = 0;
      while( posX+(count*(balloonSize+5)) < width ){
        stroke(col2);
        fill(col1);
        line(posX+(count*(balloonSize+5)),posY+25,posX+(count*(balloonSize+5)),posY+string_lenght+25);
        strokeWeight(2);
        ellipse(posX+(count*(balloonSize+5)),posY,balloonSize,balloonSize);
        strokeWeight(3);
        count++;
      }
    }

    void mouseClicked(){
      if(col1 == color(190,145,242) || col2 == color(113,81,27)){
        col1 = color(255,129,148);
        col2 = color(56,157,149);
      }else{
        col1 = color(190,145,242);
        col2 = color(113,81,27);
      }
    }

ปัญหาที่พบและวิธีแก้ :
  1) เครื่องหมาย } หลังฟังก์ชั่นหายไป (เติมเครื่องหมาย } หลังฟังก์ชั่น)
  2) ไม่สามารถออกจาก loop while ได้ เพราะเงื่อนไขเป็นจริงทุกครั้ง (กำหนดตัวแปร count แล้วสั่งให้ตัวแปรเพิ่มค่าขึ้นทีละ 1 เมื่อทำงานในแต่ละรอบ และใส่ตัวแปร count ลงในเงื่อนไขของ while )


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

LAB 3 Book


Lab 3 Book
    float movX = 0;float movY = 0;float seta = 0;
    void setup(){
      size(400,270);
      }

    void draw(){ 
      background(#34acd1);//#E5E543
      color shadow = color(45,110,130);
      int radius = 50;
      movX = radius*cos(seta);
      movY = radius*sin(seta);
      if((seta > 0.3*PI && seta < 0.7*PI )||(seta < -1.3*PI && seta > -1.7*PI )){
        background(#E5E543);//yellow
        shadow = color(129,128,36);
        }
      if((seta > 1.2*PI && (seta) < 1.6*PI)||(seta < -0.4*PI && (seta) > -0.8*PI)){
        background(#3C53E3);//purple
        shadow = color(20,28,75);
        }
        if(keyPressed){
          if(key == 'd'||keyCode == RIGHT){
            seta = (seta+(PI/60))%TWO_PI;
          }else if(key == 'a'||keyCode == LEFT){
            seta = (seta-(PI/60))%TWO_PI;
            }
          } 
      draw_book(movX,movY,shadow);
      //println(seta/PI+"PI");
      }

    void draw_book(float movX,float movY,color shadow){
      fill(shadow);//shadowColor
      noStroke();
      quad(120+movX,175+movY,300+movX,175+movY,305+movX,200+movY,175+movX,200+movY);//shadow
      stroke(6);
      fill(#42b751);//coverColor
      quad(165+movX,35+movY,295+movX,35+movY,295+movX,70+movY,175+movX,70+movY);//backCover
      fill(#d3d3d3);//paperColor
      quad(168+movX,38+movY,290+movX,38+movY,298+movX,40+movY,175+movX,40+movY);//paper
      fill(#42b751);//coverColor
      quad(175+movX,40+movY,305+movX,40+movY,305+movX,200+movY,175+movX,200+movY);//cover
      fill(#4d9155);//bookShadow
      quad(165+movX,35+movY,175+movX,40+movY,175+movX,200+movY,165+movX,195+movY);//saddle
      fill(#c4d170);//coverColor2
      quad(190+movX,70+movY,290+movX,70+movY,290+movX,180+movY,190+movX,180+movY);//magin
      fill(#c440c2);//doorColor
      quad(220+movX,100+movY,260+movX,100+movY,260+movX,150+movY,220+movX,150+movY);//door
      fill(#d3d3d3);//doorKnobColor
      ellipse(255+movX,125+movY,5,5);//doorKnob 
      }





 



ปัญหาที่พบและวิธีแก้ :
 1) ฟังก์ชั่นวาดหนังสือไม่สามารถรับตัวแปรโค้ดสีได้ (กำหนดฟังก์ชั่นให้สามารรับค่า ตัวแปรชนิด  color )
 2) เมื่อกดปุมลูกศรซ้าย-ขวา รูปไม่ขยับ แต่ปุ่ม a d สามารถขยับได้ (แก้โค้ดจาก key == 'LEFT' เป็น keyCode == LEFT)
 3) เมื่อกดปุมลูกศรซ้าย หรือ ปุ่ม a สีฉากหลังไม่เปลี่ยนตามแบบที่คาดไว้ (เพิ่มเงือนไขที่ฟังชั่น if พิจาณา ตัวแปร seta ที่เป็นลบ)