สิงหาคม 31, 2558

Lab2_Generic exercise

/*Calculate body mass index (BMI)*/
void setup(){
  int weight= 60;  //weight(kg)
  int tall= 175;   //height(cm)
  float BMI=bodyMassCal(weight,tall);  //body mass index
  println("Weight = " + weight);
  println("Height = " + tall);
  println("BMI = " + BMI);
}
float bodyMassCal(float w,float h){
  float result;
  result = w/((h/100)*(h/100));
  return result;
}

/*Calculate circumference and area of a circle from its diameter*/
void setup(){
  int diam=10;  //diameter value
  float circum = circumCal(diam);
  float areaCir = areaCal(diam);
  println("Diameter = " + diam);
  println("Circumference = " + circum);
  println("area of a circle = " + areaCir);
}
float circumCal(float d){
  float result;
  result = 2*PI*(d/2);
  return result;
}
float areaCal(float d){
  float result;
  result=PI*(d/2)*(d/2);
  return result;
}

สิงหาคม 30, 2558

Lab2_clock

void draw(){
  int posX=width/2;
  int posY=height/2;
  float s;
  float m;
  float h;
  size(220,220);
  background(30);
  //clock
  strokeWeight(2);
  fill(#22ED48); //colorGreen
  ellipse(posX,posY,200,200);
  fill(255);
  ellipse(posX,posY,180,180);
  fill(0);
  ellipse(posX,posY-75,10,10);
  ellipse(posX+75,posY,10,10);
  ellipse(posX,posY+75,10,10);
  ellipse(posX-75,posY,10,10);
  //pin
  s=radians(second()*6);
  m=radians(minute()*6);
  h=radians(hour()*30);
  strokeWeight(1);
  line(posX,posY,posX+(70*sin(s)),posY-(70*cos(s)));
  strokeWeight(2);
  line(posX,posY,posX+(50*sin(m)),posY-(50*cos(m)));
  strokeWeight(4);
  line(posX,posY,posX+(25*sin(h)),posY-(24*cos(h)));
}

 
errors: Missing right parenthesis ")"
cause: เกิดจากการใส่วงเล็บปิดไม่ครบ
how to fix: เติมวงเล็บเพิ่มให้ครบจำนวน

Lab2_battery

int posX=35, posY=30;
float resize=150;      //changeable
float charge=0;
void setup(){
  size(350,250);
  frameRate(15);
}
void draw() {
  background(130,120,240); //light_blue
  batteryTank();
  positive();
  negative();
  stroke(0);
}
void batteryTank(){
  float chargeW;
  fill(255);
  textSize(resize/10);
  text("Battery: "+charge+"%",posX+resize/100*25*2.25,posY+resize/100*125);
  //tank
  strokeWeight(3);
  fill(130); //gray
  rect(posX,posY,resize/100*80*2.25,resize/100*100,resize/100*5*2.25);
  rect(posX+resize/100*80*2.25,posY+resize/100*30,resize/100*3*2.25,resize/100*40);
  fill(150);
  rect(posX+resize/100*2.5*2.25,posY,resize/100*75*2.25,resize/100*100,resize/100*2*2.25);
  //power
  chargeW=charge/100*resize/100*75*2.25;
  fill(#38E82C); //green
  rect(posX+resize/100*2.5*2.25,posY,chargeW,resize/100*100,resize/100*2*2.25);
  charge++;
  charge %= 101;
}
void positive(){
  noStroke();
  fill(#DE1F1F); //red
  rect(posX+1.75*resize,posY+resize,resize/10,resize*0.4);
  rect(posX-75*resize/500+1.75*resize,posY+75*resize/500+resize,resize*0.4,resize/10);
}
void negative(){
  noStroke();
  fill(#1752CE); //blue
  rect(posX-75*resize/500+0.1*resize,posY+75*resize/500+resize,resize*0.4,resize/10);
}

Lab2_positive

int red=255, green=255 ,blue=255;
int posX=0;
void draw() {
  size(300,300);
  background(240,30,35);
  positive();
}
void positive(){
  int resize=100;
  int posY=50;
  noStroke();
  fill(red,green,blue);
  rect(posX-resize*1.25,posY,resize/2,resize*2);
  rect(posX-resize*1.25-75*resize/100,posY+75*resize/100,resize*2,resize/2);
  posX++;
  posX%=resize*5;
}
void mouseMoved(){
  red+=4;
  green+=2;
  blue++;
  red %= 256;
  green %= 256;
  blue %= 256;
}
 
errors: The variable "resize" does not exist
cause: เกิดจากการประกาศตัวแปรผิด function [ใส่ไว้ใน void draw()]
how to fix: ย้ายคำสั่งประกาศตัวแปรมาใส่ใน function ที่มีการใช้ตัวแปรนั้นๆ [ต้องใส่ใน void positive()]

Lab1_song

void setup(){
  int posX=280;
  size(350,185);
  background(#F7DEA7); //colorLight_Brown
  noStroke();
  //draw"THE"
  fill(0); //colorBlack
  rect(posX-25,10,70,30); //blank"THE"
  fill(#F7DEA7);
  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(0);
  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(#F7DEA7); //colorLight_Brown
  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(0);
  rect(155,50,10,97); //R
}

Lab1_movie


void setup(){
  int posX=200,posY=110;
  size(250,250);
  background(#FA2B2B); //red
  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,OPEN); //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,CHORD); //chin
  fill(255);
  ellipse(posX+70,posY+125,10,10);
  ellipse(posX-20,posY+125,10,10);
}

สิงหาคม 23, 2558

Lab1_Battery

void setup() {
  int posX=35, posY=30;
  float resize=150;
  float charge=70;
  size(350,250);
  background(130,120,240); //light_blue
  textSize(resize/10);
  text("Battery: "+charge+"%",posX+resize/100*25*2.25,posY+resize/100*125);
  //tank
  strokeWeight(3);
  fill(130); //gray
  rect(posX,posY,resize/100*80*2.25,resize/100*100,resize/100*5*2.25);
  rect(posX+resize/100*80*2.25,posY+resize/100*30,resize/100*3*2.25,resize/100*40);
  fill(150);
  rect(posX+resize/100*2.5*2.25,posY,resize/100*75*2.25,resize/100*100,resize/100*2*2.25);
  //power
  charge=charge/100*resize/100*75*2.25;
  fill(#38E82C); //green
  rect(posX+resize/100*2.5*2.25,posY,charge,resize/100*100,resize/100*2*2.25);
}
 

Lab1_book

void setup(){
  int posX=150, posY=200;
  size(300,400);
  background(0);  //colorBlack
  //bones
  fill(255);  //colorWhite
  noStroke();
  beginShape();
  vertex(posX-90,posY-110);
  vertex(posX+110,posY+90);
  vertex(posX+110,posY+110);
  vertex(posX+90,posY+110);
  vertex(posX-110,posY-90);
  vertex(posX-110,posY-110);
  endShape();
  beginShape();
  vertex(posX+90,posY-110);
  vertex(posX-110,posY+90);
  vertex(posX-110,posY+110);
  vertex(posX-90,posY+110);
  vertex(posX+110,posY-90);
  vertex(posX+110,posY-110);
  endShape();
  ellipse(posX-100,posY-120,30,30);
  ellipse(posX-120,posY-100,30,30);
  ellipse(posX+100,posY-120,30,30);
  ellipse(posX+120,posY-100,30,30);
  ellipse(posX+120,posY+100,30,30);
  ellipse(posX+100,posY+120,30,30);
  ellipse(posX-100,posY+120,30,30);
  ellipse(posX-120,posY+100,30,30);
  //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,OPEN);
  fill(255,210,50); //colorYellow
  arc(posX,posY,170,170,PI+PI/7,TWO_PI-PI/7,CHORD);
  arc(posX,posY+30,320,115,PI+PI/5,TWO_PI-PI/5,PIE);
  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); //R-eye
  ellipse(posX-35,posY+25,50,50); //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);
}

Lab1_Generic exercise

/*Calculate body mass index (BMI)*/
void setup(){
  float BMI;        //body mass index
  float weight= 60;  //weight(kg)
  float tall= 175;   //height(cm)
  BMI=weight/((tall/100)*(tall/100));
  println("Weight = " + weight);
  println("Height = " + tall);
  println("BMI = " + BMI);
}
/*Calculate circumference and area of a circle from its diameter*/
void setup(){
  float diam=10;  //diameter value
  float circum, areaCir;
  circum = 2*PI*(diam/2);
  areaCir=PI*(diam/2)*(diam/2);
  println("Diameter = " + diam);
  println("Circumference = " + circum);
  println("area of a circle = " + areaCir);
}

Lab1_positive sign

void setup() {
  int posX=125, posY=50;
  int resize=100;
  size(300,300);
  background(240,30,35);
  noStroke();
  rect(posX,posY,resize/2,resize*2);
  rect(posX-75*resize/100,posY+75*resize/100,resize*2,resize/2);
}
 

สิงหาคม 16, 2558

Lab0_Book

/*...onePiece Manga...*/
void setup(){
  size(300,400);
  background(0);  //colorBlack
  //bones
  fill(255);  //colorWhite
  noStroke();
  beginShape();
  vertex(60,40);
  vertex(260,240);
  vertex(260,260);
  vertex(240,260);
  vertex(40,60);
  vertex(40,40);
  endShape();
  beginShape();
  vertex(240,40);
  vertex(40,240);
  vertex(40,260);
  vertex(60,260);
  vertex(260,60);
  vertex(260,40);
  endShape();
  ellipse(50,30,30,30);
  ellipse(30,50,30,30);
  ellipse(250,30,30,30);
  ellipse(270,50,30,30);
  ellipse(270,250,30,30);
  ellipse(250,270,30,30);
  ellipse(50,270,30,30);
  ellipse(30,250,30,30);
  //skull & hat
  stroke(0);
  strokeWeight(4);
  ellipse(150,240,80,90);
  fill(145,55,25);  //colorBrown
  arc(150,150,170,170,PI+PI/16,TWO_PI-PI/16,OPEN);
  fill(255,210,50); //colorYellow
  arc(150,150,170,170,PI+PI/7,TWO_PI-PI/7,CHORD);
  arc(150,180,320,115,PI+PI/5,TWO_PI-PI/5,PIE);
  fill(255); //colorWhite
  arc(150,150,170,170,0,PI);
  arc(150,150,170,30,PI,TWO_PI);
  //detail
  fill(0);
  ellipse(150,210,20,20); //nose
  ellipse(185,175,50,50); //R-eye
  ellipse(115,175,50,50); //L-eye
  noFill();
  arc(150,150,195,195,HALF_PI-PI/8,HALF_PI+PI/8);
  arc(150,150,220,220,HALF_PI-PI/8+PI/48,HALF_PI+PI/8-PI/48);
  line(130,235,127,255);
  line(150,235,150,260);
  line(170,235,173,255);
  //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);
}


หมายเหตุ : ภาพได้จากโปรแกรม Processing 3.0b3 เนื่องจาก website ไม่สามารถ run ตัวแปรของ mode จากคำสั่ง arc(a,b,c,d,start,stop,mode) ได้

Lab0_Movie

/*...StarWars(Darth_Vader)...*/
void setup(){
  size(250,250);
  background(#FA2B2B);
  strokeWeight(3);
  stroke(100); //lnColorGray
  fill(0); //colorBlack
  quad(5,215,245,215,209,119,41,119); //wing
  arc(125,10,550,455,HALF_PI-PI/7,HALF_PI+PI/7); //wing
  quad(47,115,203,115,175,225,75,225); //face
  arc(125,90,160,160,PI-PI/8,TWO_PI+PI/8,OPEN); //head
  quad(137,121,190,121,245,215,212,115);
  quad(113,121,60,121,5,215,37,115);
  arc(175,120,75,60,PI,TWO_PI); //R-eyebrow
  arc(75,120,75,60,PI,TWO_PI);  //L-eyebrow
  fill(255); //colorWhite
  ellipse(155,145,70,35); //R-eye
  ellipse(95,145,70,35); //L-eye
  fill(0);
  triangle(75,225,175,225,125,165); //mouth
  stroke(255); //lnColorWhite
  line(91,208,91,225);
  line(108,188,108,225);
  line(125,180,125,225);
  line(142,188,142,225);
  line(159,208,159,225);
  stroke(100);
  fill(255);
  rect(113,165,25,15); //nose
  fill(0);
  rect(115,8,20,145);
  quad(115,153,135,153,138,165,113,165);
  arc(125,225,100,40,0,PI,CHORD); //chin
  fill(255);
  ellipse(170,225,10,10);
  ellipse(80,225,10,10);
}

Lab0_Song

/*...theScript band...*/
void setup(){
  size(350,185);
  background(#F7DEA7); //colorLight_Brown
  noStroke();
  //rect(15,50,320,125); //blank"SCRIPT"
  //draw"THE"
  fill(0); //colorBlack
  rect(75,10,70,30); //blank"THE"
  fill(#F7DEA7);
  rect(75,15,7,25);
  rect(87,15,7,25); //T
  rect(100,10,15,12);
  rect(100,27,15,13); //H
  rect(120,10,3,30);
  rect(128,15,17,7);
  rect(128,27,17,8); //E
  //draw"SCRIPT"
  fill(0);
  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(#F7DEA7); //colorLight_Brown
  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(130,10,155,80,155,10);
  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(0);
  rect(155,50,10,97); //R
}