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

Lab3 : histogram


int[] x = {0, 50, 100, 150, 200};
int[] y = {175, 100, 200, 200, 150};
void setup() {
  size(400, 300);
}
void draw() {
  background(0);
  stroke(0);
  histogram();
  fill(100, 74, 250);
  textSize(20);
  text("MAX = "+ max(y), 290, 20);
  fill(20, 255, 92);
  textSize(20);
  text("MIN = "+ min(y), 290, 40);
  fill(255);
  textSize(20);
  text("AVG = "+ avg(), 290, 60);
  move();
  stroke(255,0,0);
  line(0,height-avg(),300,height-avg());
}
void histogram() {
  for (int i = 0; i<y.length; i++) {
  
    if (y[i] == max(y)) {
      fill(100, 74, 250);
    } else if (y[i] == min(y)) {
      fill(20, 255, 92);
    } else {
      fill(255);
    }
rect(x[i], 300, 50, -y[i]);}
  
}
void move() {
  for (int i = 0; i<y.length; i++) {
    if (mousePressed&&mouseX>i*50&&mouseX<(i+1)*50) {
      y[i]=height-mouseY;
    }
  }
}

int avg(){
  int x = 0;
  for(int i = 0;i<y.length;i++) {
  x = x+(y[i]/5);
  }
return x;
}

วันจันทร์ที่ 8 กันยายน พ.ศ. 2557

Quadrant


 void setup(){
   size(300, 300);
  background(255);
  frameRate(30);
 }
  void draw(){
  strokeWeight(6);
  line(0,150,300,150);
  line(150,0,150,300);
  Quadrant();
}
void Quadrant(){
 if((mouseX>150)&&(mouseY<150)){
    println("Quadrant1");
}else if((mouseX<150)&&(mouseY<150)){
    println("Quadrant2");
}else if((mouseX<150)&&(mouseY>150)){
    println("Quadrant3");
}else {
    println("Quadrant4");
}
}

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

income tax


void setup(){
 size(200,100);
 background(255);

 float income = 500000 ;
 if (income >= 800000){ //income is greater than or equal to 800000
 println("pay for tax "+income*25/100);
 }else if((income < 800000)&&(income >= 500000)){ //income is less than 800000 and greater than or equal to 500000
 println("pay for tax "+income*20/100);
 }else if((income < 500000)&&(income >= 300000)){ //income is less than 500000 and greater than or equal to 300000
 println("pay for tax "+income*15/100);
 }else if((income < 300000)&&(income >= 150000)){ //income is less than 300000 and greater than or equal to 150000
 println("pay for tax "+income*10/100);
 }else if (income < 150000){ //income is less than 150000
 println("pay for tax "+"0");
 }
 }

calculate grade


void setup(){
 size(200,100);
 background(0);

int score = 79 ;
 char grade ;
 if (score >= 80){ //score is greater than or equal to 80
 grade = 'A' ; //show "GRADE A"
 }else if((score < 80)&&(score >= 70)){ //score is less than 80 and greater than or equal to 70
 grade = 'B' ; //show "GRADE B"
 }else if((score < 70)&&(score >= 60)){ //score is less than 70 and greater than or equal to 60
 grade = 'C' ; //show "GRADE C"
 }else if((score < 60)&&(score >= 50)){ //score is less than 60 and greater than or equal to 50
 grade = 'D' ; //show "GRADE D"
 }else{ //score is less than 50
 grade = 'F' ; //show "GRADE F"
 }
 println("GRADE "+grade); fill(255,0,10); text("GRADE "+grade,70,50);
}

Lab2 : an example showing that draw()


 void setup() {
 size(300, 250);
 background(255);
 strokeWeight(8);
 draw(155,110);
 draw(120,90);
 draw(100,70);
 draw(50,50);
 draw(155,15);
 draw(10,90);
 draw(10,10);
}
void draw(int width,int height) {
 rect(50,50,width,height);
}

Lab2 : forest


 void setup(){
 size(400, 200);
 background(64,64,64);

 //friend's tree
draw_rect(130,100,50,150) ;
draw_circle(150,90,30); // circle
draw_circle(125,110,30); // circle
draw_circle(100,100,30); // circle
draw_circle(75,90,30); // circle
draw_circle(100,80,30); // circle
draw_circle(125,70,30); // circle
draw_circle(150,60,30); // circle
draw_circle(175,70,30); // circle
draw_circle(200,80,30); // circle
draw_circle(225,90,30); // circle
draw_circle(200,100,30); // circle
draw_circle(175,110,30); // circle
}
 void draw_rect (int x, int y , int small , int longg) {
 fill(#8B4513);
 noStroke();
 rect(x+80,y,small,longg);
}
void draw_circle (int x,int y , int radius){
fill(#00FF00);
noStroke();
ellipse(x+80,y,2*radius,2*radius);

 //my tree =="
fill(#8A5C00);
draw_tree(105, 80, 165, 80, 165, 163, 105, 163); //brown tree
fill(#19A319);
noStroke();
draw_leaves(100, 90, 40, 40); //green leaves1
draw_leaves(85, 65, 40, 40); //green leaves2
draw_leaves(105, 50, 40, 40); //green leaves3
draw_leaves(153, 45, 30, 40); //green leaves4
draw_leaves(185, 75, 40, 40); //green leaves5
draw_leaves(175, 60, 40, 40); //green leaves6
draw_leaves(175, 90, 30, 40); //green leaves7
draw_leaves(140, 63, 90, 65); //green leaves8
}
void draw_tree(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4){
quad(x1,y1+50, x2, y2+50, x3, y3+50, x4, y4+50);
}
void draw_leaves(int x,int y,int width,int height){
ellipse(x, y+50, width, height); }

วันพุธที่ 3 กันยายน พ.ศ. 2557

Lab2 : convert celsius to fahrenheit


void setup() {
size(300,100);
background(0);
float Celsius, Fahrenheit ;
Celsius = 100;
Fahrenheit = Value(Celsius);
println(Celsius+"Celsius = "+Fahrenheit+"Fahrenheit");
fill(255,0,10);
text(Celsius+"Celsius = "+Fahrenheit+"Fahrenheit",70,50);
}
float Value(float Celsius) {
float Fahrenheit;
Fahrenheit = (Celsius*9)/5 + 32;
return Fahrenheit;
}