Tuesday, 16 December 2014

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));
}