วันจันทร์ที่ 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;
}
*/