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

bug report

BUG REPORT

what?
1.ใช้ switch ในการเปลี่ยนเฟรม
โดย กำหนด if(mousePressed){num=1;}
เมื่อ frame แรกมีค่า num = 0;
แล้ว เมื่อรันโปรมแกรมแล้วคลิ๊ก
frame ต่อไปที่แสดงไม่เป็นไปตามเงื่อนไขที่กำหนด

2.เมื่อนำ frame ทุก frame มารวมกันโดยใช้คำสั่ง switch
ภาพบางจุดของ frame 2 และ 3 ไม่ขยับตามคำสั่ง

why?
เนื่องจาก ฟังก์ชัน mousePressed เป็นฟังก์ชัน
ที่จะต้องมีการกด mouse ก่อน(กำหนดให้ mousePressed = true)
แต่เมื่อเรากดความเร็วในการรับค่าจากการออกแรงกดเพียง1ที
อาจมากกว่าที่เรากด mouse ทำให้ลำดับขั้นตอนของแต่ละเฟรมที่แสดงนั้น
เปลี่ยนไปอย่างรวดเร็ว จนบางครั้งดูไม่ทันว่าเฟรมนั้นๆถูกแสดงไม่หรือยัง

hot to fix?
เปลี่ยนจากการใช้เงื่อนไข
if(mousePressed){num=1;}
if(mousePressed){num=2;}
if(mousePressed){num=3;}
if(mousePressed){num=4;}
if(mousePressed){num=5;}

เป็น

void mousePressed() {
  switch(num) {
  case 0:
    num = 1;
    break;
  case 1:
    num = 2;
    break;
  case 2:
    num = 3;
    break;
  case 3:
    num = 4;
    break;
  case 4:
    num = 5;
    break;
  case 5:
    num = 0;
    break;
  }
}

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

Lab2 : income tax [return]

int income;
void setup(){
  size(200,100);
  background(255);
  fill(0);
  text("tax = "+function(500000),50,50);
  println(function(500000));
}
int function (int x){
  int income = 0;
 if (x >= 800000){ //income is greater than or equal to 800000
  income = x*25/100;
 }else if((x < 800000)&&( x >= 500000)){ //income is less than 800000 and greater than or equal to 500000
 income = x*20/100;
 }else if((x  < 500000)&&(x  >= 300000)){ //income is less than 500000 and greater than or equal to 300000
 income = x*15/100;
 }else if((x < 300000)&&(x  >= 150000)){ //income is less than 300000 and greater than or equal to 150000
income = x*10/100;
 }else{ //income is less than 150000
income =  0;
 }
  return income;
}

Lab3 : balloon


int X[] = {75,150,225};
int Y[] = {300,250,200};


void setup(){
  size(400,300);
  background(255);
  frameRate(100);
}

int i = 0;
int a = 70;
int s = 90;
void draw(){
  background(255);

  for(int i=0;i<Y.length;i++){
  balloon(X[i],Y[i],a,s);

  Y[i]=Y[i]-1;
  if  (mousePressed){Y[i] = height -(50*i) ;
 }
  }

}
void balloon(int x,int y,int a,int s){
  line(x,y+20,x,y+120);
  fill(255,0,0);
  ellipse(x,y,a,s);
  triangle(x,y+45,x+5,y+50,x-5,y+50);

}

Lab3 : turtle

 
 void setup() {

  size(400, 400);
  background(255);
  int X[] = {160,300,100};
  int Y[] = {280,100,120};
  
  for(int i=0;i<Y.length;i++){
  drawTurtle(X[i], Y[i], 40, 20, 3);
  }

}
void drawTurtle(int X, int Y, int Width, int Height,int Radius) {
  fill(255, 255, 153);
  ellipse(X, Y, Width, Height); //leg

  fill(255, 255, 153);
  rect(X+90, Y-30, Width-20, Height-10, Radius); //tail

  fill(0, 153, 0);
  ellipse(X+30, Y-30, Width+100, Height+60); //body

  fill(153, 153, 0);
  rect(X+5, Y-60, Width-20, Height, Radius+7); //shell
  rect(X+30,Y-60,Width-20,Height,Radius+7); //shell
  rect(X+55,Y-60,Width-20,Height,Radius+7); //shell

  fill(255, 255, 153);
  noStroke();
  ellipse(X-40, Y-50, Width+30, Height+60); //face
  ellipse(X-50,Y-30,Width+20,Height+20); //face

  stroke(2);
  line(X-70, Y-30, X-60, Y-30); //mouse

  fill(0);
  ellipse(X-60, Y-60, Width-25, Height-5); //eye
  fill(255);
  ellipse(X-65, Y-63, Width-35, Height-15); //eye

  fill(255, 255, 153);
  ellipse(X+20, Y, Width, Height); //leg
  ellipse(X+70,Y,Width,Height); //leg
}

Lab3 : pencil


int XR[] = {100};

int YR[] = {125};

int XT[] = {100,122,145};

int YT[] = {210,250,210};

void setup(){

  size(400,300);

  background(255);

int i = 0;

int w = 15;

int h = 90;

  drawRectPencil(XR[i],YR[i],w,h);

  drawTrianglePencil(XT[0],YT[0],XT[1],YT[1],XT[2],YT[2]); //wood

}

void drawRectPencil(int x,int y,int w,int h){

  int r = 5;

fill(255,255,0); //yellow

rect(x,y,w,h,r); //left

fill(0,255,0); //green

rect(x+15,y,w,h,r); //middle

fill(255,255,0); //yellow1

rect(x+30,y,w,h,r); //right

fill(204,204,0);//yellow2

rect(x,y-20,w+30,h-65,r-5);//eraser

fill(255,255,255);//white

rect(x,y-20,w+30,h-80,r-2);//eraser

fill(0,0,0);

rect(x+20,y+121,w-10,h-85,r-5);//lead

}

void drawTrianglePencil(int x1,int y1,int x2,int y2,int x3,int y3){

fill(255,255,255);

triangle(x1,y1,x2,y2,x3,y3);

}

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