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

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);
}