วันพฤหัสบดีที่ 24 กันยายน พ.ศ. 2558

Lab 5 : Find index of (the last) maximum value in array

def setup():
  n=[1,5,10,4,6,8,7,10,1,1,9,1]
  print("the last index of maximum number = ",lastIndex(n))
  
def lastIndex(n):   
  i=len(n)-1
  index=0
  max_n=n[i]
  while(i>0):
    
     if(max_n<n[i]):
        max_n=n[i]
        index=i
     i=i-1      
  return index        
setup()

Lab 5 : elements (value) of array and its index

def setup():
  n=[2,5,9,4,6,8,7,10,1,1,9,1]
  i=0
  while(i<len(n)):
     print("index ",i," = ",n[i])
     i=i+1     
              
setup()

Lab 5 : Find/count number of positive values in array

def setup():
  n=[2,5,-9,4,-6,8,7,-10,-1,-1,9,1]
  print("number of positive = ",findPos(n))

def findPos(n):
   i=0
   countpos=0
   while(i<len(n)):
      if(n[i]>0):
         countpos=countpos+1
      i=i+1
   return countpos
setup()

lab 5 : Find average of values in array

def setup():
  n=[2,5,9,4,6,8,7,10,1,1,9,1]
  print("sum of value = ",findsum(n))
  average(n)

def findsum(n):
   i=0
   sumArray=0
   while(i<len(n)):
      sumArray=sumArray+n[i]
      i=i+1
   return sumArray

def average(n):
   average=findsum(n)/len(n)
   print ("average = ",average)
  
setup()

Lab 5 : Find sum of positive values in array

def setup():
  n=[2,5,-9,4,-6,8,7,-10,-1,-1,9,1]
  print("sum of value positive = ",findsumPos(n))

def findsumPos(n):
   i=0
   sumArray=0
   while(i<len(n)):
      if(n[i]>0):
         sumArray=sumArray+n[i]
      i=i+1
   return sumArray
setup()

lab 5 : Find sum of values in array

def setup():
  n=[2,5,9,4,6,8,7,10,1,1,9,1]
  print("sum of value = ",findsum(n))

def findsum(n):
   i=0
   sumArray=0
   while(i<len(n)):
      sumArray=sumArray+n[i]
      i=i+1
   return sumArray
setup()

Lab 5 : Find index of (the first) maximum value in array

def setup():
  n=[1,5,10,4,6,8,7,10,1,1,9,1]
  print("the first index of maximum number = ",firstIndex(n))
  
def firstIndex(n):   
  i=0
  index=0
  max_n=n[0]
  while(i<len(n)):
    
     if(max_n<n[i]):
        max_n=n[i]
        index=i
     i=i+1      
  return index  
    
setup()

Lab 5 : Find the maximum value in array

def setup():
  n=[2,5,9,4,6,8,7,10,1,1,9,1]
  print("maximum value = ",findmax(n))

def findmax(n):
  i=0
  max_n=n[0]
  while(i<len(n)):
     if(max_n<n[i]):
        max_n=n[i]
     i=i+1      
  return max_n   
           
setup()

วันเสาร์ที่ 19 กันยายน พ.ศ. 2558

Lab 4X : Circle

def setup(): r=10 circumference=cal_circumference(r) area=cal_area(r)
print("Radius = ",r) print("Circumference = ",circumference) print("Area = ",area)
def cal_circumference(r): circumference=3.14*r*2 return circumference
def cal_area(r): area=3.14*r*r return area
setup()

Lab 4X : BMI

def setup():
myWeight=50 myHeight=160
myBMI=BMI(myHeight,myWeight) print("Height = ",myHeight) print("Weight = ",myWeight) print("Body Mass Index = ",myBMI)
def BMI(myHeight,myWeight): Height_M=myHeight/100 myBMI=myWeight/(Height_M*Height_M) return myBMI;
setup()

Lab 4X : Number sum

def setup(): num=10 print("sum = ",sum(num))
def sum(num): x=1 sum=0 while(x<=num): sum=sum+x x=x+1 return sum
setup()

Lab 4X : Sum of Prime Number

def setup(): min=0 max=10
print("SUM OF PRIME 0 - ",max," = ",sum_prime(min,max))
def sum_prime(min,max): x = 1 sum = 0 while (x <= max): if (prime(x)): sum += x x=x+1 return sum
def prime(count): x = 2 if (count == 1): return 0 while (x<count): if (count%x == 0 ): return 0 x=x+1 return 1
setup()

Lab 4X : Leapyears

def setup():
year=1997 LeapYears(year)
def LeapYears(year):
if(year%4==0 and year%100==0 and year%400==0 or year%4==0 and year%100!=0): print(year," is leap year") elif(year%4==0 and year%100==0 or year%4!=0): print(year," isn't leap year") setup()

Lab 4X : Multiplication Table

def setup(): multiTab=2 number=12 count=1 while(count<=number): ans=multiTab*count print(multiTab," x ",count," = ",ans) count=count+1
setup()

Lab 4X : Calculate grade from score

def setup(): score=79 print("Your score = ",score) cal_grade(score)
def cal_grade(score): if(score<50): print("You get grade F ") elif(score>=50 and score < 55): print("You get grade D ") elif(score>=55 and score < 60): print("You get grade D+ ") elif(score>=60 and score < 65): print("You get grade C ") elif(score>=65 and score < 70): print("You get grade C+ ") elif(score>=70 and score < 75): print("You get grade B ") elif(score>=75 and score < 80): print("You get grade B+ ") elif(score>=80 and score <=100): print("You get grade A ") setup()

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

Lab 4 : Balloon

 
void setup(){
  size(300,300);
}
void draw(){
  background(#ffffff);
  int posX=0;
  int posY=mouseY;
  int num=5;
  if(posY<100){
    fill(#F28EED);
  }else if(posY>=100 && posY<200){
    fill(#B48EF2);
  }else{
    fill(#486FD8);
  }

  draw_balloon(posX,posY,num);
}
void draw_balloon(int posX,int posY,int num){
  int count=0;
  int spread=0;
 
  while(count<num){
  strokeWeight(4);
  stroke(#623936);
  int string_length =70;
  line(posX+spread,posY,posX+spread,posY+ string_length);
  ellipse(posX+spread,posY,50,50);
 
  spread+=70;
  count++;
  }
}

Lab 4 : Flock of birds

 
float wing=0;
void setup(){
  size(500,500);
  frameRate(100);
}

void draw(){
  int X=mouseX;
  int Y=mouseY;
  int cloudX;
  int cloudY;
  int num=3;
  int row=num;
  background(#77D8E8);

  cloud(50,20);
  cloud(450,200);
  cloud(200,400);
   wing=mouseY;
  if(frameCount%60>30){
    wing+=40;
  }else{
    wing-=40;
  }
  if(mouseY<=250){
    wing+=10;
  }else{
    wing-=10;
  }
  draw_bird(X,Y,num,row);

}

void draw_bird(int X,int Y,int num,int row){
  int countX=0;
  int countY=0;
  int spreadX=100;
  int spreadY=100;
  int New=0;
 
 
  while(countX<row){
    while(countY<num){
  stroke(0);
  fill(#FFB40F);
  quad(X-40+spreadX,Y+spreadY,  X-70+spreadX,Y-10+spreadY,  X-100+spreadX,wing+spreadY,  X-70+spreadX,Y+10+spreadY);
  quad(X+40+spreadX,Y+spreadY,  X+70+spreadX,Y-10+spreadY,  X+100+spreadX,wing+spreadY,  X+70+spreadX,Y+10+spreadY);



 fill(#FF0F64);
 ellipse(X+spreadX,Y+spreadY,100,120);       //body
 fill(#FFFFFF);
 ellipse(X-30+spreadX,Y-20+spreadY,50,50);         //eye outside
 ellipse(X+30+spreadX,Y-20+spreadY,50,50);
 fill(0);
 ellipse(X-30+spreadX,Y-10+spreadY,15,15);         //eye inside
 ellipse(X+30+spreadX,Y-10+spreadY,15,15);
 fill(#ffffff);
 ellipse(X-30+spreadX,Y-15+spreadY,5,5);         //eye
 ellipse(X+30+spreadX,Y-15+spreadY,5,5);
 fill(#FCF624);
 quad(X+spreadX,Y+spreadY,X+10+spreadX,Y+10+spreadY,X+spreadX,Y+25+spreadY,X-10+spreadX,Y+10+spreadY);  //lip

 spreadX+=200;
countY++;

 //spreadY+=100;
  }
  spreadX=New;
  countY=0;
  spreadY+=100;
  New+=100;
 countX++;
}

}
void cloud(int cloudX,int cloudY){
  noStroke();
  fill(#ffffff);
  ellipse(cloudX,cloudY,80,80);
  ellipse(cloudX-50,cloudY+30,80,70);
  ellipse(cloudX+60,cloudY+30,100,80);
  ellipse(cloudX+10,cloudY+50,100,100);
}

Lab 4 : Book

 
float mycolor;
void setup(){
size(600,600);


}
void draw(){
 background(#FCFDFF);
 //int posX=posX+2;
 int posX=mouseX;
 int posY=mouseY;
 int count = 1;
 int num = 3;

  while(count<=num){
  draw_book(posX, posY);
  posX = posX + 50;
  posY = posY + 50;
  count++;
 if(posX<100){
    fill(#FF585A);
  }else if(posX>=100 && posX<200){
    fill(#FC94F0) ;
  }else if(posX>=200 && posX<300){
    fill(#E058FF) ;
  }else if(posX>=300 && posX<400){
    fill(#8358FF) ;
  }else if(posX>=400 && posX<500){
    fill(#58AAFF) ;
  }else{
    fill(#58FF88) ;
  }

  }
  count=1;
}

void draw_book(int posX,int posY){

  int x=455+posX;
  int x2=5+posX;
  int y=455+posY;
  int y2=5+posY;
  println("PUCCA");
  print("2015");

  strokeWeight(4);             //front cover
  //fill(mycolor);
  quad(posX,posY,450+posX,posY,450+posX,450+posY,posX,450+posY);

  fill(#000000);               //bun hair
  ellipse(80+posX,100+posY,130,130);
  ellipse(370+posX,100+posY,130,130);

  fill(#FC4747);               //ribbon
  ellipse(315+posX,150+posY,80,80);
  ellipse(125+posX,150+posY,80,80);

  fill(#000000);               //hair
  ellipse(222+posX,210+posY,300,250);

  fill(#FFF6C9);               //face
  ellipse(222+posX,230+posY,250,200);

  strokeWeight(5);             //eyes
  line(110+posX,220+posY,170+posX,250+posY);
  line(335+posX,220+posY,265+posX,250+posY);

  noStroke();                  //cheek
  fill(#FFB6A2);
  ellipse(310+posX,275+posY,25,25);
  ellipse(133+posX,275+posY,25,25);

  stroke(#000000);             //mouth
  noFill();
  arc(220+posX, 280+posY, 40, 50, 0,PI);

  fill(#FFF152);               //Pucca
  textSize(80);
  textAlign(RIGHT);
  text("PUCCA", 360+posX, 420+posY);

  strokeWeight(2);             //paper
  line(x2,y,x,y);
  line(x2+5,y+5,x+5,y+5);
  line(x2+10,y+10,x+10,y+10);
  line(x2+15,y+15,x+15,y+15);
  line(x2+20,y+20,x+20,y+20);

  line(x,y2,x,y);
  line(x+5,y2+5,x+5,y+5);
  line(x+10,y2+10,x+10,y+10);
  line(x+15,y2+15,x+15,y+15);
  line(x+20,y2+20,x+20,y+20);

  line(posX,450+posY,25+posX,475+posY);
  line(450+posX,posY,475+posX,25+posY);
}

Lab 4 : Linking Park

 
 int posX=0;
 int posY=0;

void setup(){
  size(600,600);
}

void draw(){
 background(0);
 int num=2;
 posY=posY+2;
 draw_linkin(posX,posY,num);


 if(posY>600){
   posY=-50;
 }
}

void draw_linkin(int posX,int posY,int num){
  int n=0;
  int spread=0;
  while(n<=2){
 
  fill(#ffffff);
  ellipse(200+posX+spread,240+posY,350,350);        //inside circle
  fill(#000000);
  ellipse(200+posX+spread,240+posY,320,320);   

  fill(#ffffff);
  triangle(200+posX+spread,100+posY,80+posX+spread,320+posY,320+posX+spread,320+posY);     //triangle
  fill(#000000);
  triangle(200+posX+spread,150+posY,120+posX+spread,300+posY,280+posX+spread,300+posY);  
 
  strokeWeight(30);                //black line
  stroke(#000000);
  line(238+posX+spread,posY-10,92+posX+spread,260+posY);
  line(265+posX+spread,70+posY,161+posX+spread,260+posY);
  line(273+posX+spread,70+posY,163+posX+spread,260+posY);
  line(178+posX+spread,360+posY,126+posX+spread,450+posY);
 

  stroke(#ffffff);                 //white line                                                 
  line(233+posX+spread,200+posY,128+posX+spread,385+posY);

  strokeWeight(5);                 //outside circle
  noFill();
  ellipse(200+posX+spread,240+posY,370,370);
  spread+=400;
  n++;
  }
}

Lab 4 : movie

 

void setup() {
  size(600,600);
}
void draw(){
 background(#FFEC85);
 int num = 2; // number of ant
 int count = 1;
 int X=mouseX;
 int Y=200;

 X=(X+2);


 if(X<=100 || X>=500){
   fill(#FF3E6E);
   textSize(40);
   textAlign(CENTER);
   text("I LOVE RIRAKUMA",300,550);
 }



  while(count<=num){
 draw_bear(X,Y);
 Y-=300;
 count++;
 }
}


void draw_bear(int X,int Y){
  int x=30;
  int x2=270;
  int y=170;

  strokeWeight(6);
  stroke(#624403);

  fill(#E09A2C);                        //outside ear
  ellipse(X,Y,130,130);
  ellipse(290+X,Y,130,130);

  fill(#FFE63D);                        //inside ear
  ellipse(-10+X,20+Y,80,80);
  ellipse(300+X,20+Y,80,80);

  fill(#E09A2C);                        //face
  ellipse(150+X,120+Y,340,280);

  fill(#624403);                        //eye
  ellipse(60+X,130+Y,20,30);
  ellipse(235+X,130+Y,20,30);

  noStroke();                           //nose
  fill(#ffffff);
  ellipse(150+X,175+Y,110,80);
  fill(#624403);
  ellipse(150+X,160+Y,25,20);

  noFill();                                //mouth
  stroke(#624403);
  arc(178+X,170+Y,55,55,HALF_PI,PI);
  arc(122+X,170+Y,55,55,0,HALF_PI);

  stroke(#FA4A46);                 //left cheek
  line(x-15+X,y+Y,x-25+X,y+20+Y);
  line(x+X,y+Y,x-10+X,y+20+Y);
  line(x+15+X,y+Y,x+5+X,y+20+Y);

  line(x2-15+X,y+Y,x2-25+X,y+20+Y);     //right cheek
  line(x2+X,y+Y,x2-10+X,y+20+Y);
  line(x2+15+X,y+Y,x2+5+X,y+20+Y);
}

Lab 4 : Multiplication Table

 
void setup(){
  int multiTab=2;       
  int number=12;
  int count=1;
 
  while(count<=number){
    int ans=multiTab*count;
    println(+multiTab+" x "+count+" = "+ans);
    count++;
  }
 
}

Lab 4 : Number sum

 
void setup(){
  int num=10;
  println(sum(num));
}

int sum(int num){
 int x=1;
 int sum=0;
 while(x<=num){
  sum=sum+x;
 x++;
 }
 return sum;
}

Lab 4 :Sum of Prime Number

 
void setup() {
  size(400,100);
  int min=0;
  int max=10;
 
  background(0);
  textSize(20);
  textAlign(CENTER);
  text("SUM OF PRIME 0 - "+max+" = "+sum_prime(min,max),200,50);
}
int sum_prime(int min,int max) {
  int x = 1;
  int sum = 0;
  while (x <= max) {
    if (prime(x)) {
      sum += x;
    }
    x++;
  }
  return sum;
}
boolean prime(int count) {
 
  int x = 2;
  while (x<count) {
    if (count%x == 0 ) {
      return false;
    }
    x++;
  }
  if (count == 1){
  return false;
  }
  return true;
}

Lab 4 : Loan Payment

 
void setup(){
  size(500,600);
  textSize(15);
  background(0);
 
  loan_money(5000,12,1);
}
void loan_money(float amount, float rate, int year){
  int month = year*12;
  float per = (rate/100)/month;
  float paymentAmount = amount*(per/(1-pow(1+per,-month)));
  float principal = paymentAmount;
  float unpaid = amount;
  float total = 0;
  int count = 1;
  int y=0;
 
  textAlign(LEFT);
  text("Num.",20,50);
  text("Interest",100,50);
  text("Principal",200,50);
  text("Unpaid",300,50);
  text("Interest total",380,50);
 
  //textAlign(CENTER);
 
 
  while(count <= month){
    text(nf(count, 2),30,100+y); //Num
    text(nf(per*unpaid, 2, 2),100,100+y); //Interest
    total+=per*unpaid;
    principal=paymentAmount-(per*unpaid);
    unpaid =unpaid-principal;
   
    if(unpaid < 0){
      unpaid = 0;
    }
    text(nf(principal, 3, 2),200,100+y); //Principal
    text(nf(unpaid, 4, 2),300,100+y); //Unpaid
    text(nf(total, 3, 2),400,100+y); //Total
   
    count++;
    y+=40;
  }
}

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

Lab 3 : Delivery Charge

 
void setup(){
  background(#3B7EE8);
  size(500,500);
  textAlign(CENTER);                  //text
  textSize(40);
  fill(#ffffff);
  text("Delivery Charge",width/2,70);

  // package
  //     Letter = 1        Box = 2
  // Service
  //     Next Day Priority = 1    Next Day Standard = 2    2-Day = 3

  int pack=2;
  int ser=3;
  int weight=2;

  charge(pack,ser,weight);
}

void charge(int pack,int ser,int weight){

  textSize(25);
  textAlign(LEFT);

  if(pack==1){
    text("Package Type = Letter",50,height/2-100);
  }else if(pack==2){
    text("Package Type = Box",50,height/2-100);
  }else{
    text("Package Type = No Package",50,height/2-100);
  }

  // ----------- PACKAGE LETTER ----------
  if(pack==1 && ser==1){
    text("Your Service = Next Day Standard",50,height/2);
     if(weight<=8){
       text("Your Weight = "+weight+" oz",50,height/2+100);
       text("Cost = 10.50 $",50,height-50);
     }
     if(weight>8){
       text("Your Weight = Overweight",50,height/2+100);
       text("Cost = - $",50,height-50);
     }
  }if(pack==1 && ser==2){
    text("Your Service = Next Day Priority",50,height/2);
     if(weight<=8){
       text("Your Weight = "+weight+" oz",50,height/2+100);
       text("Cost = 12 $",50,height-50);
     }
     if(weight>8){
       text("Your Weight = Overweight",50,height/2+100);
       text("Cost = - $",50,height-50);
     }
  }if(pack==1 && ser==3){
    text("Your Service = 2-Day",50,height/2);
    text("Your Weight = Not available",50,height/2+100);
    text("Cost = Not available",50,height-50);
  }

  // ----------- PACKAGE BOX ----------
  float weightPack2Ser1 = (weight/weight*15.75)+(weight-1)*1.25;
  float weightPack2Ser2 = (weight/weight*13.75)+(weight-1)*1;
  float weightPack2Ser3 = (weight/weight*7)+(weight-1)*0.5;
  if(pack==2 && ser==1){
    text("Your Service = Next Day Standard",50,height/2);
    text("Your Weight = "+weight+" pound",50,height/2+100);
    text("Cost = "+weightPack2Ser1+"$",50,height-50);
  }if(pack==2 && ser==2){
    text("Your Service = Next Day Standard",50,height/2);
    text("Your Weight = "+weight+" pound",50,height/2+100);
    text("Cost = "+weightPack2Ser2+"$",50,height-50);
  }if(pack==2 && ser==3){
    text("Your Service = Next Day Standard",50,height/2);
    text("Your Weight = "+weight+" pound",50,height/2+100);
    text("Cost = "+weightPack2Ser3+"$",50,height-50);
  }


}

Lab 3 : Power of ten

 
void setup(){
  size(400,300);
  background(0);
 
  int power=30;
  powerTen(power);
}

void powerTen(int power){
  String word;
  int X=200;
  int Y=100;
 
 
  if (power == 6) {
    word="Million";
  } else if (power == 9) {
    word="Billion";
  } else if (power == 12) {
    word="Trillion";
  } else if (power == 15) {
    word="Quadrillion";
  } else if (power == 18) {
    word="Quintillion";
  } else if (power == 21) {
    word="Sextillion";
  } else if (power == 30) {
    word="Nonillion";
  } else if (power == 100) {
    word="Googol";
  } else {
    word="-";
  }
 
  textAlign(CENTER);        
  textSize(20);
  fill(#ffffff);
  text("Power of ten is "+power,X,Y);
  text("Word for that number is "+word,X,Y+100);
 
}

Lab 3 : Leap Year

 
void setup(){
  size(400,300);
  int year=1997;
  background(0);

  LeapYears(year);
}

void LeapYears(int year){
  int posX=width/2;
  int posY=150;
  textAlign(CENTER);                  //text
  textSize(20);
  fill(#ffffff);

  if(year%4==0 && year%100==0 && year%400==0 || year%4==0 && year%100!=0){
   text(year+" is leap year",posX,posY);
  }else if(year%4==0 && year%100==0 || year%4!=0){
   text(year+" isn't leap year",posX,posY);
  }
}

Lab 3 : Flying Bird

 
float wing=0;
void setup(){
  size(500,500);
  frameRate(100);
}

void draw(){
  int X=mouseX;
  int Y=mouseY;
  int cloudX;
  int cloudY;
  background(#77D8E8);
 
  cloud(50,20);
  cloud(450,200);
  cloud(200,400);
   wing=mouseY;
  if(frameCount%60>30){
    wing+=40;
  }else{
    wing-=60;
  }
  if(mouseY<=250){
    wing+=40;
  }else{
    wing-=30;
  }
  draw_bird(X,Y);
 
}

void draw_bird(int X,int Y){

  stroke(0);
  fill(#FFB40F);
    quad(X-50,Y,  X-100,Y-10,  X-150,wing,  X-100,Y+10);
  quad(X+50,Y,  X+100,Y-10,  X+150,wing,  X+100,Y+10);
 

 
  fill(#FF0F64);
  ellipse(X,Y,130,150);       //body
  fill(#FFFFFF);
  ellipse(X-30,Y-20,50,50);         //eye outside
  ellipse(X+30,Y-20,50,50);
  fill(0);
  ellipse(X-30,Y-10,20,20);         //eye inside
  ellipse(X+30,Y-10,20,20);
  fill(#ffffff);
  ellipse(X-35,Y-10,5,5);         //eye
  ellipse(X+25,Y-10,5,5);
  fill(#FCF624);
  quad(X,Y+5,X+10,Y+20,X,Y+35,X-10,Y+20);  //lip

}

void cloud(int cloudX,int cloudY){
  noStroke();
  fill(#ffffff);
  ellipse(cloudX,cloudY,80,80);
  ellipse(cloudX-50,cloudY+30,80,70);
  ellipse(cloudX+60,cloudY+30,100,80);
  ellipse(cloudX+10,cloudY+50,100,100);
}

Lab 3 : Battery

 
int value;
int energy = 1;
int per;
void setup(){
  size(600,500);
}

void draw(){
  background(0);
int posX=100;
int posY=100;
 
  draw_positive(posX+450,posY);
  draw_nagative(posX-70,posY);
  draw_battary(posX,posY);

 
  value = value+energy;   
  if(value==380){             //discharge
    energy = 0;
    energy = energy-1;
  }if(value == 0 ){           //charge
   energy = 0;
   energy = energy+1;
  }
 
  int per = (value*100)/380;
  textAlign(CENTER);                  //text
  textSize(40);
  fill(#ffffff);
  text(per+" %", 200+posX,270+posY);
}

void myColor(){
  if(value>80*3.8){
    fill(#50C110);
  }else if(value<=80*3.8 && value>60*3.8){
    fill(#ACDB23);
  }else if(value<=60*3.8 && value>40*3.8){
    fill(#ECF750);
  }else if(value<=40*3.8 && value>20*3.8){
    fill(#FA7C28);
  }else if(value<=20*3.8 && value>0){
    fill(#FA3D28);
  }
}


void draw_battary(int posX,int posY) {

  strokeWeight(10);                      //outside battary
  stroke(#ffffff);
  noFill();
  rect(posX,posY,400,200);

  rect(400+posX,45+posY,50,100);               //battery terminal

  noStroke();   //energy of battary
  myColor();
  //fill(#ffffff);
  rect(10+posX,10+posY,value,180);
}

void draw_positive(int posX,int posY){
  rect(posX+10,posY, 10 ,30);
  rect(posX,posY+10, 30,10);
}

void draw_nagative(int posX,int posY){
  rect(posX,posY+10, 30,10);
}

Lab 3 : Calculate grade from score

 
String grade;
void setup(){
  size(400,300);
  background(#000000);
  float score=79;

  cal_grade(score);
}

void cal_grade(float score){
 int x=50;
 int y=100;

  textAlign(LEFT);
  textSize(30);
  fill(#ffffff);

 if(score<50){
   grade="F";
 }else if(score>=50 && score < 55){
   grade="D";
 }else if(score>=55 && score < 60){
   grade="D+";
 }else if(score>=60 && score < 65){
   grade="C";
 }else if(score>=65 && score < 70){
   grade="C+";
 }else if(score>=70 && score < 75){
   grade="B";
 }else if(score>=75 && score < 80){
   grade="B+";
 }else if(score>=80 && score <=100){
   grade="A";
 }

  textAlign(LEFT);
  fill(#ffffff);
  text("Your score = "+score,x,y);
  text("You get grade  "+grade,x,y+100);
}

Lab 3 : Interaction Book

 
float mycolor;
void setup(){
size(600,600);


}
void draw(){
 background(#FCFDFF);
 //int posX=posX+2;
 int posX=mouseX;
 int posY=mouseY;

 if(posX<100){
    fill(#FF585A);
  }else if(posX>=100 && posX<200){
    fill(#FC94F0) ;
  }else if(posX>=200 && posX<300){
    fill(#E058FF) ;
  }else if(posX>=300 && posX<400){
    fill(#8358FF) ;
  }else if(posX>=400 && posX<500){
    fill(#58AAFF) ;
  }else{
    fill(#58FF88) ;
  }
  draw_book(posX, posY);
}

void draw_book(int posX,int posY){
 
  int x=455+posX;
  int x2=5+posX;
  int y=455+posY;
  int y2=5+posY;
  println("PUCCA");
  print("2015");

  strokeWeight(4);             //front cover
  //fill(mycolor);
  quad(posX,posY,450+posX,posY,450+posX,450+posY,posX,450+posY);

  fill(#000000);               //bun hair
  ellipse(80+posX,100+posY,130,130);
  ellipse(370+posX,100+posY,130,130);

  fill(#FC4747);               //ribbon
  ellipse(315+posX,150+posY,80,80);
  ellipse(125+posX,150+posY,80,80);

  fill(#000000);               //hair
  ellipse(222+posX,210+posY,300,250);

  fill(#FFF6C9);               //face
  ellipse(222+posX,230+posY,250,200);

  strokeWeight(5);             //eyes
  line(110+posX,220+posY,170+posX,250+posY);
  line(335+posX,220+posY,265+posX,250+posY);

  noStroke();                  //cheek
  fill(#FFB6A2);
  ellipse(310+posX,275+posY,25,25);
  ellipse(133+posX,275+posY,25,25);

  stroke(#000000);             //mouth
  noFill();
  arc(220+posX, 280+posY, 40, 50, 0,PI);

  fill(#FFF152);               //Pucca
  textSize(80);
  textAlign(RIGHT);
  text("PUCCA", 360+posX, 420+posY);

  strokeWeight(2);             //paper
  line(x2,y,x,y);
  line(x2+5,y+5,x+5,y+5);
  line(x2+10,y+10,x+10,y+10);
  line(x2+15,y+15,x+15,y+15);
  line(x2+20,y+20,x+20,y+20);

  line(x,y2,x,y);
  line(x+5,y2+5,x+5,y+5);
  line(x+10,y2+10,x+10,y+10);
  line(x+15,y2+15,x+15,y+15);
  line(x+20,y2+20,x+20,y+20);

  line(posX,450+posY,25+posX,475+posY);
  line(450+posX,posY,475+posX,25+posY);
}

Lab 3 : Animation Rilakuma

 
 int X=0;
 int Y=200;

void setup() {
  size(600,600);
}
void draw(){
 background(#FFEC85);

 X=(X+2);
 draw_bear(X,Y);

 if(X<=0 || X>=600){
   fill(#FF3E6E);
   textSize(40);
   textAlign(CENTER);
   text("I LOVE RIRAKUMA",300,550);
 }

 if(X>=650){
   X=-500;
 }

}


void draw_bear(int X,int Y){
  int x=30;
  int x2=270;
  int y=170;

  strokeWeight(6);
  stroke(#624403);

  fill(#E09A2C);                        //outside ear
  ellipse(X,Y,130,130);
  ellipse(290+X,Y,130,130);

  fill(#FFE63D);                        //inside ear
  ellipse(-10+X,20+Y,80,80);
  ellipse(300+X,20+Y,80,80);

  fill(#E09A2C);                        //face
  ellipse(150+X,120+Y,340,280);

  fill(#624403);                        //eye
  ellipse(60+X,130+Y,20,30);
  ellipse(235+X,130+Y,20,30);

  noStroke();                           //nose
  fill(#ffffff);
  ellipse(150+X,175+Y,110,80);
  fill(#624403);
  ellipse(150+X,160+Y,25,20);

  noFill();                                //mouth
  stroke(#624403);
  arc(178+X,170+Y,55,55,HALF_PI,PI);
  arc(122+X,170+Y,55,55,0,HALF_PI);

  stroke(#FA4A46);                 //left cheek
  line(x-15+X,y+Y,x-25+X,y+20+Y);
  line(x+X,y+Y,x-10+X,y+20+Y);
  line(x+15+X,y+Y,x+5+X,y+20+Y);

  line(x2-15+X,y+Y,x2-25+X,y+20+Y);     //right cheek
  line(x2+X,y+Y,x2-10+X,y+20+Y);
  line(x2+15+X,y+Y,x2+5+X,y+20+Y);
}

Lab 3 : Interaction Linkin Park

 
 int posX=0;
 int posY=0;

void setup(){
  size(600,600);
}

void draw(){
 background(0);
 //int posX=posX+2;
 //int posY=posY+2;

 draw_linkin(posX,posY);
}

void keyPressed() {
  int moving = 10;
  if (keyCode == RIGHT) {
    posX = posX + moving;
  } else if (keyCode == LEFT) {
    posX = posX - moving;
  }else if (keyCode == UP) {
    posY = posY - moving;
  }else if (keyCode == DOWN) {
    posY = posY + moving;
  }
}

void draw_linkin(int posX,int posY){
  fill(#ffffff);
  ellipse(240+posX,240+posY,450,450);        //inside circle
  fill(#000000);
  ellipse(240+posX,240+posY,400,400);    

  fill(#ffffff);
  triangle(240+posX,70+posY,90+posX,340+posY,390+posX,340+posY);     //triangle
  fill(#000000);
  triangle(240+posX,120+posY,130+posX,320+posY,350+posX,320+posY);  

  strokeWeight(30);                //black line
  stroke(#000000);
  line(260+posX,posY-10,114+posX,260+posY);
  line(287+posX,70+posY,183+posX,260+posY);
  line(295+posX,70+posY,185+posX,260+posY);
  line(200+posX,360+posY,148+posX,450+posY);
 
  stroke(#ffffff);                 //white line                                                  
  line(275+posX,165+posY,138+posX,408+posY);

  strokeWeight(5);                 //outside circle
  noFill();
  ellipse(240+posX,240+posY,470,470);
}

วันอาทิตย์ที่ 30 สิงหาคม พ.ศ. 2558

Lab 3 : Balloon

 
void setup(){
  size(300,300);
}
void draw(){
  background(#ffffff);
  int posX=mouseX;
  int posY=mouseY;

  if(posY<100){
    fill(#F28EED);
  }else if(posY>=100 && posY<200){
    fill(#B48EF2);
  }else{
    fill(#486FD8);
  }

  draw_balloon(posX,posY);
}
void draw_balloon(int posX,int posY){
  strokeWeight(4);
  stroke(#623936);
  int string_length =70;
  line(posX,posY,posX,posY+ string_length);
  ellipse(posX,posY,50,50);

}

Lab 2 : Syntax Error

1.Missing a semicolon ";"
      สาเหตุ
            เกิดจากการลืมใส่เครื่องหมาย " ; "
     วิธีแก้ปัญหา
            ตรวจสอบบรรทัดที่โปรแกรมเตือน Syntax Error และเติมเครื่องหมายให้ถูกต้อง

2.The variable "PercentBatt" does not exist
     สาเหตุ
            พิมชื่อตัวแปรผิด ประกาศตัวแปรไว้ไม่ตรงกับ local ที่เราใช้ หรือไม่ได้ประกาศตัวแปร
     วิธีแก้ปัญหา
            ตรวจสอบชื่อตัวแปร แก้ชื่อตัวแปรให้ตรงกับที่ประกาศไว้ถ้ายังไม่ได้ประกาศตัวแปร ก็ทำการประกาศให้ตรงกับ local ที่ใช้

3.The local variable "posX" may not have been intialized
     สาเหตุ
            เกิดจากการที่ยังให้ได้กำหนดค่าให้ตัวแปร
     วิธีแก้ปัญหา
            กำหนดค่าให้ตัวแปรที่โปรแกรมแจ้งเตือน

Lab 2 : Function Positive

 
void setup(){
  size(400,400);
  frameRate(10);
}

void draw(){
  background(0);
  //float fill_pos=random(0,255);
  draw_positive(mouseX,mouseY,50);
}

void draw_positive(int X,int Y,int sizePos){
  fill(#D8D8D8);          //shadow
  noStroke();
  rect(X+sizePos+3,Y+3,sizePos+3,sizePos*3+3);
  rect(X+3,Y+sizePos+3,sizePos*3+3,sizePos+3);
 
  float fill_pos1=random(0,255);
  float fill_pos2=random(0,255);
  float fill_pos3=random(0,255);
  noStroke();           //positive
  fill(fill_pos1,fill_pos2,fill_pos3);
  rect(X+sizePos,Y,sizePos,sizePos*3);
  rect(X,Y+sizePos,sizePos*3,sizePos);
}

Lab 2 : Function Battary

 
int PercentBatt;

void setup(){
  size(600,500);
}

void draw(){
  background(#74A3FF);
int posX=100;
int posY=100;
  draw_battary(posX,posY);
  draw_positive(posX+450,posY);
  draw_nagative(posX-70,posY);
 
  textAlign(CENTER);                  //text
  textSize(40);
  fill(#ffffff);
  text(PercentBatt+" %", 200+posX,270+posY);
}


void mousePressed(){
  int increaseX;
 
  int count=PercentBatt;
  if(count<100){
  increaseX=10;
    PercentBatt = (PercentBatt + increaseX);
  }if(count>=100){
   PercentBatt =0;
  }
}


void draw_battary(int posX,int posY) {

  strokeWeight(10);                      //outside battary
  stroke(#ffffff);
  noFill();
  rect(posX,posY,400,200);

  rect(400+posX,45+posY,50,100);               //battery terminal

  noStroke();                               //energy of battary
  fill(#ffffff);
  rect(10+posX,10+posY,PercentBatt*3.8,180);
}

void draw_positive(int posX,int posY){
  rect(posX+10,posY, 10 ,30);
  rect(posX,posY+10, 30,10);
}

void draw_nagative(int posX,int posY){
  rect(posX,posY+10, 30,10);
}

Lab 2 : Clock

 
void setup(){
  size(300, 300);
}
void draw(){
  background(0);
  draw_clock(width/4,height/3);
  draw_time(width/2, height/2-20);
  draw_date(width/2, height/2+20);
}
void draw_clock(int x,int y){
  noStroke();
  fill(#ffffff);
  rect(x-5,y-5,150+10,90+10, 7);
  fill(#8047FC);
  rect(x,y,150,90, 7);
}
void draw_time(int x, int y){
  fill(#ffffff);
  textSize(20);
  textAlign(CENTER);
  text(hour()+":"+minute()+":"+second(), x, y);
}
void draw_date(int x, int y){
  fill(#ffffff);
  textSize(20);
  textAlign(CENTER);
  text(day()+"/"+month()+"/"+year(), x, y);
}

Lab 2 : Function book

 
 int posX=0;
 int posY=0;
void setup(){
size(600,600);


}
void draw(){
 background(#FCFDFF);
 //int posX=posX+2;
 //int posY=posY+2;

 draw_book(posX,posY);

 posX=(posX+2)% width;
 posY=(posY+2)%height;
}

void draw_book(int posX,int posY){

  int x=455+posX;
  int x2=5+posX;
  int y=455+posY;
  int y2=5+posY;
  println("PUCCA");
  print("2015");

  strokeWeight(4);             //front cover
  fill(#FF1A29);
  quad(posX,posY,450+posX,posY,450+posX,450+posY,posX,450+posY);

  fill(#000000);               //bun hair
  ellipse(80+posX,100+posY,130,130);
  ellipse(370+posX,100+posY,130,130);

  fill(#FC4747);               //ribbon
  ellipse(315+posX,150+posY,80,80);
  ellipse(125+posX,150+posY,80,80);

  fill(#000000);               //hair
  ellipse(222+posX,210+posY,300,250);

  fill(#FFF6C9);               //face
  ellipse(222+posX,230+posY,250,200);

  strokeWeight(5);             //eyes
  line(110+posX,220+posY,170+posX,250+posY);
  line(335+posX,220+posY,265+posX,250+posY);

  noStroke();                  //cheek
  fill(#FFB6A2);
  ellipse(310+posX,275+posY,25,25);
  ellipse(133+posX,275+posY,25,25);

  stroke(#000000);             //mouth
  noFill();
  arc(220+posX, 280+posY, 40, 50, 0,PI);

  fill(#FFF152);               //Pucca
  textSize(80);
  textAlign(RIGHT);
  text("PUCCA", 360+posX, 420+posY);

  strokeWeight(2);             //paper
  line(x2,y,x,y);
  line(x2+5,y+5,x+5,y+5);
  line(x2+10,y+10,x+10,y+10);
  line(x2+15,y+15,x+15,y+15);
  line(x2+20,y+20,x+20,y+20);

  line(x,y2,x,y);
  line(x+5,y2+5,x+5,y+5);
  line(x+10,y2+10,x+10,y+10);
  line(x+15,y2+15,x+15,y+15);
  line(x+20,y2+20,x+20,y+20);

  line(posX,450+posY,25+posX,475+posY);
  line(450+posX,posY,475+posX,25+posY);
}

Lab 2 : Function Music

 
 int posX=0;
 int posY=0;

void setup(){
  size(600,600);
}

void draw(){
 background(0);
 //int posX=posX+2;
 //int posY=posY+2;

 draw_linkin(posX,posY);
}

void keyPressed(){
  int dx = 5;
  posX = (posX + dx) % width;
 
  int dy = 5;
  posY = (posY + dy) % height;
}

void draw_linkin(int posX,int posY){
  fill(#ffffff);
  ellipse(240+posX,240+posY,450,450);        //inside circle
  fill(#000000);
  ellipse(240+posX,240+posY,400,400);     

  fill(#ffffff);
  triangle(240+posX,70+posY,90+posX,340+posY,390+posX,340+posY);     //triangle
  fill(#000000);
  triangle(240+posX,120+posY,130+posX,320+posY,350+posX,320+posY);   

  strokeWeight(30);                //black line
  stroke(#000000);
  line(260+posX,posY-10,114+posX,260+posY);
  line(287+posX,70+posY,183+posX,260+posY);
  line(295+posX,70+posY,185+posX,260+posY);
  line(200+posX,360+posY,148+posX,450+posY);
  
  stroke(#ffffff);                 //white line                                                   
  line(275+posX,165+posY,138+posX,408+posY);

  strokeWeight(5);                 //outside circle
  noFill();
  ellipse(240+posX,240+posY,470,470);
}

Lab 2 : Function Movie

 
void setup() {
  size(600,600);
}
void draw(){
 background(#FFEC85);
 //int posX=posX+2;
 //int posY=posY+2;

 draw_bear(mouseX,50);
 draw_bear(mouseX,400);

}


void draw_bear(int X,int Y){
  int x=30;
  int x2=270;
  int y=170;

  strokeWeight(6);
  stroke(#624403);

  fill(#E09A2C);                        //outside ear
  ellipse(X,Y,130,130);
  ellipse(290+X,Y,130,130);

  fill(#FFE63D);                        //inside ear
  ellipse(-10+X,20+Y,80,80);
  ellipse(300+X,20+Y,80,80);

  fill(#E09A2C);                        //face
  ellipse(150+X,120+Y,340,280);

  fill(#624403);                        //eye
  ellipse(60+X,130+Y,20,30);
  ellipse(235+X,130+Y,20,30);

  noStroke();                           //nose
  fill(#ffffff);
  ellipse(150+X,175+Y,110,80);
  fill(#624403);
  ellipse(150+X,160+Y,25,20);

  noFill();                                //mouth
  stroke(#624403);
  arc(178+X,170+Y,55,55,HALF_PI,PI);
  arc(122+X,170+Y,55,55,0,HALF_PI);

  stroke(#FA4A46);                 //left cheek
  line(x-15+X,y+Y,x-25+X,y+20+Y);
  line(x+X,y+Y,x-10+X,y+20+Y);
  line(x+15+X,y+Y,x+5+X,y+20+Y);

  line(x2-15+X,y+Y,x2-25+X,y+20+Y);     //right cheek
  line(x2+X,y+Y,x2-10+X,y+20+Y);
  line(x2+15+X,y+Y,x2+5+X,y+20+Y);
}

Lab 2 : Function BMI

 
void setup(){
  size(300,200);
 
  float myWeight=50;
  float myHeight=160;

  float myBMI=BMI(myHeight,myWeight);
 
  textAlign(CENTER);              //text
  textSize(20);
  fill(#ffffff);
  text("Height = "+myHeight,150,50);
  text("Weight = "+myWeight,150,100);
  textAlign(LEFT);
  text("Body Mass Index = "+myBMI,20,150);
 
}

float BMI(float myHeight,float myWeight){
  float Height_M=myHeight/100;
  float myBMI=myWeight/(Height_M*Height_M);
  return myBMI;
}