วันพุธที่ 26 พฤศจิกายน พ.ศ. 2557

Lab 5 - Queue

void setup(){
  Patient[] P = {new Patient("Aruk Pupatee",80,false),new Patient("Tanaphat Kalaya",100,true),
  new Patient("Anuparp Chewakumnuan",20,true),new Patient("Apichit Saisuttivong",100,false)};
  PatientQueue PQ = new PatientQueue(P);
  PQ.showAllPatient();
  Patient P1 = new Patient("Anothai Arunthong",90,false);
  Patient P2 = new Patient("Warunatid Thongsalee",100,false);
  Patient P3 = new Patient("Chayanin Khawsanit",60,true);
  PQ.addPatient(P1);
  PQ.addPatient(P2);
  PQ.addPatient(P3);
  PQ.showAllPatient();
  PQ.removePatient();
  PQ.showAllPatient();
}
class PatientQueue{
  Patient[] HospitalPatient;
  PatientQueue(Patient p){
    this.HospitalPatient = new Patient[1];
    this.HospitalPatient[0] = p;
  }
  PatientQueue(Patient[] p){
    this.HospitalPatient = p;
    this.sortQueue();
  }
  void sortQueue(){
    Patient[] p = new Patient[this.HospitalPatient.length-this.getTotalEmer()];
    int pcount = 0;
    for(int i=0;i<this.HospitalPatient.length;i++){
      if(!this.HospitalPatient[i].getEmer()){
        p[pcount] = this.HospitalPatient[i];
        pcount++;
      }
    }
    for(int i=0;i<this.HospitalPatient.length;i++){
      for(int j=i;j<this.HospitalPatient.length;j++){
        if(!this.HospitalPatient[i].getEmer() && this.HospitalPatient[j].getEmer()){
          Patient temp = this.HospitalPatient[i];
          this.HospitalPatient[i] = this.HospitalPatient[j];
          this.HospitalPatient[j] = temp;
        }
        else {
          if(this.HospitalPatient[i].getEmer() && this.HospitalPatient[j].getEmer()){
            if(this.HospitalPatient[i].getBlood() > this.HospitalPatient[j].getBlood()){
              Patient temp = this.HospitalPatient[i];
              this.HospitalPatient[i] = this.HospitalPatient[j];
              this.HospitalPatient[j] = temp;
            }
          }
        }
      }
    }
    for(int i=(this.HospitalPatient.length-1);i>(this.getTotalEmer()-1);i--){
      HospitalPatient[i] = p[pcount-1];
      pcount--;
    }
  }
  void addPatient(Patient p){
    Patient[] temp = new Patient[this.HospitalPatient.length+1];
    for(int i=0;i<this.HospitalPatient.length;i++){
      temp[i] = this.HospitalPatient[i];
    }
    temp[temp.length-1] = p;
    this.HospitalPatient = temp;
    this.sortQueue();
  }
  void removePatient(){
    Patient[] temp = new Patient[this.HospitalPatient.length-1];
    for(int i=0;i<(this.HospitalPatient.length-1);i++){
      temp[i] = this.HospitalPatient[i+1];
    }
    this.HospitalPatient = temp;
  }
  void showAllPatient(){
    for(int i=0;i<this.HospitalPatient.length;i++){
      print((i+1)+")."+this.HospitalPatient[i].getName());
      print(" Blood : "+this.HospitalPatient[i].getBlood()+"%");
      if(this.HospitalPatient[i].getEmer()){
        println(" <<< EMERGENCY !!");
      }
      else println("");
    }
    println("----------------------------------------------------");
  }
  int getTotalEmer(){
    int count=0;
    for(int i=0;i<this.HospitalPatient.length;i++){
      if(this.HospitalPatient[i].getEmer()){
        count++;
      }
    }
    return count;
  }
}
class Patient{
  String name;
  int blood;
  boolean isEmergency;
  Patient(String n,int b,boolean e){
    this.name = n;
    if(b>100){
      this.blood = 100;
    }
    else this.blood = b;
    this.isEmergency = e;
  }
  String getName(){
    return this.name;
  }
  int getBlood(){
    return this.blood;
  }
  boolean getEmer(){
    return this.isEmergency;
  }
}

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

Lab 5 - class House

void setup(){
  size(500,500);
  background(255);
  House h = new House(100,100,150,150);
  h.display();
}
class House{
  int x,y,w,h;
  Door door;
  Window left_window;
  Window right_window;
  Roof roof;
  House(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.door = new Door(this.x+((this.w*2)/5),this.y+(this.h/2),this.w/5,this.h/2);
    this.left_window = new Window(this.x+(this.w/7),this.y+(this.h/2),this.w/7,this.h/5);
    this.right_window = new Window(this.x+((this.w*5)/7),this.y+(this.h/2),this.w/7,this.h/5);
    this.roof = new Roof(this.x,this.y,this.w,this.h/2);
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
    this.door.display();
    this.left_window.display();
    this.right_window.display();
    this.roof.display();
  }
}
class Door{
  int x,y,w,h;
  Door(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
  }
}
class Window{
  int x,y,w,h;
  Window(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
    line(this.x+(this.w/2),this.y,this.x+(this.w/2),this.y+this.h);
    line(this.x,this.y+(this.h/2),this.x+this.w,this.y+(this.h/2));
  }
}
class Roof{
  int x,y,w,h;
  Roof(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    triangle(this.x,this.y,this.x+(this.w/2),this.y-this.h,this.x+this.w,this.y);
  }
}

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

Lab 5 - class Matrix

void setup(){
  int[][] a = {{-1,2,1},
               {3,-2,4},
               {2,-3,1}};
  int[][] b = {{7,4,-2},
               {-1,-5,1},
               {-3,4,6}};
  Matrix m = new Matrix(a);
  Matrix n = new Matrix(b);
  m.ChangeValueAt(4,2,2);
  m.ShowMatrix();
  m.AddMatrix(n);
  m.ShowMatrix();
  n.SubMatrix(m);
  n.ShowMatrix();
}

class Matrix{
  int[][] matrix;
  int max_row;
  int max_column;
  Matrix(int r,int c){
    matrix = new int[r][c];
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  Matrix(int[][] a){
    this.matrix = a;
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  void ChangeMatrix(int[][] a){
    this.matrix = a;
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  void ChangeValueAt(int value,int r,int c){
    this.matrix[r-1][c-1] = value;
  }
  void ShowMatrix(){
    for(int r=0;r<this.max_row;r++){
      print("|");
      for(int c=0;c<this.max_column;c++){
        if(this.matrix[r][c]>=0){
          print("  "+this.matrix[r][c]+" ");
        }
        else print(" "+this.matrix[r][c]+" ");
      }
      println("|");
    }
    println("");
  }
  void AddMatrix(Matrix x){
    if((this.max_row==x.max_row)&&(this.max_column==x.max_column)){
      for(int r=0;r<this.max_row;r++){
        for(int c=0;c<this.max_column;c++){
          this.matrix[r][c] = this.matrix[r][c] + x.matrix[r][c];
        }
      }
    }
    else println("Can't add matrix");
  }
  void SubMatrix(Matrix x){
    if((this.max_row==x.max_row)&&(this.max_column==x.max_column)){
      for(int r=0;r<this.max_row;r++){
        for(int c=0;c<this.max_column;c++){
          this.matrix[r][c] = this.matrix[r][c] - x.matrix[r][c];
        }
      }
    }
    else println("Can't subtract matrix");
  }
  /*
  int FindDet(){
    int det=0;
    if((this.max_row==this.max_column) ){
      if(this.max_row==2){
        det = (this.matrix[0][0]*this.matrix[1][1])-(this.matrix[1][0]*this.matrix[0][1]);
      }
      else {
        if(this.max_row==3){
          for(int c=0;c<this.matrix[0].length;c++){
            for(int r=c
          }
        }
        else {
          println("Can't find determinant");
        }
      }
    }
    else println("Can't find determinant");
    return det;
  }
  */
}

วันจันทร์ที่ 27 ตุลาคม พ.ศ. 2557

Lab 5 - class Complex Number



void setup(){
  Complex a = new Complex(5,7);
  Complex b = new Complex(2,4);
  Complex z,y;
  println(a.toString());
  z = a.addComplex(b);
  println(z.toString());
  y = b.subtractComplex(a);
  println(y.toString());
}
class Complex{
  int real;
  int imagine;
  Complex(int real,int imagine){
    this.real = real;
    this.imagine = imagine;
  }
  Complex addComplex(Complex x){
    int r,i;
    r = this.real + x.real;
    i = this.imagine + x.imagine;
    Complex c = new Complex(r,i);
    return c;
  }
  Complex subtractComplex(Complex x){
    int r,i;
    r = this.real - x.real;
    i = this.imagine - x.imagine;
    Complex c = new Complex(r,i);
    return c;
  }
  String toString(){
    String s="";
    if(this.real!=0){
      s = s+this.real;
    }
    if(this.imagine!=0){
      if(this.imagine>0){
        s = s+"+";
      }
      s = s+this.imagine+"i";
    }
    return s;
  }
}

วันพุธที่ 22 ตุลาคม พ.ศ. 2557

Lab 5 - class Balloon




int n=5;
Balloon[] B = new Balloon[n];

void setup(){
  size(350,500);
  background(255);
  for(int i=0;i<B.length;i++){
    B[i] = new Balloon((width/(n+1))*(i+1),height+((height/n)*(i+1)),20,color(255,0,0));
  }
}

void draw(){
  background(255);
  for(int i=0;i<B.length;i++){
    B[i].display();
    B[i].moveUp(1);
  }
}

void mousePressed(){
  for(int i=0;i<B.length;i++){
    B[i].reset();
  }
}

class Balloon{
  int x,y,r,c;
  int dx,dy,dr,dc;

  Balloon(int x,int y,int r,color c){
    this.x = x;
    this.y = y;
    this.r = r;
    this.c = c;
    this.dx = x;
    this.dy = y;
    this.dr = r;
    this.dc = c;
  }

  void display(){
    int WBalloon = 2*this.r;
    int HBalloon = 3*this.r;
    fill(this.c);
    ellipse(this.x,this.y,WBalloon,HBalloon);
    line(this.x,this.y+(HBalloon/2),this.x,this.y+(1.2*HBalloon));
  }

  void moveUp(int speed){
    this.y = this.y - speed;
  }

  void reset(){
    this.x = dx;
    this.y = dy;
    this.r = dr;
    this.c = dc;
  }
}

วันพุธที่ 15 ตุลาคม พ.ศ. 2557

Lab 4 - Draw Board Game (Mark Horse)




int MaxRow=8,MaxColumn=8;
int[][] HorseP1 = new int[MaxRow][MaxColumn];
int[][] HorseP2 = new int[MaxRow][MaxColumn];
void setup(){
  size(400,400);
  //MaxColumn = number of vertical line , MaxRow = number of horizontal line
  color black=color(0),white=color(255),red=color(255,0,0),blue=color(5,255,255);
  DrawBoard(MaxRow,MaxColumn,black,white);
  ResetBoard(HorseP1,HorseP2);
  DrawAllHorse(HorseP1,HorseP2,red,blue);
}
void DrawBoard(int R,int C,color Color1,color Color2){
  color FillColor=Color1;
  for(int r=0;r<R;r++){
    for(int c=0;c<C;c++){
      if((c%2)==(r%2)){
        FillColor = Color1;
      }
      else{
        FillColor = Color2;
      }
      fill(FillColor);
      rect(c*(width/C),r*(height/R),width/C,height/R);
    }
  }
}
void ResetBoard(int[][] P1,int[][] P2){
  //Reset All
  for(int r=0;r<MaxRow;r++){
    for(int c=0;c<MaxColumn;c++){
      P1[r][c] = 0;
      P2[r][c] = 0;
    }
  }
  //Add P1
  for(int r=0;r<=1;r++){
    for(int c=0+r;c<P1[r].length;c=c+2){
      P1[r][c] = 1;
    }
  }
  //Add P2
  for(int r=0;r<=1;r++){
    for(int c=0+r;c<P2[r+MaxRow-2].length;c=c+2){
      P2[r+MaxRow-2][c] = 1;
    }
  }
}
void DrawHorse(int x,int y,color c){
  fill(c);
  ellipse(x,y,(width/MaxColumn)-10,(height/MaxRow)-10);
}
void DrawAllHorse(int[][] P1,int[][] P2,color CP1,color CP2){
  for(int r=0;r<MaxRow;r++){
    for(int c=0;c<MaxColumn;c++){
      if(P1[r][c]==1){
        DrawHorse((c*(width/MaxColumn))+(width/(2*MaxColumn)),(r*(height/MaxRow))+(height/(2*MaxRow)),CP1);
      }
      if(P2[r][c]==1){
        DrawHorse((c*(width/MaxColumn))+(width/(2*MaxColumn)),(r*(height/MaxRow))+(height/(2*MaxRow)),CP2);
      }
    }
  }
}

Lab 4 - Calculate Values (2D Arrays)

ตัวอย่างการใช้ Array 2 มิติในการเก็บชั่วโมงในการใช้ Internet ของนักเรียน 3 คน และนำมาคำนวน




void setup(){
  int[][] StdHrsInternetWeek =
  {{3,4,5,6,5,4,3},
  {2,1,4,7,2,1,3},
  {7,8,9,5,10,6,5}};
  PrintStdHrs(StdHrsInternetWeek);
  PrintAvgHrsPerDay(StdHrsInternetWeek);
}
void PrintStdHrs(int[][] A){
  for(int i=0;i<A.length;i++){
    println("Total student"+i+"'s internet hrs = "+SumRow(A[i])+" , Average per day = "+AvgRow(A[i]));
  }
}
int SumRow(int[] a){
  int sum = 0;
  for(int i=0;i<a.length;i++){
    sum = sum + a[i];
  }
  return sum;
}
float AvgRow(int[] a){
  float avg = (float)SumRow(a)/a.length;
  return avg;
}
void PrintAvgHrsPerDay(int[][] A){
  String[] day = {"Mondays","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
  for(int i=0;i<A[0].length;i++){
    println("Average "+day[i]+" = "+AvgColumn(A,i));
  }
}
int SumColumn(int[][] a,int n){
  int sum=0;
  for(int i=0;i<a.length;i++){
    sum = sum + a[i][n];
  }
  return sum;
}
float AvgColumn(int[][] a,int n){
  float avg = (float)SumColumn(a,n)/a.length;
  return avg;
}

วันพุธที่ 1 ตุลาคม พ.ศ. 2557

Count word

void setup(){
  String s = "";
  int count = count_words(s);
  println(count);
  s = "Welcome";
  count = count_words(s);
  println(count);
  s = "   Welcome  to Thailand";
  count = count_words(s);
  println(count);
}

int count_words(String a){
  int words;
  int start=-1,end=-1;
  if(a.length() != 0){
    words=1;
    if(a.charAt(0)==' '){
      for(int i=0;(i<a.length())&&(start<0);i++){
        if((a.charAt(i)==' ')&&(a.charAt(i+1)!=' ')){
          start = i+1;
        }
      }
    }
    else {start = 0;}
    if(a.charAt(a.length()-1)==' '){
      for(int i=a.length()-1;(i>=0)&&(end<0);i--){
        if((a.charAt(i)==' ')&&(a.charAt(i-1)!=' ')){
          end = i-1;
        }
      }
    }
    else {end = a.length();}
    for(int i=start;i<end;i++){
      if((a.charAt(i)==' ')&&(a.charAt(i+1)!=' ')){
          words = words+1;
      }
    }
  }
  else {words = 0;}
  return words;
}

โค้ดข้างล่าง ลองทำอีกวิธี แต่ยังบัคอยู่
/*
int count_words(String a){
  a = trim(a);
  println(a);
  String[] x = split(a,' ');
  int n = x.length;
  return n;
}
*/

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

Assignment 1 Bug Report - ปัญหาเกี่ยวกับ mousePressed

Bug
- ต้องการให้คลิกเปลี่ยนเฟรม แล้วมันกระโดดข้ามไปหลายเฟรม
Why
- เวลาจับเงื่อนไขการคลิกเมาส์ กรณีใช้ mousePressed ในเฟรมใกล้ๆกัน จะเกิดปัญหา เช่น
ต้องการให้คลิกเมาส์แต่ละครั้ง เปลี่ยนจากเฟรม 1 ไป 2 จาก 2 ไป 3 จาก 3 ไป 4
เมื่อใช้ mousePressed() กลายเป็นว่าคลิกแล้วเปลี่ยนจากเฟรม 1 ไป 4
How to fix bug
- เลือกใช้เป็นฟังก์ชัน mousePressed() หรือ mouseClicked() แทน
ไม่เช่นนั้นก็ใส่เงื่อนไขเพิ่มเติม เวลาใช้ mousePressed

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

Assignment 1 Bug Report - ปัญหาเกี่ยวกับ frameRate

Bug
- เวลานำค่า frameRate มาใช้ในการคำนวนเพื่อหน่วงเวลาแล้วมีปัญหา
Why ?
- ตัวแปรที่นำค่า frameRate มาคำนวน มีการ update ไวมากจนใช้ == ไม่ได้
How To Fix
- ใช้เป็น >= , <= แทน

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

Lab 3 - Random Values in an array (with parameter to specify duplicate values are allowed)

โปรแกรมสุ่มค่าให้ Array โดยที่สามารถเลือกได้ว่าค่าที่สุ่มมาสามารถที่จะซ้ำหรือไม่ซ้ำกันได้




void setup(){
  int[] K = new int[5];
  RandomValueInArray(K,5,9,true);
  PrintArray(K);
  RandomValueInArray(K,5,9,true);
  PrintArray(K);
  RandomValueInArray(K,5,9,true);
  PrintArray(K);
  RandomValueInArray(K,5,9,false);
  PrintArray(K);
  RandomValueInArray(K,5,9,false);
  PrintArray(K);
  RandomValueInArray(K,5,9,false);
  PrintArray(K);
}
void RandomValueInArray(int[] a,int min,int max,boolean AllowDuplicate){
  if(AllowDuplicate){
    for(int i=0;i<a.length;i++){
      a[i] = int(random(min,max+0.9999999));
    }
  }
  else{
    a[0] = int(random(min,max+0.9999999));
    for(int i=1;i<a.length;i++){
      a[i] = int(random(min,max+0.9999999));
      CheckDuplicate(a,i,min,max);
    }
  }
}
void PrintArray(int[] a){
  print("K = {"+a[0]);
  for(int i=1;i<a.length;i++){
    print(","+a[i]);
  }
  println("}");
}
void CheckDuplicate(int[] b,int n,int min,int max){
  boolean Duplicate = true;
  while(Duplicate){
    Duplicate = false;                                                    
    for(int i=0;(i<n)&&(!Duplicate);i++){
      if(b[i]==b[n]){
        b[n] = int(random(min,max+0.9999999));
        Duplicate = true;
      }
      else{
        Duplicate = false;
      }
    }
  }
}

Lab 3 - Random values in an array

โปรแกรมสุ่มค่าให้กับ Array




void setup(){
  int[] K = new int[5];
  RandomValueInArray(K,0,10);
  PrintArray(K);
  RandomValueInArray(K,3,8);
  PrintArray(K);
  RandomValueInArray(K,5,9);
  PrintArray(K);
}
void RandomValueInArray(int[] a,int min,int max){
  for(int i=0;i<a.length;i++){
    a[i] = int(random(min,max+0.9999999));
  }
}
void PrintArray(int[] a){
  print("K = {"+a[0]);
  for(int i=1;i<a.length;i++){
    print(","+a[i]);
  }
  println("}");
}

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

Lab 3 - Find the index of a value in an array

โปรแกรมหา Index ของค่าที่ต้องการใน Array



void setup(){
    int[] K = {15,5,6,7,8,9,4,1,2,11,20,3};
    FindIndexValue(K,5);
    FindIndexValue(K,2);
    FindIndexValue(K,8);
    FindIndexValue(K,18);
    FindIndexValue(K,20);
    FindIndexValue(K,0);
    FindIndexValue(K,1);
}
void FindIndexValue(int[] a,int x){
    int Index=-1;
    boolean Found=false;
    for(int i=0;i<a.length&&(!Found);i++){
        if(x==a[i]){
            Index = i;
            Found = true;
        }
    }
    if(Found){
        println(x+" has index = "+Index+" in K");
    }
    else{
        println(x+" is not in K");
    }
}

Lab 3 - Histrogram

 HISTROGRAM !!!




/* Program Draw Histrogram */
    /* Variable */
    int Space=50,MiniSpace=5;
    int MaxValueY=500,ValuePerScaleY=50,RatioY=1;
    int MinValueX=40,MaxValueX=100,ValuePerScaleX=10,RatioX=4;
    /*
    Ratio = Pixel/Value
    Example
    Ratio = 2 is mean value 1 points equal to 2 pixels
    Ratio = 0.5 is mean value 1 points equal to 0.5 pixels
    */

    int n = int((MaxValueX-MinValueX)/ValuePerScaleX);
    //int[] Y = new int[n];
    int[] Y = new int[n];
    int[] StartX = new int[n];

void setup(){
    size(500,600);
    MiniSpace=((MinValueX*RatioX)<MiniSpace)?(MinValueX*RatioX):MiniSpace;
    background(255);
    stroke(0);
    fill(0);
    frameRate(60);
    for(int i=0;i<n;i++){
      Y[i] = int(random(0,MaxValueY));
    }
    for(int i=0;i<n;i++){
      StartX[i] = Space+((MiniSpace + (ValuePerScaleX*i))*RatioX);
    }
}
void draw(){
    background(255);
    stroke(0);
    fill(0);
    int MinValue = FindMin(Y);
    int MaxValue = FindMax(Y);
    float AverageValue = FindAverage(Y);
    DrawAxis(MinValueX,MaxValueX,MaxValueY,ValuePerScaleX,ValuePerScaleY,RatioX,RatioY,Space,MiniSpace);
    for(int i=0;i<n;i++){
      DrawGraph(StartX[i],height-Space-(Y[i]*RatioY),ValuePerScaleX*RatioX,Y[i]*RatioY,MinValue,MaxValue);
    }
    DrawMinMaxAvg(MinValue,MaxValue,AverageValue);
    DrawLineNow(mouseX,mouseY);
}
void mouseDragged(){
    for(int i=0;i<n;i++){
      if((StartX[i]<mouseX)&&(mouseX<(StartX[i]+(ValuePerScaleX*RatioX)))){
        if((height-mouseY)>(Space+(MaxValueY*RatioY))){
          Y[i] = MaxValueY;
        }
        else{
          if((height-mouseY)<Space){
            Y[i] = 0;
          }
          else{
            Y[i] = height-mouseY-Space;
          }
        }
      }
    }
}
void mousePressed(){
  for(int i=0;i<n;i++){
      if((StartX[i]<mouseX)&&(mouseX<(StartX[i]+(ValuePerScaleX*RatioX)))){
        if((height-mouseY)>(Space+(MaxValueY*RatioY))){
          Y[i] = MaxValueY;
        }
        else{
          if((height-mouseY)<Space){
            Y[i] = 0;
          }
          else{
            Y[i] = height-mouseY-Space;
          }
        }
      }
    }
}
void DrawAxis(int MinX,int MaxX,int MaxY,int SX,int SY,int RX,int RY,int Sp,int MSp){
    int GapTextX1=5,GapTextY1=5,GapTextX2=0,GapTextY2=20;
    //1 mean Y axis , 2 mean X axis
    stroke(0);
    noFill();
    rect(Sp,height-Sp-(MaxY*RY),MSp+((MaxX-MinX)*RX)+Sp,MaxY*RY);
    //draw scale Y
    stroke(150);
    fill(0);
    textAlign(RIGHT);
    text(0,Sp-GapTextX1,height-Sp+GapTextY1);
    text(MaxY,Sp-GapTextX1,height-Sp-(MaxY*RY)+GapTextY1);
    for(int i=1;(SY*i)<MaxY;i++){
        line(Sp,height-Sp-(SY*RY*i),Sp+MSp+((MaxX-MinX)*RX)+Sp,height-Sp-(SY*RY*i));
        text(SY*i,Sp-GapTextX1,height-Sp-(SY*RY*i)+GapTextY1);
    }
    //draw scale X
    textAlign(CENTER);
    stroke(0);
    if(MSp > 0){
        line(Sp+(MSp*RX),height-Sp-5,Sp+(MSp*RX),height-Sp+5);
        text(MinX,Sp+(MSp*RX)-GapTextX2,height-Sp+GapTextY2);
    }
    line(Sp+(MSp*RX)+((MaxX-MinX)*RX),height-Sp-5,Sp+(MSp*RX)+((MaxX-MinX)*RX),height-Sp+5);
    text(MaxX,Sp+(MSp*RX)+((MaxX-MinX)*RX)-GapTextX2,height-Sp+GapTextY2);
    for(int i=1;(SX*i)<(MaxX-MinX);i++){
        line(Sp+(MSp*RX)+(SX*RX*i),height-Sp-5,Sp+(MSp*RX)+(SX*RX*i),height-Sp+5);
        text(MinX+(SX*i),Sp+(MSp*RX)+(SX*RX*i)-GapTextX2,height-Sp+GapTextY2);
    }
}
void DrawGraph(int X,int Y,int W,int H,int min,int max){
  color C;
  if((H==min)&&(min!=max)){
    C = color(122,255,127);
  }
  else{
    if((H==max)&&(min!=max)){
      C = color(255,0,0);
    }
    else{
      C = color(255,255,0);
    }
  }
  fill(C);
  rect(X,Y,W,H);
}
void DrawLineNow(int x,int y){
  stroke(0);
  fill(0);
  int LLine=10,LGap=5;
  //assign x
  if(x<Space){
    x=Space;
  }
  else{
    if(x>(Space+(MiniSpace+((MaxValueX-MinValueX)*RatioX))+Space)){
      x=Space+(MiniSpace+((MaxValueX-MinValueX)*RatioX))+Space;
    }
  }
  //assign y
  if((height-y)<Space){
    y=height-Space;
  }
  else{
    if((height-y)>(Space+(MaxValueY*RatioY))){
      y=height-(Space+(MaxValueY*RatioY));
    }
  }
  //draw line x
  for(int i=0;(Space+(LLine*i)+(LGap*i))<x;i++){
    if((Space+(LLine*(i+1))+(LGap*i))<x){
      line(Space+(LLine*i)+(LGap*i),y,Space+(LLine*(i+1))+(LGap*i),y);
    }
    else{
      line(Space+(LLine*i)+(LGap*i),y,x,y);
    }
  }
  //draw line y
  for(int i=0;(Space+(LLine*i)+(LGap*i))<(height-y);i++){
    if((Space+(LLine*(i+1))+(LGap*i))<(height-y)){
      line(x,height-Space-(LLine*i)-(LGap*i),x,height-Space-(LLine*(i+1))-(LGap*i));
    }
    else{
      line(x,height-Space-(LLine*i)-(LGap*i),x,y);
    }
  }
  text((height-y-Space)/RatioY,x,y);
}
void DrawMinMaxAvg(int min,int max ,float avg){
  textAlign(LEFT);
  //draw min
  fill(122,255,127);
  text("Min = "+min,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY));
  //draw max
  fill(255,0,0);
  text("Max = "+max,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY)+15);
  //draw average
  stroke(0,0,255);
  fill(0,0,255);
  line(Space,height-Space-(avg*RatioY),Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space,height-Space-(avg*RatioY));
  text("Average = "+avg,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY)+30);
}
int FindMin(int[] a){
  int min = a[0];
  for(int i=0;i<a.length;i++){
    min = (min<a[i])? min:a[i];
  }
  return min;
}
int FindMax(int[] a){
  int max = a[0];
  for(int i=0;i<a.length;i++){
    max = (max>a[i])? max:a[i];
  }
  return max;
}
float FindAverage(int[] a){
  int sum = 0;
  for(int i=0;i<a.length;i++){
    sum = sum + a[i];
  }
  float avg = sum/a.length;
  return avg;
}

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

Lab 3 - Balloon

 BALLOON UNLIMIT !!!




void setup(){
  size(600,500);
  background(255);
  frameRate(30);
}
int MaxWave = 50;
float[] BalloonX1 = new float[MaxWave];
float[] BalloonY1 = new float[MaxWave];
float[] BalloonR1 = new float[MaxWave];
color[] BalloonC1 = new color[MaxWave];
int CountBalloon1 = -1;
float[] BalloonX2 = new float[MaxWave];
float[] BalloonY2 = new float[MaxWave];
float[] BalloonR2 = new float[MaxWave];
color[] BalloonC2 = new color[MaxWave];
int CountBalloon2 = -1;
int Wave = 1;
int MinR = 15,MaxR = 45;
void draw(){
  background(255);
  //setup balloon
  if((frameCount>=frameRate)&&(Wave==1)){
    frameCount = 0;
    SetupBalloon1(CountBalloon1);
    CountBalloon1 = CountBalloon1 +1;
  }
  if((frameCount>=frameRate)&&(Wave==2)){
    frameCount = 0;
    SetupBalloon2(CountBalloon2);
    CountBalloon2 = CountBalloon2 +1;
  }
  //draw balloon
  for(int i=0;i<CountBalloon1;i++){
    DrawBalloon(BalloonX1[i],BalloonY1[i],BalloonR1[i],BalloonC1[i]);
    BalloonY1[i] = BalloonY1[i]-1;
  }
  for(int i=0;i<CountBalloon2;i++){
    DrawBalloon(BalloonX2[i],BalloonY2[i],BalloonR2[i],BalloonC2[i]);
    BalloonY2[i] = BalloonY2[i]-1;
  }
  //new wave
  if((CountBalloon1>=MaxWave)&&(Wave==1)){
    CountBalloon2=-1;
    Reset2();
    Wave = 2;
  }
  if((CountBalloon2>=MaxWave)&&(Wave==2)){
    CountBalloon1=-1;
    Reset1();
    Wave = 1;
  }
}

void DrawBalloon(float X,float Y,float R,color C){
  fill(C);
  ellipse(X,Y,2.5*R,3*R);
  line(X,Y+(1.5*R),X,Y+(4.5*R));
}

void SetupBalloon1(int Count){
  if(Count>=0){
    BalloonR1[Count] = random(MinR,MaxR);
    BalloonC1[Count] = color(random(0,255),random(0,255),random(0,255));
    BalloonX1[Count] = random(0,width);
    BalloonY1[Count] = height+(2*BalloonR1[Count]);
  }
}

void SetupBalloon2(int Count){
  if(Count>=0){
    BalloonR2[Count] = random(MinR,MaxR);
    BalloonC2[Count] = color(random(0,255),random(0,255),random(0,255));
    BalloonX2[Count] = random(0,width);
    BalloonY2[Count] = height+(2*BalloonR2[Count]);
  }
}
void mouseClicked() {
  background(255);
  Reset1();
  Reset2();
  Wave = 1;
}
void Reset1(){
    for(int i=0;i<MaxWave;i++){
    BalloonR1[i] = 0;
    BalloonC1[i] = 0;
    BalloonX1[i] = 0;
    BalloonY1[i] = height+MaxR;
    }
    CountBalloon1 = -1;

    frameCount = 0;
}
void Reset2(){
    for(int i=0;i<MaxWave;i++){
    BalloonR2[i] = 0;
    BalloonC2[i] = 0;
    BalloonX2[i] = 0;
    BalloonY2[i] = height+MaxR;
    }
    CountBalloon2 = -1;

    frameCount = 0;
}

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

Calculate Income Tax

โปรแกรมคำนวนภาษี !!



void setup(){
    //initialization
    size(300,100);
    background(255);
    //variable
    int income;
    //assign variable
    income = 800001;
    //calculate tax
    println("Tax = "+CalTax(income)+" Baht");
    fill(0);
    textSize(25);
    text("Tax = "+CalTax(income)+" Baht",20,40);
}
void CalTax(i){
    int tax;
    if(i>800000){
        tax = (25/100)*i;
        }else{
            if(i>500000){
                tax = (20/100)*i;
                }else{
                    if(i>300000){
                        tax = (15/100)*i;
                        }else{
                            if(i>150000){
                                tax = (10/100)*i;
                                }else{
                                    tax = 0;
                                    }}}}
    return tax;
}