Tuesday, 25 November 2014

Lab 5 load table balloon

class Balloon {
  float x;
  float y;
  float r;

  Balloon(int x, int y, int r) {

    this.x = x;
    this.y = y;
    this.r = r;
  }
  void display() {

    line(this.x, this.y, this.x, this.y+this.r);
    ellipse(this.x, this.y, this.r, this.r);
  }
  void move_up() {
    this.y = this.y-3;
    if (this.y < 0) {
      this.y = height;
    }
  }
  void move_right() {
    this.x = this.x+1.5;
    if (this.x > width) {
      this.x = 0;
    }
  }
  void fill_c() {
    fill(this.x+this.y-445);
  }
}


Balloon[] b;
Table table;
void setup() {
  size(500, 500);
  table = loadTable("data/new.csv", "header");
  b = new Balloon[table.getRowCount()];
  int index = 0;
  for (TableRow row : table.rows ()) {
    int pos_x  = row.getInt("positionx");
    int pos_y  = row.getInt("positiony");
    int radius  = row.getInt("radius");
    b[index] = new Balloon(pos_x,pos_y,radius);
    index++;
  }
}

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

Lab 5 save table balloon

Table table;

void setup() {

  table = new Table();

  table.addColumn("positiony", Table.INT);
  table.addColumn("positionx", Table.INT);
  table.addColumn("radius", Table.INT);

TableRow[] Row = new TableRow[2];

  Row[0] = table.addRow();
  Row[0].setInt("positionx", 300);
  Row[0].setInt("positiony",300);
  Row[0].setInt("radius", 60);

  Row[1] = table.addRow();
  Row[1].setInt("positionx",200);
  Row[1].setInt("positiony",100);
  Row[1].setInt("radius", 80);
  saveTable(table, "data/new.csv");
}

Sunday, 23 November 2014

Lab 5 cont class Queue New

class Data {
  String name;
  int priority;
  Data(String name, int priority) {
    this.name = name;
    this.priority = priority;
  }
  String toString() {
    String print = "Name#"+name+" Priority#"+priority;
    return print;
  }
}
class Queue {
  Data[] wait;
  int limit;
  Queue(int limit) {
    this.limit = limit;
    this.wait = new Data[limit];
  }
  void show_queue() {
    for (int i = 0; i < this.wait.length; i++) {
      if (this.wait[i] != null) {
        println(this.wait[i]);
      }
    }
  }
  void add(Data in) {
    for (int i = 0; i < this.wait.length; i++) {
      if ( this.wait[i] == null ) {
        this.wait[i] = in;
        i = this.wait.length;
      }
    }
  }
  void remove() {
    int high = 0;
    for (int i = 0; i < this.wait.length; i++) {
      if (this.wait[i] != null) {
        if (this.wait[high].priority < this.wait[i].priority) {
          high = i;
        }
      }
    }
    this.wait[high] = null;
    Data[] copy = new Data[this.limit];
    for (int i = 0; i < copy.length; i++) {
      copy[i] = this.wait[i];
      this.wait[i] = null;
    }
    int count = 0;
    for (int i = 0; i < copy.length; i++) {
    if (copy[i] != null) {
        this.wait[count] = copy[i];
        count++;
      }
    }
  }
}
Data[] human = new Data[322];
Queue hospital = new Queue(200);//max --> 2147483647
void setup() {
  human[0] = new Data("Somsak", 30);
  human[1] = new Data("Prasert", 30);
  hospital.add(human[0]);
  hospital.add(human[1]);
  //hospital.remove();
  //hospital.remove();
  hospital.show_queue();
}

Tuesday, 18 November 2014

Lab 5 cont class Queue

class Data {
  String name;
  int priority;
  Data(String name, int priority) {
    this.name = name;
    this.priority = priority;
  }
  String toString() {
    String print = "name#"+name+" priority#"+priority;
    return print;
  }
}
class Queue {
  Data[] wait = new Data[1];
  int limit;
  Queue(int limit) {
    this.limit = limit;
    this.wait = new Data[limit];
  }
  void show_queue() {
    for (int i = 0; i < wait.length; i++) {
      if (wait[i] != null) {
        println(wait[i]);
      }
    }
  }
  void add(Data in) {
    for (int i = 0; i < wait.length; i++) {
      if ( wait[i] == null ) {
        wait[i] = in;
        i = wait.length;
      }
    }
  }
  void remove() {
    int high = 0;
    for (int i = 0; i < wait.length; i++) {
      if (wait[i] != null) {
        if (wait[high].priority < wait[i].priority) {
          high = i;
        }
      }
    }
    wait[high] = null;
  }
}
Data human;
Data human2;
Queue hos = new Queue(50);
void setup() {
  human = new Data("Somsak", 300);
  human2 = new Data("Prasert", 60);
  hos.add(human);
  hos.add(human2);
  hos.remove();
  hos.show_queue();
}

New Matrix

class Matrix {
  int[][] mat;
  int error;
  Matrix(int[][] mat) {
    this.mat = mat;
    this.error = 0;
  }
  Matrix() {
    this.error = 1;
  }
  String toString() {
    if( this.error == 0){
    String print = "Matrix";
    for (int i = 0; i < this.mat.length; i++) {
      print = print+" Row"+i+"#";
      for (int j = 0; j < this.mat[i].length; j++) {
        print = print+" "+this.mat[i][j];
      }
    }
    return print;
  }
  else{
    String print = "ERROR";
    return print;
  }
  }
  Matrix add(Matrix in) {
      if (this.mat.length != in.mat.length) {
      Matrix sum = new Matrix();
      return sum;
    } else {
      int[][] result = new int[this.mat.length][this.mat[0].length];
      for (int i = 0; i < this.mat.length; i++) {
        for (int j = 0; j < this.mat[i].length; j++) {
          result [i][j] = this.mat[i][j]+in.mat[i][j];
        }
      }
      Matrix sum = new Matrix(result);
      return sum;
    }
  }
  Matrix minus(Matrix in) {
      if (this.mat.length != in.mat.length) {
      Matrix sum = new Matrix();
      return sum;
    } else {
      int[][] result = new int[this.mat.length][this.mat[0].length];
      for (int i = 0; i < this.mat.length; i++) {
        for (int j = 0; j < this.mat[i].length; j++) {
          result [i][j] = this.mat[i][j]-in.mat[i][j];
        }
      }
      Matrix sum = new Matrix(result);
      return sum;
    }
  }
}
int[][] math = {
  {
    5, 5, 5
  }
  , {
    4, 4, 4
  }
  , {
    2, 2, 2
  }
};
int[][] math2 = {
  {
    5, 5, 5
  }
  , {
    4, 4, 4
  }
  , {
    2, 2, 2
  }
};
Matrix m;
Matrix m2;
void setup() {
  m = new Matrix(math);
  m2 = new Matrix(math2);
  println(m.add(m2));
  println(m.minus(m2));
}

Monday, 17 November 2014

New Complex

class Complex{
 float real_num;
 float imaginary_num;
Complex(float real_num, float imaginary_num){
 this.real_num = real_num;
 this.imaginary_num = imaginary_num;
}
String toString(){
 if(this.imaginary_num >= 0){
    String print = this.real_num+"+"+this.imaginary_num+"i";
    return print;
  }
 else{
    String print = this.real_num+""+this.imaginary_num+"i";
    return print;
  }
}
Complex plus(Complex complex){
 Complex m = new Complex(this.real_num+complex.real_num,this.imaginary_num+complex.imaginary_num);
 return m;
}
Complex minus(Complex complex){
 Complex m = new Complex(this.real_num-complex.real_num,this.imaginary_num-complex.imaginary_num);
 return m;
}
}
Complex a;
Complex b;
void setup(){
  a = new Complex(4,5);
  b = new Complex(2,2);
  println(b.minus(a));
  println(a.plus(b));
}

Tuesday, 11 November 2014

Lab 5 Matrix

class Matrix{
  int main_matrix[][];
  int column;
  Matrix exmatrix;
    Matrix(int main_matrix[][], int column){
     this.main_matrix = main_matrix;
     this.column = column;
    }
    void printm(){
     println("#Matrix#");
     for(int i = 0; i < this.main_matrix.length; i++){
       print("[ ");
       for(int i2 = 0; i2 < this.column; i2 += 1){
         print(this.main_matrix[i][i2]+" ");
         }
      println("]");
     }
     println("########");
    }
    Matrix plus(Matrix in){
      this.exmatrix = in;
      if((this.exmatrix.main_matrix.length == this.main_matrix.length) && (this.exmatrix.column == this.column)){
      int[][] ans = new int[this.main_matrix.length][this.column];
      for(int i = 0; i < this.main_matrix.length; i++){
       for(int i2 = 0; i2 < this.column; i2 += 1){
         ans[i][i2] = (this.main_matrix[i][i2])+(this.exmatrix.main_matrix[i][i2]);
       }
      }
      Matrix m = new Matrix(ans,this.column);
      return m;
      }
      else{
       println(" Row and Column are not the same");
       int[][] ans = new int[this.main_matrix.length][this.column];
      Matrix m = new Matrix(ans,this.column);
      return m;
      }
    }
    Matrix minus(Matrix in){
      this.exmatrix = in;
      if((this.exmatrix.main_matrix.length == this.main_matrix.length) && (this.exmatrix.column == this.column)){
      int[][] ans = new int[this.main_matrix.length][this.column];
      for(int i = 0; i < this.main_matrix.length; i++){
       for(int i2 = 0; i2 < this.column; i2 += 1){
         ans[i][i2] = (this.main_matrix[i][i2])-(this.exmatrix.main_matrix[i][i2]);
       }
      }
      Matrix m = new Matrix(ans,this.column);
      return m;
      }
      else{
       println(" Row and Column are not the same");
       int[][] ans = new int[this.main_matrix.length][this.column];
      Matrix m = new Matrix(ans,this.column);
      return m;
      }
    }
}
int[][] amatrix = {{2,2,4,4},
                   {3,3,4,6},
                   {3,2,2,7}};
int[][] bmatrix = {{4,4,3,4},
                   {3,3,4,6},
                   {3,3,4,6}};
void setup(){
  Matrix a = new Matrix(amatrix, 4);//(2d array, column)
  Matrix b = new Matrix(bmatrix, 4);
  a.printm();
  b.printm();
  a.plus(b).printm();
  a.minus(b).printm();
}

Saturday, 8 November 2014

Lab 5 Complex number

class Complex{
 float real_num;
 float imaginary_num;
 Complex complex;
Complex(float real_num, float imaginary_num){
 this.real_num = real_num;
 this.imaginary_num = imaginary_num;
}
void printcln(){
  if(this.imaginary_num >= 0){
    println(this.real_num+"+"+this.imaginary_num+"i");
  }
 else{
    println(this.real_num+""+this.imaginary_num+"i");
  }
}
void printc(){
 if(this.imaginary_num >= 0){
    print(this.real_num+"+"+this.imaginary_num+"i");
  }
 else{
    print(this.real_num+""+this.imaginary_num+"i");
  }
}
Complex plus(Complex c){
 this.complex = c;
 Complex m = new Complex(this.real_num+this.complex.real_num,this.imaginary_num+this.complex.imaginary_num);
 return m;
}
Complex minus(Complex c){
 this.complex = c;
 Complex m = new Complex(this.real_num-this.complex.real_num,this.imaginary_num-this.complex.imaginary_num);
 return m;
}
}
Complex a;
Complex b;
void setup(){
  a = new Complex(4,5);
  b = new Complex(2,2);
 a.printcln();
 a.plus(b).printcln();
 a.minus(b).printcln();
 b.minus(a).printcln();
}

Wednesday, 5 November 2014

Lab 5 Fraction

class Fraction{
  float top;
  float btm;
Fraction fraction;

Fraction(float top, float btm){
  this.top = top;
  this.btm = btm;
}
void printf(){
  print(this.top+"/"+this.btm);
}
void printfln(){
  println(this.top+"/"+this.btm);
}
float decimal(){
  return this.top/this.btm;
}
void printdecimalln(){
  print(this.top/this.btm);
}
Fraction plus(Fraction f){
  this.fraction = f;
  if(this.btm == this.fraction.btm){
    Fraction m = new Fraction(this.top+this.fraction.top,this.btm);
    return m;
  }
  else{
    Fraction m = new Fraction((this.top*fraction.btm)+(this.fraction.top*this.btm),(this.fraction.btm*this.btm));
    return m;
  }
}
Fraction minus(Fraction f){
  this.fraction = f;
  if(this.btm == this.fraction.btm){
    Fraction m = new Fraction(this.top-this.fraction.top,this.btm);
    return m;
  }
  else{
    Fraction m = new Fraction((this.top*fraction.btm)-(this.fraction.top*this.btm),(this.fraction.btm*this.btm));
    return m;
  }
}
Fraction multiply(Fraction f){
  this.fraction = f;
  Fraction m = new Fraction(this.top*this.fraction.top,this.btm*this.fraction.btm);
  return m;
}
Fraction devide(Fraction f){
  this.fraction = f;
  Fraction m = new Fraction(this.top*this.fraction.btm,this.btm*this.fraction.top);
  return m;
}
}
Fraction a = new Fraction(1,4);
Fraction b = new Fraction(1,5);
void setup(){
  a.printfln();
  b.printfln();
  println(a.decimal());
  println(b.decimal());
 (a.plus(b)).printfln();
 (a.minus(b)).printfln();
 (a.multiply(b)).printfln();
 (a.devide(b)).printfln();
}

Lab 5 2 Balloon

class Balloon{
  float x;
  float y;
  float r;

Balloon(int x, int y, int r){

  this.x = x;
  this.y = y;
  this.r = r;

}
void display(){ 

  line(this.x , this.y, this.x, this.y+this.r);
  ellipse(this.x, this.y, this.r, this.r);

}
void move_up(){
 this.y = this.y-3;
 if(this.y < 0){this.y = height;}
}
void move_right(){
 this.x = this.x+1.5;
 if(this.x > width){this.x = 0;}
}
void fill_c(){
 fill(this.x+this.y-445);
}
}

  this.x =
Balloon b1;
Balloon b2;
void setup(){
  size(500, 500);
  b1 = new Balloon(150, 150, 50);
  b2 = new Balloon(250, 250, 50);
}

void draw(){
  background(255);
  b1.fill_c();
  b1.display();
  b1.move_up();
  b1.move_right();
  b2.fill_c();
  b2.display();
  b2.move_up();
  b2.move_right();
}

Lab 5 1 Balloon

class Balloon{
  float x;
  float y;
  float r;

Balloon(int x, int y, int r){

  this.x = x;
  this.y = y;
  this.r = r;

}
void display(){

  line(this.x , this.y, this.x, this.y+this.r);
  ellipse(this.x, this.y, this.r, this.r);

}
void move_up(){
 this.y = this.y-3;
 if(this.y < 0){this.y = height;}
}
void move_right(){
 this.x = this.x+1.5;
 if(this.x > width){this.x = 0;}
}
void fill_c(){
 fill(this.x+this.y-445);
}
}


Balloon b;
void setup(){
  size(500, 500);
  b = new Balloon(250, 250, 50);
}

void draw(){
  background(255);
  b.fill_c();
  b.display();
  b.move_up();
  b.move_right();
}

Lab 5 Table / Load Table

Table table;

void setup() {

  table = new Table();

  table.addColumn("name", Table.STRING);
  table.addColumn("age", Table.INT);
  table.addColumn("height", Table.FLOAT);
  table.addColumn("weight", Table.FLOAT);

TableRow[] Row = new TableRow[2];

  Row[0] = table.addRow();
  Row[0].setString("name", "Jermaine");
  Row[0].setInt("age", 15);
  Row[0].setFloat("height", 188);
  Row[0].setFloat("weight", 20);

  Row[1] = table.addRow();
  Row[1].setString("name", "Chanon");
  Row[1].setInt("age", 32);
  Row[1].setFloat("height", 188);
  Row[1].setFloat("weight", 120);
  saveTable(table, "data/new.csv");
}
-------------------------------------------------------------

  table = loadTable("data/new.csv", "header");

  println(table.getRowCount() + " total rows in table");

  for (TableRow row : table.rows()) {
    float h  = row.getFloat("height");
    int a  = row.getInt("age");
    float w  = row.getFloat("weight");
    String name = row.getString("name");
  
    println(name + " has a " + w + " kg and  " + h + " cm and age of " + a);
  }
}

Lab 5 cont House Window Door

class House{
 int house_x;
 int house_y;
 int house_w;
 int house_h;
 Door door;
 Window window_left;
 Window window_right;
 int Door_Set;
 int Window_Set;
  House(int house_x, int house_y, int house_w, int house_h){
    this.house_x = house_x;
    this.house_y = house_y;
    this.house_w = house_w;
    this.house_h = house_h;
    this.Window_Set = 0;
    this.Door_Set = 0;
  }
void drawWindow(Window w_l, Window w_r){
  this.window_left = w_l;
  this.window_right = w_r;
  this.Window_Set = 1;
}
void drawDoor(Door d){
  this.door = d;
  this.Door_Set = 1;
}
void display(){
 rect(this.house_x, this.house_y, this.house_w, this.house_h);
 triangle(this.house_x, this.house_y, this.house_x+this.house_w, this.house_y, this.house_x+(this.house_w/2),this.house_y-(this.house_h/2));
 if( this.Door_Set == 1){
 this.door.display();
 }
 if( this.Window_Set == 1){
 this.window_left.display();
 this.window_right.display();
 }
}
void move_left(){
 this.house_x += 1;
  if(this.house_x > width+150){this.house_x = -150;}
 if( this.Door_Set == 1){
 this.door.move_left();
 }
 if( this.Window_Set == 1){
 this.window_left.move_left();
 this.window_right.move_left();
}
}
}

class Door{
 int door_x;
 int door_y;
 int door_w;
 int door_h;

  Door(int door_x, int door_y, int door_w, int door_h){
    this.door_x = door_x;
    this.door_y = door_y;
    this.door_w = door_w;
    this.door_h = door_h;
  }
  Door(){
    this.door_x = 0;
    this.door_y = 0;
    this.door_w = 0;
    this.door_h = 0;
  }
void display(){
 rect(this.door_x, this.door_y, this.door_w, this.door_h);
}
 void move_left(){
   this.door_x +=1;
   if(this.door_x > width+150){ this.door_x = -150;}
 }
}
class Window{
  int window_x;
  int window_y;
  int window_w;
  int window_h;
 Window(int window_x, int window_y, int window_w, int window_h){
   this.window_x = window_x;
   this.window_y = window_y;
   this.window_w = window_w;
   this.window_h = window_h;
 }
 Window(){
   this.window_x = 0;
   this.window_y = 0;
   this.window_w = 0;
   this.window_h = 0;
 }
void display(){
   rect(this.window_x, this.window_y, this.window_w, this.window_h);
 }
 void move_left(){
   this.window_x +=1;
   if(this.window_x > width+150){ this.window_x = -150;}
 }
}
House h;
Door d;
Window window_left;
Window window_right;
Window no_window = new Window(0,0,0,0);
House k;
void setup(){
  size(300, 300);
  h = new House(100,100,100,50);
  k = new House(100,200,100,50);
  d = new Door(130,125,40,25);
  window_left = new Window(110, 110, 10, 10);
  window_right = new Window(180, 110, 10, 10);
  h.drawDoor(d);
  h.drawWindow(window_left, window_right);
  //h.drawWindow(window_left, no_window);
}
void draw(){
 background(255);
 h.display();
 k.display();
 k.move_left();
 h.move_left();
}