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

Lab4x - Summation of Prime Number

def setup():
  print("Sum of Prime Number\n")
  sum_prime(0,20)
  sum_prime(0,100)

def sum_prime(minNumber,maxNumber):
   print(minNumber,"-",maxNumber)
   sumPrime = 0
   while (minNumber <= maxNumber):
      if (prime(minNumber)):
         sumPrime += minNumber
      minNumber += 1
   print("Sum = ",sumPrime)

def prime(value):
   num = 2
   while (num <= value):
      if (value%num == 0):
         if (value == num):
            return True
         else:
            return False
      num += 1
   return False
 
setup()

Lab4x - Summation of Integers

def setup():
   print("Sum of Integers\n")
   sumIntegers(1,5)
   sumIntegers(1,20)
 
def sumIntegers(minNumber,maxNumber):
   print(minNumber," + ",(minNumber+1)," + ... + ",maxNumber)
   sumIntegers = 0
   while (minNumber <= maxNumber):
      sumIntegers = sumIntegers + minNumber
      minNumber = minNumber+1
   print("Sum = ",sumIntegers)
 
setup()

Lab4x - Service Charge

def setup():
    packageType = 2
    serviceType = 3
    weight = 16
    print("Expressimo Delivery Service")
    charge(packageType, serviceType, weight)

def charge(package_type, service_type, weight):
    cost = 0
    if (package_type == 1):
        print("Package : LETTER");
        if (service_type == 1):
            print("Service : Next day Priority")
            if (weight <= 8):
                cost = 12
            else:
                cost = 0

        elif (service_type == 2):
            print("Service : Next day Standard")
            if (weight <= 8):
                cost = 10.5
            else:
                cost = 0

        elif (service_type == 3):
            print("Service : No Service")
        else:
            print("Service : ERROR")

        print("Weight : ",weight," Oz")
    elif (package_type == 2):
        print("Package : BOX")
        if (service_type == 1):
            print("Service : Next day Priority")
            if (weight <= 1):
                cost = 15.75
            else:
                cost = cal_service1(weight)

        elif (service_type == 2):
            print("Service : Next day Standard")
            if (weight <= 1):
                cost = 13.75
            else:
                cost = cal_service2(weight)

        elif (service_type == 3):
            print("Service : Two-days")
            if (weight <= 1):
                cost = 7.00
            else:
                cost = cal_service3(weight)

        else:
            print("Service : ERROR")

        print("Weight :",weight,"Pound")

    else:
        print("Package : ERROR")

    print("Charge : $",cost)

def cal_service1(weight):
  firstCharge = 15.75
  priority_charge = firstCharge + ((weight-1)*1.25)
  return priority_charge

def cal_service2(weight):
  firstCharge = 13.75;
  standard_charge = firstCharge + ((weight-1)*1)
  return standard_charge

def cal_service3(weight):
  firstCharge = 7.00
  twodays_charge = firstCharge + ((weight-1)*0.5)
  return twodays_charge

setup()

Lab4x - Loan

def setup():
    loanAmount = 5000
    interestPercent = 12
    loanTerm = 12

    loan(loanAmount,interestPercent,loanTerm);

def loan(loanAmount,interestPercent,loanTerm):
    print("Payment No.   Principal    Interest    Balance       Total Interest")
    showValue(loanAmount,interestPercent,loanTerm)

def showValue(loanAmount,interestPercent,loanTerm):
    interestRate = interestPercent/100
    effectiveInterest = interestRate/12
    monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)))
   
    interest = 0
    totalInterest = 0
    principal = 0
    balance = loanAmount
    term = 1
    while (term <= loanTerm):
        interest = balance * effectiveInterest;
        principal = monthlyPayment-interest;
        totalInterest += interest;
        balance -= principal;
        if (balance <= 0):
            balance = 0

        print(term,"           ","$%.2f"%principal,"    ","%.2f"%interest,"    ","$%.2f"%balance,"    ","$%.2f"%totalInterest)
        term += 1

setup()

Lab4x - Power of Ten

def setup():
   print("Power of 10\n")
   power_of_ten(9)
   power_of_ten(100)
 
def power_of_ten(power):
   print("Power =",power)
   if (power == 6):
      print("'Million'")
   elif (power == 9):
      print("'Billion'")
   elif (power == 12):
      print("'Trillion'")
   elif (power == 15):
      print("'Quadrillion'")
   elif (power == 18):
      print("'Quintillion'")
   elif (power == 21):
      print("'Setillion'")
   elif (power == 30):
      print("'Nonillion'")
   elif (power == 100):
      print("'Googol'")
   else:
      print("'Error'")
setup()

Lab4x - Multiplication Table

def setup():
    multi_table(9)

def multi_table(number):
    minMultiplier = 1
    maxMultiplier = 12
    while (minMultiplier <= maxMultiplier):
        answer = number*minMultiplier
        print(number," x ",minMultiplier," = ",answer)
        minMultiplier += 1

setup()

Lab4x - Leafyear

def setup():
   print("Leap Year?\n")
   leapyear(1796)
   leapyear(2000)
   leapyear(1800)
   leapyear(2015)

def leapyear(year):
   if((year%4 == 0 and year%100 != 0) or (year%4 == 0 and year%100 == 0 and year%400 == 0)):
      print("'",year,"' is a leap year.")
   else:
      print("'",year,"' isn't a leap year.")
 
setup()

Lab4x - Grade

def setup():
   cal_grade(99)

def cal_grade( score):
   print("Score = ",score)
   if (score <= 100 and score >= 80):
      print("Grade A")
   elif (score <= 79 and score >= 70):
      print("Grade B")
   elif (score <= 69 and score >= 60):
      print("Grade C")
   elif (score <= 59 and score >= 50):
      print("Grade D")
   elif (score < 50):
      print("Grade F")
      print("See you again next year.")
     
setup()

Lab4x - Circle

def setup():
    rad = 15
    print("Radius =",rad)
    print("Diameter =",cal_dia(rad))
    print("Area =",cal_area(rad))
    print("Circumference =",cal_cir(rad))

def cal_cir(rad):
    cir = 2*3.14*rad
    return cir

def cal_area(rad):
    area = 3.14*rad*rad
    return area

def cal_dia(rad):
    dia = rad*2
    return dia

setup()

Lab4x - BMI

def setup():
   print("BMI =",cal_BMI(173,60))

def cal_BMI(high,weight):
   print("Weight =",weight,"kg")
   print("Height =",high,"cm")
   highM = high/100
   BMI = weight/(highM*highM)
   return BMI
 
setup()

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

Lab4 - balloons



float x =25;
int y = 400;
int num = 8;

void setup() {
  size(750, 400);
}

void draw() {
  background(255);
  int count = 0;
  int space = 0;

  while (count < num) {
    draw_balloon(x+space, y, 50, 50);
    count += 1;
    space += 50;
  }

  y -= 4;
  if (y == -100) {
    y = 400;
    x = random(25, width-(count*50-25));
  }
}

void draw_balloon(float x, int y, int balloonSize, int string) {
  fill(#FF0000);
  ellipse(x, y, balloonSize, balloonSize);
  line(x, y+balloonSize/2, x, (y+balloonSize/2)+string);
}

void mousePressed() {
  if (mouseButton == LEFT) {
    num +=1;
    if (num>15) {
      num = 15;
    }
  }
  if (mouseButton == RIGHT) {
    num -=1;
    if (num < 1) {
      num = 1;
    }
  }
}

Lab4 - Birds



int wingFly;
int x= 30;
int birdSize = 50;
void setup() {
  size(700, 500);
}

void draw() {
  background(#AAFFFF);
  int num = 0;
  int count = 7;
  int space = 0;
  wingFly = mouseY;
  if (mouseY > height/2) {
    if (frameCount%40 > 20) {
      wingFly += birdSize/3;
    } else {
      wingFly -= birdSize/3;
    }
  } else if (mouseY < height/2) {
    if (frameCount%20 > 10) {
      wingFly += birdSize/3;
    } else {
      wingFly -= birdSize/3;
    }
  }
  while (num < count) {
    flybird(mouseX+space, mouseY, birdSize);
    num++;
    if (num%2 == 0) {
      space += num*80;
    } else if (num%2 != 0) {
      space -= num*80;
    }
  }
}

void flybird(int x, int y, int birdSize) {
  strokeWeight(3);
  fill(#FF00FF);
  ellipse(x, y, birdSize, birdSize);
  fill(0);
  ellipse(x-(birdSize/5), y, birdSize/4, birdSize/3);
  ellipse(x+(birdSize/5), y, birdSize/4, birdSize/3);
  fill(255);
  ellipse(x-(birdSize/5), y, birdSize/6, birdSize/5);
  ellipse(x+(birdSize/5), y, birdSize/6, birdSize/5);
  fill(#FFFF00);
  triangle(x-(birdSize/4), y+(birdSize/4), x+(birdSize/4), y+(birdSize/4), x, y+birdSize/1.5);

  line(x-(birdSize/2), y, x-birdSize, wingFly);
  line(x+(birdSize/2), y, x+birdSize, wingFly);
}

Lab4 - Multiplication Table



void setup() {
  size(100, 250);
  background(0);
  multi_table(8);
}

void multi_table(int number) {
  int minMultiplier = 1;
  int maxMultiplier = 12;
  int space = 20;
  while (minMultiplier <= maxMultiplier) {
    int answer = number*minMultiplier;
    text(number+" x "+minMultiplier+" = "+answer, width/6, space);
    minMultiplier += 1;
    space += 20;
  }
}

Lab4 - Summation of Prime Number



void setup() {
  size(250, 150);
  background(50);
  int minNumber = 0;
  int maxNumber = 20;
  int sum = 0;

  int space = 30;
  textAlign(CENTER);
  text("Sum of Prime Number", width/2, height/2-space);
  text(minNumber+" - "+maxNumber, width/2, height/2);
  while (minNumber <= maxNumber) {
    if (prime(minNumber)) {
      sum += minNumber;
    }
    minNumber++;
  }
  text("Sum = "+sum, width/2, height/2+space);
}
boolean prime(int value) {
  int num = 2;
  while (num <= value) {
    if (value%num == 0) {
      if (value == num) {
        return true;
      } else {
        return false;
      }
    }
    num++;
  }
  return false;
}

Lab4 - Summation of Integers



void setup() {
  int minNumber = 1;
  int maxNumber = 5;
  int sum = 0;

  size(200, 150);
  background(255);
  int space = 30;
  textAlign(CENTER);
  fill(0);
  text("Sum of Integers", width/2, height/2-space);
  text(minNumber+" + "+(minNumber+1)+" + ... + "+maxNumber, width/2+3, height/2);

  while (minNumber <= maxNumber) {
    sum += minNumber;
    minNumber +=1;
  }

  text("Sum = "+sum, width/2, height/2+space);
}

Lab4 - Loan



void setup() {
  size(450, 270);
  background(0);
  float loanAmount = 5000;
  float interestPercent = 12;
  int loanTerm = 12;

  loan(loanAmount, interestPercent, loanTerm);
}

void loan(float loanAmount, float interestPercent, int loanTerm) {
  float interestRate = interestPercent/100;
  float effectiveInterest = interestRate/12;
  float monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)));
  head(30, 20);

  float interest = 0;
  float totalInterest = 0;
  float principal = 0;
  float balance = loanAmount;
  int term = 1;
  while (term <= loanTerm) {
    interest = balance * effectiveInterest;
    principal = monthlyPayment-interest;
    totalInterest += interest;
    balance -= principal;
    if (balance <= 0) {
      balance = 0;
    }
    showValue(term, balance, principal, interest, totalInterest, 30, 20*(1+term));
    term++;
  }
}

void showValue(int term, float balance, float principal, float interest, float totalInterest, int x, int y) {
  int space = 80;
  fill(#FFFF00);
  text(term, x, y);
  text("$"+nf(principal, 1, 2), x+space, y);
  text("$"+nf(interest, 1, 2), x+space*2, y);
  text("$"+nf(balance, 1, 2), x+space*3, y);  text("$"+nf(totalInterest, 1, 2), x+space*4, y);
}

void head(int x, int y) {
  int space = 80;
  fill(#FF0000);
  text("Payment No.", x, y);
  text("Principal", x+space, y);
  text("Interest", x+space*2, y);
  text("Balance", x+space*3, y);  text("Total Interest", x+space*4, y);
}

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

Lab3-Battery(Interactive)

int volumeposY = 110;
int volumeHeight = 380;
int batt_usage;
int posY_change;
int volumeColor = #008B00;
int update = 0;

void setup() {
  size(400, 600);
}

void draw() {
  int batteryposX = 100, batteryposY = 100;
  frameRate(50);
  background(0);

Lab3-Delivery Charge



int space = 50;
void setup() {
  size(650, 300);
  background(#DDFFFF);
  int x =50, y =50;

  int packageType = 2;
  int serviceType = 3;
  float weight = 16;

  fill(0);
  textSize(40);
  text("Expressimo Delivery Service", x, y);
  charge(x+space, y+space, packageType, serviceType, weight);
}

void charge(int x, int y, int package_type, int service_type, float weight) {
  float cost =0;
  fill(#FF0000);
  textSize(30);
  if (package_type == 1) {
    text("Package : LETTER", x, y);
    if (service_type == 1) {
      text("Service : Next day Priority", x, y+space);
      if (weight <= 8) {
        cost = 12;
      } else {
        cost = 0;
      }
    } else if (service_type == 2) {
      text("Service : Next day Standard", x, y+space);
      if (weight <= 8) {
        cost = 10.5;
      } else {
        cost = 0;
      }
    } else if (service_type == 3) {
      text("Service : No Service", x, y+space);
    } else {
      text("Service : ERROR", x, y+space);
    }
    text("Weight : "+weight+" Oz", x, y+space*2);
  } else if (package_type == 2) {
    text("Package : BOX", x, y);
    if (service_type == 1) {
      text("Service : Next day Priority", x, y+space);
      if (weight <= 1) {
        cost = 15.75;
      } else {
        cost = cal_service1(weight);
      }
    } else if (service_type == 2) {
      text("Service : Next day Standard", x, y+space);
      if (weight <= 1) {
        cost = 13.75;
      } else  {
        cost = cal_service2(weight);
      }
    } else if (service_type == 3) {
      text("Service : Two-days", x, y+space);
      if (weight <= 1) {
        cost = 7.00;
      } else  {
        cost = cal_service3(weight);
      }
    } else {
      text("Service : ERROR", x, y+space);
    }
    text("Weight : "+weight+" Pound", x, y+space*2);
  } else {
    text("Package : ERROR", x, y);
  }
  text("Charge : $"+cost, x, y+space*3);
}
float cal_service1(float weight) {
  float priority_charge;
  float firstCharge = 15.75;
  priority_charge = firstCharge + ((weight-1)*1.25);
  return priority_charge;
}

float cal_service2(float weight) {
  float standard_charge;
  float firstCharge = 13.75;
  standard_charge = firstCharge + ((weight-1)*1);
  return standard_charge;
}

float cal_service3(float weight) {
  float twodays_charge;
  float firstCharge = 7.00;
  twodays_charge = firstCharge + ((weight-1)*0.5);
  return twodays_charge;
}

Lab3-Leap Year



void setup() {
  int x = 50, y = 50;
  int space = 50;
  background(#AAFF88);
  size(500, 300);
  textSize(40);
  fill(#FF0000);
  text("Leap Year?", x, y);
  leapyear(x+space, y+space, 1796);
  leapyear(x+space, y+space*2, 2000);
  leapyear(x+space, y+space*3, 1800);
  leapyear(x+space, y+space*4, 2015);
}

void leapyear(int x, int y, int year) {
  fill(#008D00);
  textSize(30);
  if (is_leapYear(year)) {
    text("'"+year+"' is a leap year.", x, y);
  } else {
    text("'"+year+"' is not a leap year.", x, y);
  }
}

boolean is_leapYear(int year) {
   boolean result = false;
   if((year%4 == 0 && year%100 != 0) || (year%4 == 0 && year%100 == 0 && year%400 == 0)){
   result = true;
   }else{
   result = false;
   }
   return result;
}

Lab3-Power of 10



int space = 70;
void setup() {
  background(#2222FF);
  size(400, 250);

  int x = width/2, y = 70;
  textAlign(CENTER);
  textSize(60);
  fill(255);
  text("Power of 10", x, y);
  power_of_ten(x, y+space, 9);
}
void power_of_ten(int x, int y, int power) {
  textAlign(CENTER);
  textSize(40);
  fill(0);
  text("10^"+power, x, y);
  if (power == 6) {
    text("'Million'", x, y+space);
  } else if (power == 9) {
    text("'Billion'", x, y+space);
  } else if (power == 12) {
    text("'Trillion'", x, y+space);
  } else if (power == 15) {
    text("'Quadrillion'", x, y+space);
  } else if (power == 18) {
    text("'Quintillion'", x, y+space);
  } else if (power == 21) {
    text("'Sextillion'", x, y+space);
  } else if (power == 30) {
    text("'Nonillion'", x, y+space);
  } else if (power == 100) {
    text("'Googol'", x, y+space);
  }
}

Lab3-Grade



void setup() {
  size(400, 200);
  int x = width/2, y = 70;
  int space = 80;

  int score = 99;
  background(#0088CC);
  fill(255);
  textAlign(CENTER);
  textSize(50);
  text("Score = "+score, x, y);
  grade(x, y+space, score);
}

void grade(int x, int y, int score) {

   if (score <= 100 && score >= 80) {
    text("Grade A", x, y);
  } else if (score <= 79 && score >= 70) {
    text("Grade B", x, y);
  } else if (score <= 69 && score >= 60) {
    text("Grade C", x, y);
  } else if (score <= 59 && score >= 50) {
    text("Grade D", x, y);
  } else if (score < 50) {
    text("Grade F", x, y);
    textSize(15);
    text("See you again next year.", x, y+20);
  }
}

Lab3-SPYAIR(Interactive)

float spin = 0;
float speed = 0;
int textposX = 360;
int R = 255, G = 255, B = 255;
int x_axis = -2;
void setup() {
  size(700, 400);
}

Lab3-Gintama Elizabeth(Interactive)

int eyeSize = 100;
int R = 255;
int G = 255;
int B = 255;

void setup() {
  size(500, 600);
  background(100);
  strokeWeight(5);
}

Lab3-Jurassic Brown(Interactive)

int posX = 300;
int R = 255, G = 0, B = 0;
int x_axis_moved;

void setup() {
  size(600, 400);
}

void draw() {
  background(0);
  logo(posX, 200);
}

Lab3-Bird



int wingFly;
int x= 30;
int birdSize = 150;
void setup() {
  size(700, 500);
}

void draw() {
  background(255);
  wingFly = mouseY;
  if (frameCount%x > x/2) {
    wingFly += birdSize/3;
  } else {
    wingFly -= birdSize/3;
  }
  flybird(mouseX, mouseY, birdSize);
}

void keyPressed() {
  if (key == 'z' || key == 'Z') {
    birdSize -= 10;
    if (birdSize <= 0) {
      birdSize = 0;
    }
  }
  if (key == 'x' || key == 'X') {
    birdSize += 10;
    if (birdSize >= height) {
      birdSize = height;
    }
  }
}
void mouseClicked() {
  if (mouseButton == LEFT) {
    x -= 8;
    if (x <= 0) {
      x =4;
    }
  } else if (mouseButton == RIGHT) {
    x += 8;
    if (x >= 60) {
      x = 60;
    }
  }
}

void flybird(int x, int y, int birdSize) {
  strokeWeight(3);
  fill(#FF00FF);
  ellipse(x, y, birdSize, birdSize);
  fill(0);
  ellipse(x-(birdSize/5), y, birdSize/4, birdSize/3);
  ellipse(x+(birdSize/5), y, birdSize/4, birdSize/3);
  fill(255);
  ellipse(x-(birdSize/5), y, birdSize/6, birdSize/5);
  ellipse(x+(birdSize/5), y, birdSize/6, birdSize/5);
  fill(#FFFF00);
  triangle(x-(birdSize/4), y+(birdSize/4), x+(birdSize/4), y+(birdSize/4), x, y+birdSize/1.5);

  line(x-(birdSize/2), y, x-birdSize, wingFly);
  line(x+(birdSize/2), y, x+birdSize, wingFly);
}

LAb3-balloon



void setup(){
  size(300,500);
}
void draw(){
  background(255);
  draw_balloon(mouseX,mouseY,100,100);
}

void draw_balloon(int x,int y,int balloonSize,int string){
  fill(255);
   if(x < 20 || x > 280 || y < 20 || y > 480){
    fill(#DD0000);
  }else{
    fill(255);
  }
  ellipse(x,y,balloonSize,balloonSize);
  line(x,y+balloonSize/2,x,(y+balloonSize/2)+string);
}

วันอังคารที่ 1 กันยายน พ.ศ. 2558

Lab2-Example Error

ปัญหา Error : Missing right parenthesis ")"
    สาเหตุ : เกิดจากการที่เราลืมใส่วงเล็บไม่ครบ มีวงเล็บเปิดไม่มีวงเล็บปิด เป็นต้น
    วิธีแก้ : ใส่วงเล็บให้ครบ


ปัญหา Error : Missing a semicolon";"
    สาเหตุ : ลืมใส่เครื่องหมาย semicolon(;)
    วิธีแก้ : ใส่ semicolon ลงไปและใส่ให้ถูกที่


ปัญหา Error : Missing right curly bracket "}"
    สาเหตุ : เกิดจากการที่เราลืมใส่วงเล็บปีกกาไม่ครบ มีวงเล็บปีกกาเปิดไม่มีวงเล็บปีกกาปิด เป็นต้น
    วิธีแก้ : ใส่วงเล็บปีกกาให้ครบ


ปัญหา Error : The variable "(ชื่อตัวแปรที่ error)" does not exist
    สาเหตุ : ตัวแปรนี้ยังไม่ได้ประกาศ หรือ เกิดจากการที่พิมชื่อตัวแปรผิด
    วิธีแก้ : ประกาศตัวแปร หรือ แก้ไขชื่อตัวแปรให้ถูกต้อง


ปัญหา Error : Void methods cannot return a value
    สาเหตุ : ฟังก์ชั่น void จะไม่สามารถส่งค่ากลับได้ จึงเกิดการ error
    วิธีแก้ : ลบ "return (ชื่อตัวแปร);" ออก

Lab2-Digital Clock



void setup(){
    size(500,200);
}

void draw(){
    background(#DD0000);
    digitalClock(width/2,height/2,50);
}

void digitalClock(int posX,int posY,int textsize){
    int sec = second();
    int min = minute();
    int hr = hour();
    int space = 150;
    fill(255);
    textSize(textsize);
    textAlign(CENTER,CENTER);
    text(hr+"hr",posX-space,posY);
    text(min+"min  ",posX,posY);
    text(sec+"sec",posX+space,posY);
}

Lab2-Positive(Function)

int resizeW;
int resizeH;
int weight = 100;
int high = 300;

void setup() {
  size(500, 500);
}

Lab2-SPYAIR(Function)

float spin = 0;
float speed = 0;
int textposX = 360;
int R = 255, G = 255, B = 255;

void setup() {
    size(700, 400);
}