แสดงบทความที่มีป้ายกำกับ Lab 4 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Lab 4 แสดงบทความทั้งหมด

กันยายน 15, 2558

Lab4_Generic exercise

//*****sum-Of-1-to-N*****
void setup(){
  int N = 15;
  sumOfOneToN(N);
}
void sumOfOneToN(int n){
  int sum = 0;
  println("N = "+n);
  while(n>0){
    sum += n;
    n--;
  }
  println("sum of int from 1 to N = "+sum);
}

//*****Multiplication-Table*****
void setup(){
  int num = 13;
  multipli_table(num);
}
void multipli_table(int n){
  int multi = 1;
  println("Multiplication-Table of "+n);
  while(multi <= 12){
    println(n+" x "+multi+" = "+(n*multi));
    multi++;
    }
}

//*****Prime-Number*****
void setup(){
  int final_num = 100;
  primeCal(final_num);
}
void primeCal(int n){
  int num = 1;
  println("Prime number from 1 to "+n);
  while(num<=n){
    boolean primeNum = true;
    int i = 2;
    while(i<num && primeNum){
      if((num%i) == 0)
        primeNum = false;
      i++;
    }
    if(primeNum)
      print(num+" ");
    num++;
  }
}
//*****Monthly-loan-payment*****
void setup() {
  float loan = 5000;
  float interest_rate = 12; //percent
  int loan_term = 12; //months
  loanCal(loan, interest_rate, loan_term);
}

void loanCal(float loan, float interest_rate, float loan_term) {
  float payMonthly, balance = loan, total_interest=0;
  float interest, principal, j;
  int count=1;
  j = interest_rate/100/12;
  payMonthly = loan*(j/(1-(pow(1+j,-loan_term))));
  println("***** Monthly Loan Payment *****");
  println(" Loan Amount : $"+loan);
  println(" Loan Term   : "+loan_term+" months");
  println(" Interest Rate : "+interest_rate+"%");
  println("************************************");
  
  println("Payment NO.|   Balance   |   Interest   | Principal | Unpaid Balance | Total Interest to Date");
  while (count<=loan_term) {
    interest = j*balance;
    principal = payMonthly-interest;
    print("     "+count+"       "+balance+"      "+interest+"         "+principal);
    balance -= principal;
    total_interest += interest;
    println("       "+balance+"          "+total_interest);
    count++;
  }
  println("Payment Every Month : $"+payMonthly);
  println("Total of 12 Payments : $"+(loan+total_interest));
  println("Total Interest : $"+total_interest);
}

Lab4_songs

int limit=0;
int light_brown=#F7DEA7;
int white=255;
color screen;
color Line=255;
boolean dropNote=false;
int noteY=0;
void draw() {
  int noteX=10;
  size(350, 185);
  background(screen);
  limit++;
  limit %= 180;
  draw_song();
  if (dropNote) {
    while (noteX<width) {
      note(noteX);
      noteX += 25;
    }
      if (noteY>200) {
        dropNote=false;
        noteY=0;
   
    }
  }
}
void draw_song() {
  int posX=100;
  posX+=limit;
  noStroke();
  //draw"THE"
  fill(Line);
  rect(posX-25, 10, 70, 30); //blank"THE"
  fill(screen);
  rect(posX-25, 15, 7, 25);
  rect(posX-13, 15, 7, 25); //T
  rect(posX, 10, 15, 12);
  rect(posX, 27, 15, 13); //H
  rect(posX+20, 10, 3, 30);
  rect(posX+28, 15, 17, 7);
  rect(posX+28, 27, 17, 8); //E
  triangle(posX+33, 10, posX+45, 45, posX+45, 10);
  //draw"SCRIPT"
  fill(Line);
  arc(45, 75, 60, 60, HALF_PI, TWO_PI);
  arc(45, 131, 70, 70, HALF_PI+PI, TWO_PI+HALF_PI); //S
  arc(130, 105, 110, 110, QUARTER_PI+PI/32, TWO_PI-QUARTER_PI-PI/32); //C
  rect(165, 50, 20, 10);
  rect(165, 105, 20, 10);
  arc(185, 82.5, 60, 65, 3*PI/2, 5*PI/2);
  quad(170, 115, 185, 115, 215, 147, 215, 163); //R
  rect(220, 50, 10, 120); //I
  rect(235, 50, 10, 120);
  rect(245, 50, 20, 10);
  rect(245, 105, 20, 10);
  arc(265, 82.5, 60, 65, 3*PI/2, 5*PI/2); //P
  quad(265, 50, 330, 50, 335, 60, 265, 60);
  rect(300, 50, 10, 120); //T
  fill(screen);
  arc(45, 76, 40, 40, HALF_PI, TWO_PI);
  arc(45, 131, 50, 50, HALF_PI+PI, TWO_PI+HALF_PI); //S
  arc(130, 105, 90, 90, QUARTER_PI, TWO_PI-QUARTER_PI); //C
  triangle(147, 50, 155, 80, 155, 50);
  arc(185, 82.5, 40, 45, 3*PI/2, 5*PI/2); //R
  quad(220, 65, 230, 60, 230, 65, 220, 70); //I
  arc(265, 82.5, 40, 45, 3*PI/2, 5*PI/2); //P
  triangle(220, 170, 310, 130, 310, 170);
  fill(Line);
  rect(155, 50, 10, 97); //R
}
void note(int noteX) {
  fill(Line);
  ellipse(noteX, noteY, 15, 15);
  stroke(Line);
  strokeWeight(3);
  line(noteX+6, noteY, noteX+6, noteY-35);
  line(noteX+6, noteY-30, noteX+13, noteY-10);
  line(noteX+13, noteY-10, noteX+12, noteY-5);
  noteY++;
}
void mouseClicked() {
  if (mouseButton==RIGHT) {
    white *= -1;
    Line += white;
    screen += light_brown;
    light_brown*=-1;
  } else if (mouseButton==LEFT) {
    dropNote=true;
  }
}
 

Lab4_books

int wink;
float spin;
void draw() {
  size(300, 400);
  background(0);  //colorBlack
  draw_book();
  if (keyPressed) {
    if (key==CODED) {
      if (keyCode==LEFT) spin+=PI/32;
      else if (keyCode==RIGHT) spin-=PI/32;
    }
  }
  fill(255);
  text("Pressed LEFT or RIGHT arrow.", 75, 350);
  fill(0);
}
void draw_book() {
  int posX=155, posY=150;
  float addPos=0,count=0;
  if (frameCount%50<=25) wink=1;
  else wink=0;
  //bones
  while (count<2) {
    translate(155, 150);
    rotate(spin+addPos);
    fill(255);  //colorWhite
    noStroke();
    beginShape();
    vertex(-90, -110);
    vertex(110, 90);
    vertex(110, 110);
    vertex(90, 110);
    vertex(-110, -90);
    vertex(-110, -110);
    endShape();
    beginShape();
    vertex(90, -110);
    vertex(-110, 90);
    vertex(-110, 110);
    vertex(-90, 110);
    vertex(110, -90);
    vertex(110, -110);
    endShape();
    ellipse(-100, -120, 30, 30);
    ellipse(-120, -100, 30, 30);
    ellipse(100, -120, 30, 30);
    ellipse(120, -100, 30, 30);
    ellipse(120, 100, 30, 30);
    ellipse(100, 120, 30, 30);
    ellipse(-100, 120, 30, 30);
    ellipse(-120, 100, 30, 30);
    rotate(-spin-addPos);
    translate(-155, -150);
    addPos=PI/4;
    count++;
  }
  //skull & hat
  stroke(0);
  strokeWeight(4);
  ellipse(posX, posY+90, 80, 90);
  fill(145, 55, 25);  //colorBrown
  arc(posX, posY, 170, 170, PI+PI/16, TWO_PI-PI/16);
  fill(255, 210, 50); //colorYellow
  arc(posX, posY, 170, 170, PI+PI/7, TWO_PI-PI/7);
  arc(posX, posY+30, 320, 115, PI+PI/5, TWO_PI-PI/5);
  fill(255); //colorWhite
  arc(posX, posY, 170, 170, 0, PI);
  arc(posX, posY, 170, 30, PI, TWO_PI);
  //detail
  fill(0);
  ellipse(posX, posY+60, 20, 20); //nose
  ellipse(posX+35, posY+25, 50, 50*wink); //R-eye
  ellipse(posX-35, posY+25, 50, 50*wink); //L-eye
  noFill();
  arc(posX, posY, 195, 195, HALF_PI-PI/8, HALF_PI+PI/8);
  arc(posX, posY, 220, 220, HALF_PI-PI/8+PI/48, HALF_PI+PI/8-PI/48);
  line(posX-20, posY+85, posX-23, posY+105);
  line(posX, posY+85, posX, posY+110);
  line(posX+20, posY+85, posX+23, posY+105);
  //book
  strokeWeight(3);
  stroke(100); //lncolorGray
  arc(5, 400, 16, 50, HALF_PI, 3*PI/2);
  line(8, 0, 8, 375);
  line(8, 375, 300, 375);
  line(7, 380, 300, 380);
  line(6, 385, 300, 385);
  line(5, 390, 300, 390);
  line(4, 395, 300, 395);
  wink++;
}
 

Lab4_movies

int posX=100,posY=100;
int speed=1,assist=1;
boolean hold=false;
void draw(){
  size(300,250);
  background(#FF7700); //red
  draw_movie();
  if(hold) saber();
}
void draw_movie(){
  strokeWeight(3);
  stroke(100); //lnColorGray
  fill(0); //colorBlack
  quad(posX-95,posY+115,posX+145,posY+115,posX+109,posY+19,posX-59,posY+19); //wing
  arc(posX+25,posY-90,550,455,HALF_PI-PI/7,HALF_PI+PI/7); //wing
  quad(posX-53,posY+15,posX+103,posY+15,posX+75,posY+125,posX-25,posY+125); //face
  arc(posX+25,posY-10,160,160,PI-PI/8,TWO_PI+PI/8); //head
  quad(posX+37,posY+21,posX+90,posY+21,posX+145,posY+115,posX+112,posY+15);
  quad(posX+13,posY+21,posX-40,posY+21,posX-95,posY+115,posX-63,posY+15);
  arc(posX+75,posY+20,75,60,PI,TWO_PI); //R-eyebrow
  arc(posX-25,posY+20,75,60,PI,TWO_PI);  //L-eyebrow
  fill(255); //colorWhite
  ellipse(posX+55,posY+45,70,35); //R-eye
  ellipse(posX-5,posY+45,70,35); //L-eye
  fill(0);
  triangle(posX-25,posY+125,posX+75,posY+125,posX+25,posY+65); //mouth
  stroke(255); //lnColorWhite
  line(posX-9,posY+108,posX-9,posY+125);
  line(posX+8,posY+88,posX+8,posY+125);
  line(posX+25,posY+80,posX+25,posY+125);
  line(posX+42,posY+88,posX+42,posY+125);
  line(posX+59,posY+108,posX+59,posY+125);
  stroke(100);
  fill(255);
  rect(posX+13,posY+65,25,15); //nose
  fill(0);
  rect(posX+15,posY-92,20,145);
  quad(posX+15,posY+53,posX+35,posY+53,posX+38,posY+65,posX+13,posY+65);
  arc(posX+25,posY+125,100,40,0,PI); //chin
  fill(255);
  ellipse(posX+70,posY+125,10,10);
  ellipse(posX-20,posY+125,10,10);
  posX+=speed;
  posX %= 250;
}
void saber(){
  int count=0,addX=0;
  while(count<2){
  strokeWeight(14);
  stroke(#FF0000);
  line(posX-95+addX,225,posX-95+addX,20);
  strokeWeight(8);
  stroke(255);
  line(posX-95+addX,220,posX-95+addX,23);
  strokeWeight(3);
  stroke(100);
  fill(200);
  rect(posX-100+addX,225,10,25);
  addX=235;
  count++;
  }
}
void mouseClicked(){
  if(mouseButton==RIGHT){
    assist*=(-1);
    speed+=assist;
  }
  if(mouseButton==LEFT){
    if(hold==true) hold=false;
    else hold=true;
  }
}
 

กันยายน 13, 2558

Lab4_birds

float cloudX=0;
float move=1;
void draw(){
  int count= -2;
  int posX, posY;
  size(500,500);
  background(#00AAFF);
  cloud();
  while(count<3){
    posX=mouseX+(count*60);
    posY=mouseY+(count*60);
    translate(posX,posY);
    strokeWeight(1.5);
    bird();
    translate(-posX,-posY);
    count++;
  }
}
void bird(){
  rotate(PI/5);
  fill(#FF0000);
  ellipse(0,-25,10,20);  //hair
  ellipse(-10,-25,10,20);
  rotate(-PI/5);
  wing();
  ellipse(0,0,50,50);    //body
  fill(#FFFF00);
  arc(0,0,50,50,PI/4,3*PI/4);
  fill(255);
  ellipse(-10,-5,15,10); //left eye
  fill(0);
  ellipse(-10,-5,5,5);
  fill(255);
  ellipse(10,-5,15,10);  //right eye
  fill(0);
  ellipse(10,-5,5,5);
  triangle(0,0,-10,10,10,10); //beak
}
void wing(){
  float roll;
  if((frameCount%50)<=25)
    roll=PI/16;
  else
    roll=-PI/16;
  rotate(roll);
  ellipse(-30,0,50,20);  //left wing
  rotate(-2*roll);
  ellipse(30,0,50,20);   //right wing
  rotate(roll);
}
void cloud(){
  noStroke();
  fill(255);
  ellipse(510-cloudX,100,100,30);
  ellipse(30+cloudX,175,120,60);
  ellipse(450-cloudX,300,75,50);
  ellipse(100+cloudX,275,85,45);
  ellipse(350-cloudX,405,215,140);
  stroke(0);
  cloudX+=move;
  if (cloudX>=450)
    move*=-1;
  else if (cloudX<=-50)
    move*=-1;
}
 

Lab4_balloons

void draw(){
  int count=-1;
  size(200,400);
  background(255);
  while(count<2){
    int posX=width/2;
    posX+=(55*count);
    draw_balloon(posX);
    count++;
  }
}
void draw_balloon(int x){
  int posY=mouseY;
  color ball;
  if(mouseY<=50){
    posY=50;
    ball=#00FF00;
  }
  else if(mouseY>=300){
    posY=300;
    ball=#FF0000;
  }
  else ball=255;
  strokeWeight(1.5);
  fill(ball);
  line(x,posY,x,posY+100);
  ellipse(x,posY,50,50);
}