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

lab 4X summation

def setup():
  number=20
  count=1
  summation=0
  while count <= number:
    summation = summation + count
    count = count + 1
  print(" The summation is",summation)
     
setup()

lab 4X calculatoion of BMI

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

lab 4X calculation of cricle

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

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

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

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

setup()

lab 4X Grade

def setup():
  score=78
  print("Your score was =",score)
  if score<101and score>=80:
    print("you got A")
  if score<80and score>=70:
    print("you got B")
  if score<70and score>=60:
    print("you got C")
  if score<60and score>=50:
    print("you got D")
  if score<50:
    print("you got F")
 
setup()                    

lab 4X multiply table

def setup():
    multiply = 24
    count = 1
    while (count<=12):
           print(multiply,"*",count,"=",multiply*count)
           count = count + 1
 
setup()

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

lab 4 loan payment

void setup() {
  size(500, 600);
  background(126, 36, 12);
  textAlign(LEFT);
  textSize(20);
  fill(0, 255, 0);
  text("Monthly Loan Payment", 170, 30);
  textSize(13);
  fill(0, 0, 255);

  float loanAmount = 5000;
  float loanTerm = 1;
  float interestRate = 12;

  loanPayment(loanAmount, loanTerm, interestRate);
}

void loanPayment(float loanAmount, float loanTerm, float rate) {
  loanTerm = loanTerm*12;
  float percen = ((rate/100)/loanTerm);
  float B = percen/(1-(pow(1+percen, -loanTerm)));
  float paymentAmount = loanAmount*B;
  float payment12month = paymentAmount*loanTerm;
  text("Loan Amount : $"+loanAmount, 10, 65);
  text("Loan Term : "+(int)(loanTerm)+" months", 10, 90);
  text("Interset Rate : "+nf(rate, 0, 2)+" %", 10, 115);
  text("Payment Every Month : $"+nf(paymentAmount, 0, 2), 275, 65);
  text("Total of 12 Payments : $"+nf(payment12month, 0, 2), 275, 90);
  text("Total Interset : $"+nf((payment12month-loanAmount), 0, 2), 275, 115);

  int count = 1;
  float interest = loanAmount*((rate/100)/loanTerm);
  float principal = paymentAmount - interest;
  float endBal = loanAmount - principal;
  float totalInterest = interest;
  float begin;
  int y = 25;

  text("Payment No.", 20, 155);
  text("Interest", 120, 155);
  text("Princical", 200, 155);
  text("Unpaid Balance", 280, 155);
  text("Total interest", 400, 155);

  while (count <= loanTerm) {
    text(nf(count, 2), 50, 155+y);
    text("$"+nf(interest, 0, 2), 120, 155+y);
    text("$"+nf(principal, 0, 2), 200, 155+y);
    text("$"+nf(endBal, 0, 2), 300, 155+y);
    text("$"+nf(totalInterest, 0, 2), 420, 155+y);


    begin = endBal;
    interest = begin*((rate/100)/loanTerm);
    principal = paymentAmount - interest;
    endBal = abs(begin - principal);
    totalInterest = totalInterest + interest;

    count = count +1;
    y = y + 30;
  }
}

lab 4 movies

int eye_size = 50;
int red = 255;
int green = 255;
int blue = 0;

void setup () {
  size(700, 500);
  background(0, 0, 0);
  strokeWeight(3);
}
void draw() {
  fill(red, green, blue);
  rect(40, 50, 300, 400);
  rect(360, 50, 300, 400);

  eye(70, 100, eye_size);
  eye(400, 100, eye_size);
  mouth(90, 300);
}
void mouseMoved() {
  red = red -5;
  green = green - 15;
  blue = blue  - 51;
  if (red==0) {
    red = 255;
  }
  if (green==0) {
    green = 255;
  }
  if (blue==0) {
    blue = 255;
  }
}
void eye(int eyeposX, int eyeposY, int eye_size) {
  int picture  =0;
  int new_picture = 0;
  int number = 2;
  while (picture<number) {
    fill(255, 255, 255);
    ellipse(eyeposX+50+new_picture, eyeposY+50, eye_size+70, eye_size+70);
    fill(red, green, blue);
    ellipse(eyeposX+50+new_picture, eyeposY+50, eye_size+20, eye_size+20);
    fill(0, 0, 0);
    ellipse(eyeposX+50+new_picture, eyeposY+50, eye_size/2, eye_size/2);


    new_picture = new_picture + 120;
    picture = picture +1;
  }
}
void mouth(int mouthposX, int mouthposY) {
  int picture  =0;
  int new_picture = 0;
  int number = 2;
  while (picture<number) {
    fill(210, 54, 27);
    ellipse(mouthposX+100+new_picture, mouthposY+30, 280, 60);
    line(mouthposX-30+new_picture, mouthposY+30, mouthposX+220+new_picture, mouthposY+30);
    new_picture = new_picture +320 ;
    picture = picture +1;
  }
}

lab 4 flock of birds

int space = 5;
void setup() {
  size(900, 500);
  strokeWeight(6);
}
void draw() {
  background(34, 88, 126);
  int posX = mouseX;
  int posY = mouseY;
  if (frameCount%30>15) {
    space = 120;
  } else {
    space = -55;
  }
  draw_birds( posX, posY);
}
void draw_birds(int posX, int posY) {
  int radius = 60;
  int string_length = 50;
  int birds = 0;
  int new_birds = 0;
  int new_birds2=0;
  int number = 6;
  while (birds<number) {
    fill(255, 0, 0);
    stroke(0, 0, 0);
    ellipse(posX+new_birds, posY+new_birds2, radius, radius);
    fill(255, 255, 255);
    stroke(0, 0, 0);
    line(posX+30+new_birds, posY+new_birds2, posX+70+new_birds, posY+new_birds2+string_length-space);
    line(posX-30+new_birds, posY+new_birds2, posX-70+new_birds, posY+new_birds2+string_length-space);
    noStroke();
    ellipse(posX+new_birds-13, posY+new_birds2-10, radius-45, radius-45);
    ellipse(posX+new_birds+13, posY+new_birds2-10, radius-45, radius-45);
    fill(0, 0, 0);
    ellipse(posX+new_birds-13, posY+new_birds2-7, radius-55, radius-55);
    ellipse(posX+new_birds+13, posY+new_birds2-7, radius-55, radius-55);
    fill(253, 249, 64);
    triangle(posX+new_birds-30, posY+new_birds2+40, posX+new_birds-10, posY+new_birds2+5, posX+new_birds+10, posY+new_birds2+5);
    new_birds = new_birds +150;
    new_birds2 = new_birds2+50;
    birds = birds +1;
  }
}

lab 4 multiply table

void setup() {
  int multiply = 24;
  int count = 1;
  while (count<=12) {
    println(multiply+"*"+count+"="+multiply*count);
    count = count + 1;
  }
}

lab 4 summation of prime number

void setup() {
  int number=30;
  int divide=2;
  int count=2;
  int sum=0;
  boolean primeNum = true;

  while (count<=number) {
    while (divide<count) {
      if (count%divide==0) {
        primeNum=false;
        break;
      } else primeNum=true;
      divide= divide+ 1;
    }
    if (primeNum) {
      print(count+" ");
      sum=sum+count;
    }
    divide=2;
    count= count +1;
  }
  println(" ");
  println("Sum = "+sum);
}


lab 4 summation

void setup() {
  int count = 1;
  int sum = 0;
  int number = 15;

  while (count <= number) {
    sum = sum + count;
    print(count);
    print(" + ");
    count = count+1;
  }
  print(" = "+sum);
}

lab 4 balloons

void setup() {
  size(600, 600);
}
void draw() {
  background(160, 231, 231);
  int X = mouseX;
  int Y = mouseY;

  if (Y<50) {
    Y = 50;
    fill(225, 0, 0);
  }
  if (Y>500) {
    Y = 500;
    fill(0, 225, 0);
  }
  draw_balloon(X, Y);
}
void draw_balloon(int X, int Y) {
  int radius = 50;
  int string_length  = 80;
  int number = 10;
  int balloon = 0;
  int new_balloon = 0;

  while (balloon<number) {
    line(X+new_balloon, Y, X+new_balloon, Y + string_length);
    ellipse(X+new_balloon, Y, radius, radius);
    new_balloon=new_balloon+60;
    balloon=balloon+1;
  }
}

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

Lab 3 leap_year

String leap_year;
void setup() {
  size(500, 500);
}
void draw() {
  int year = 1996;
  background(255,255,255);
  fill(255,0,0);
  textSize(50);
  textAlign(CENTER);
  text("Year is "+year, width/2, 200);
  text(year+" is "+leap_year, width/2, 250);
  if (year%4==0&&year%100!=0)
    leap_year="Leap Year";
  else if (year%4==0&&year%100==0)
    if (year%400==0)
      leap_year="Leap Year";
    else if (year%400!=0)
      leap_year="not Leap Year";
    else leap_year="not Leap Year";
}

Lab 3 battery

int space = 5;
int space2 = 5;
int posY=2;
void setup() {
  size(500, 500);
}
void draw() {
  background(0, 0, 0);
  fill(225, 225, 225);
  strokeWeight(5);
  stroke(255, 255, 255);
  rect(150, 80, 200, 380);
  rect(190, 20, 40, 60);
  rect(270, 20, 40, 60);
  fill(0, 255, 0);
  rect(152, 85, 195, 370);
  fill(0, 0, 0);
  rect(152, 85, 195, 4+space);
  space = (space + 1 )%width;
  if ( space>=368) {
    space = (space - 1)%width;
    fill(0, 225, 0);
    stroke(255, 255, 255);
    rect(152, 455-posY, 195, 2+space2);
    if (space2<370) {
      space2=(space2 +1)%width;
      posY=(posY+1)%width;
    }
  }
}


Lab 3 grade

void setup() {
  size(500, 500);
}
void draw() {
  background(0, 255, 0);
  int score=78;
  String grade;
  fill(0);
  textSize(50);
  text("Score="+score, 130, 100);
  if (score<101&&score>79) {
    grade="A";
  } else if (score<=79&&score>69) {
    grade="B";
  } else if (score<=69&&score>59) {
    grade="C";
  } else if (score<=59&&score>49) {
    grade="D";
  } else if (score<=49&&score>=0) {
    grade = "F";
  } else {
    grade = "Error";
  }
  fill(255, 0, 0);
  textSize(65);
  text("Grade="+grade, 120, 250);
}

Lab 3 music(move)

void setup () {
  size(500, 500);
}
void draw() {
  background(0, 255, 255);
  draw_music(mouseX, mouseY);
}
void draw_music(int X, int Y) {

  fill(44, 235, 16);
  stroke(255, 255, 255);
  rect(X, Y, 450, 450);//Green
  stroke(255, 255, 255);
  fill(255, 255, 255);
  ellipse(X+230, Y+200, 360, 300);//circle
  stroke(255, 255, 255);
  triangle(X+165, Y+405, X+315, Y+315, X+175, Y+225);//triangle
  fill(0, 0, 0);
  textSize(18);
  text("shu thag line - timethai Feat.kratae R siam  ", X+35, Y+425);
  fill(0, 255, 0);
  textSize(145);
  text("LINE  ", X+80, Y+250);
}

Lab 3 flying_bird

int wing;
void setup() {
  size(500, 500);
  strokeWeight(6);
}
void draw() {
  background(48, 231, 231);

  wing=mouseY;
  if (frameCount%60>30) {
    wing+=130;
  } else {
    wing-=130;
  }
  draw_flybird(mouseX, mouseY);
}
void draw_flybird(int x, int y) {
  fill(225, 0, 0);
  stroke(0, 0, 0);
  ellipse(x, y, 90, 90);
  line(x+45, y, x+130, wing);
  line(x-45, y, x-130, wing);
  line(x+50, y, x+135, wing);
  line(x-50, y, x-135, wing);
  line(x+55, y, x+140, wing);
  line(x-55, y, x-140, wing);
  line(x+60, y, x+145, wing);
  line(x-60, y, x-145, wing);
  fill(253, 249, 64);
  noStroke();
  triangle(x+10, y+10, x-10, y+30, x-30, y+10);
  noStroke();
  fill(255, 255, 255);
  ellipse(x-25, y-5, 25, 20);
  ellipse(x+25, y-5, 25, 20);
  fill(0, 0, 0);
  ellipse(x-25, y-5, 10, 10);
  ellipse(x+25, y-5, 10, 10);
}

Lab 3 movies(change color)

int eye_size = 100;
int red = 255;
int green = 255;
int blue = 0;
void setup () {
  size(500, 500);
  background(34, 23, 44);
  strokeWeight(3);
}
void draw() {
  fill(red, green, blue);
  rect(50, 50, 400, 400);
  eye(100, 150, eye_size);
  eye(300, 150, eye_size);
  mouth(150, 300);
}
void mouseMoved() {
  red = red -5;
  green = green - 15;
  blue = blue  - 51;
  if (red==0) {
    red = 255;
  }
  if (green==0) {
    green = 255;
  }
  if (blue==0) {
    blue = 255;
  }
}
void eye(int eyeposX, int eyeposY, int eye_size) {
  fill(255, 255, 255);
  ellipse(eyeposX+50, eyeposY+50, eye_size+70, eye_size+60);
  fill(red, green, blue);
  ellipse(eyeposX+50, eyeposY+50, eye_size, eye_size);
  fill(0, 0, 0);
  ellipse(eyeposX+50, eyeposY+50, eye_size/2, eye_size/2);

  line(eyeposX+90, eyeposY+20, eyeposX+130, eyeposY-40);
  line(eyeposX+50, eyeposY-60, eyeposX+50, eyeposY);
  line(eyeposX+10, eyeposY+20, eyeposX-30, eyeposY-40);
}
void mouth(int mouthposX, int mouthposY) {
  fill(210, 54, 27);
  ellipse(mouthposX+100, mouthposY+60, 350, 80);
  line(mouthposX-30, mouthposY+60, mouthposX+240, mouthposY+60);
}