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

14 กันยายน 2558

Lab4 Flock birds

float wing_y;
float bird_y;
int size_draw = 40;
boolean wing_up;
boolean wing_down;
int max_bird = 3;

void setup() {
  size(600, 600);
  background(255);
  mouseX=width/2;
  mouseY=height/2;
  wing_up = true;
}

void draw() {
  int present_bird = 1;
  int X = 0;
  int Y = 0;
  background(255);

  if (mouseY<size_draw*2) {
    mouseY=size_draw*2;
  }
  if (mouseX<size_draw) {
    mouseX = size_draw;
  }

  while (present_bird <= max_bird) {
    draw_bird(mouseX+X, mouseY+Y, size_draw, bird_y);
    X += size_draw*3;
    if (present_bird%4==0) {
      X=0;
      Y-=size_draw*1.1;
    }
    present_bird++;
  }

  if (wing_up == true) {
    wing_y-= size_draw*0.15;
    bird_y-= size_draw*0.01;
    if (wing_y <= -(size_draw*0.7)) {
      wing_up = false;
      wing_down = true;
    }
  }

  if (wing_down == true) {
    wing_y+= size_draw*0.15;
    bird_y+= size_draw*0.01;
    if (wing_y >= (size_draw*0.7)) {
      wing_up = true;
      wing_down = false;
    }
  }
}

void draw_bird(float mouse_x, float mouse_y, float s_draw, float bird_y) {
  fill(#D30327);
  strokeWeight(s_draw*0.2);
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x-(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));//left wing
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x+(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));//right wing
  strokeWeight(s_draw*0.05);
  ellipse(mouse_x, mouse_y-(s_draw*0.5)+bird_y, s_draw, s_draw);//body
  strokeWeight(1);
  fill(255);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.3, s_draw*0.35);//left white eye
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.3, s_draw*0.35);//right white eye
  fill(0);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.1, s_draw*0.1);//left black eye
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.1, s_draw*0.1);//right black eye
  fill(#F9BC09);
  triangle(mouse_x-(s_draw*0.17), mouse_y-(s_draw*0.35)+bird_y, mouse_x+(s_draw*0.17), mouse_y-(s_draw*0.35)+bird_y, mouse_x, mouse_y-(s_draw*0.05)+bird_y); //mounth
}

void keyPressed() {
  if (keyCode==UP) {
    size_draw++;
    if (size_draw>=90)size_draw=90;
  }
  if (keyCode==DOWN) {
    size_draw--;
    if (size_draw<=20)size_draw=20;
  }
}
void mousePressed() {
  if (mouseButton == LEFT) {
    max_bird++;
  } else if (mouseButton == RIGHT) {
    max_bird--;
    {
      if (max_bird<=1)max_bird=1;
    }
  }
}

Lab4 Balloon

int max_balloon = 4;
int size_draw = 50;
float Y = height;
void setup() {
  size(600, 600);
}


void draw() {
  int present_balloon = 1;
  float X = 0;
  background(0);
  if (Y < height*0.16) {
    fill(#00FD00);
  } else if (Y < height*0.33) {
    fill(#A0FA00);
  } else if (Y < height*0.5) {
    fill(#F2FB01);
  } else if (Y < height*0.66) {
    fill(#EFA200);
  } else if (Y < height*0.83) {
    fill(#EB6100);
  } else {
    fill(#D00022);
  }

  while (present_balloon <= max_balloon) {
    draw_balloon(X, Y);
    X += size_draw*1.2;
    present_balloon++;
  }

  Y-=2;
  if (Y<=(-size_draw*2))Y=height;
}

void draw_balloon(float pos_x, float pos_y) {
  stroke(#009CDA);
  strokeWeight(size_draw*0.05);
  float radius = size_draw;
  float string_length = size_draw*2;
  line(pos_x+(size_draw*0.6), pos_y, pos_x+(size_draw*0.6), pos_y+string_length);
  ellipse(pos_x+(size_draw*0.6), pos_y, radius, radius);
}

void keyPressed() {
  if (keyCode==UP) {
    size_draw++;
    if (size_draw>=90)size_draw=90;
  }
  if (keyCode==DOWN) {
    size_draw--;
    if (size_draw<=20)size_draw=20;
  }
}
void mousePressed() {
  if (mouseButton == LEFT) {
    max_balloon++;
  } else if (mouseButton == RIGHT) {
    max_balloon--;
    {
      if (max_balloon<=1)max_balloon=1;
    }
  }
}

Lab4 Loan Payment

void setup() {
  size(685, 500);
  background(0);
  fill(255);
  monthly_loan_payment(5000, 12, 1);
}
void monthly_loan_payment(float loan_amount, float interest_rate, int loan_term) {
  float ratepermonth = (interest_rate/100)/12;
  float paypermonth = loan_amount*(ratepermonth/(1-pow(1+ratepermonth, -(loan_term*12))));//M=P*(J/(1-(1+J)^-n))
  float unpaid = loan_amount;
  float total_interest = 0;
  int month = 1;
  textSize(25);
  text("Monthly Loan Payment", 205, 30);
  textSize(17);
  text("Payment No.        Interest        Principal        Unpaid Balance        Total Interest", 20, 70);
  int Y =110;
  while (month <= (loan_term*12)) {
    float interest = ratepermonth*unpaid;
    total_interest+=interest;
    float principal = paypermonth-interest;
    unpaid = abs(unpaid-principal);
    text(nf(month, 2), 60, Y);
    text("$"+nf(interest, 1, 2), 168, Y);
    text("$"+nf(principal, 1, 2), 272, Y);
    text("$"+nf(unpaid, 1, 2), 408, Y);
    text("$"+nf(total_interest, 3, 2), 576, Y);
    Y+=30;
    month++;
  }
}

Lab4 Sum of Prime number

void setup() {
  size(350, 100);
  background(0);
  int max_val = 10;
  int p_num =2;
  int sum=0;
  while (p_num <= max_val) {
    if (prime_number(p_num)) {
      sum += p_num;
      println(sum);
    }
    p_num++;
  }
  textSize(18);
  textAlign(CENTER);
  text("Sum of Prime number from 1 to "+max_val, width/2, height/2-20);
  text("'"+sum+"'", width/2, height/2+20);
}

boolean prime_number(int p_num) {
  boolean result = true;
  int divisor = 2;
  while (divisor<p_num) {
    if (p_num%divisor==0) {
      result = false;
      break;
    }
    result = true;
    divisor++;
  }
  return result;
}

Lab4 Multiplication Table

void setup() {
  size(250, 300);
  background(0);
  int multi_table = 25;
  int max_multiplier_value = 12;
  int x=1;
  textSize(18);
  text("Multiplication Table "+"'"+multi_table+"'", 10, 20);
  while (x <= max_multiplier_value) {
    int result = multi_table*x;
    text(multi_table+" x "+x+" = "+result, 10, 30+(20*x));
    x++;
  }
}

Lab4 Sum of Integers

void setup() {
  size(300, 100);
  background(0);
  int max_val = 99;
  int x=0;
  int sum=0;
  while (x <= max_val) {
    sum += x;
    x++;
  }
  textSize(18);
  textAlign(CENTER);
  text("Sum of Integers from 1 to "+max_val, width/2, height/2-20);
  text("'"+sum+"'", width/2, height/2+20);
}