วันอาทิตย์ที่ 16 พฤศจิกายน พ.ศ. 2557
A2 Link
A2 Link : https://bitbucket.org/g5company/assignment_2/src/3ae10ff2e792d599df03d0db26d73ec2b9b632c4/A2.java?at=default
วันอาทิตย์ที่ 2 พฤศจิกายน พ.ศ. 2557
Lab 5[Continue] : Task#2 - Queue[fixed]
/*
>>>Basic Array Queue<<<
Mod. able Variable List
1. maxQ = Max queue size
Method List
1. come(String name,int priority) ==> Add a new case with priority to the queue
2. come(String name) ==> Add a new case without priority to the queue
3. go() ==> remove first case in the queue & shift others instead
*/
class Queue
{
Case[] queue;
int maxQ;
Queue(int maxQ)
{
this.queue=new Case[maxQ];
this.maxQ=maxQ;
reset();
}
void come(String name,int priority)
{
int index;
index=findInsertIndex(priority);
insertCase(name,priority,index);
}
void come(String name)
{
int index;
index=findLastIndex();
insertNormalCase(name,index);
}
int findInsertIndex(int priority)
{
int index=maxQ;
for(int i=maxQ-1;i>=0;i--)
{
if(priority>queue[i].priority)
{
index=i;
}
}
return index;
}
int findLastIndex()
{
int last=maxQ;
for(int i=maxQ-1;i>=0;i--)
{
if(queue[i].priority==0)
{
last=i;
}
}
return last;
}
void insertCase(String name,int priority,int index)
{
for(int i=maxQ-1;i>index;i--)
{
queue[i]=queue[i-1];
}
queue[index]=new Case(name,priority);
}
void insertNormalCase(String name,int index)
{
if(index!=maxQ)
{
for(int i=maxQ-1;i>index;i--)
{
queue[i]=queue[i-1];
}
queue[index]=new Case(name,queue[index-1].priority);
}
}
void go()
{
for(int i=0;i<maxQ-1;i++)
{
queue[i]=queue[i+1];
}
queue[maxQ-1]=new Case("None",0);
}
void reset()
{
for(int i=0;i<this.maxQ;i++)
{
this.queue[i]=new Case("None",0);
}
}
void show()
{
println("Name - Priority");
for(int i=0;i<maxQ;i++)
{
println(this.queue[i]);
}
}
}
class Case
{
String name;
int priority;
Case(String name,int priority)
{
this.name=name;
this.priority=priority;
}
String toString()
{
String string;
string=this.name+" - "+this.priority;
return string;
}
}
void setup()
{
Queue q1=new Queue(5);
q1.come("A",10);
q1.come("B",9);
q1.come("C",8);
q1.come("D",9);
q1.come("E");
q1.go();
q1.go();
q1.show();
}
>>>Basic Array Queue<<<
Mod. able Variable List
1. maxQ = Max queue size
Method List
1. come(String name,int priority) ==> Add a new case with priority to the queue
2. come(String name) ==> Add a new case without priority to the queue
3. go() ==> remove first case in the queue & shift others instead
*/
class Queue
{
Case[] queue;
int maxQ;
Queue(int maxQ)
{
this.queue=new Case[maxQ];
this.maxQ=maxQ;
reset();
}
void come(String name,int priority)
{
int index;
index=findInsertIndex(priority);
insertCase(name,priority,index);
}
void come(String name)
{
int index;
index=findLastIndex();
insertNormalCase(name,index);
}
int findInsertIndex(int priority)
{
int index=maxQ;
for(int i=maxQ-1;i>=0;i--)
{
if(priority>queue[i].priority)
{
index=i;
}
}
return index;
}
int findLastIndex()
{
int last=maxQ;
for(int i=maxQ-1;i>=0;i--)
{
if(queue[i].priority==0)
{
last=i;
}
}
return last;
}
void insertCase(String name,int priority,int index)
{
for(int i=maxQ-1;i>index;i--)
{
queue[i]=queue[i-1];
}
queue[index]=new Case(name,priority);
}
void insertNormalCase(String name,int index)
{
if(index!=maxQ)
{
for(int i=maxQ-1;i>index;i--)
{
queue[i]=queue[i-1];
}
queue[index]=new Case(name,queue[index-1].priority);
}
}
void go()
{
for(int i=0;i<maxQ-1;i++)
{
queue[i]=queue[i+1];
}
queue[maxQ-1]=new Case("None",0);
}
void reset()
{
for(int i=0;i<this.maxQ;i++)
{
this.queue[i]=new Case("None",0);
}
}
void show()
{
println("Name - Priority");
for(int i=0;i<maxQ;i++)
{
println(this.queue[i]);
}
}
}
class Case
{
String name;
int priority;
Case(String name,int priority)
{
this.name=name;
this.priority=priority;
}
String toString()
{
String string;
string=this.name+" - "+this.priority;
return string;
}
}
void setup()
{
Queue q1=new Queue(5);
q1.come("A",10);
q1.come("B",9);
q1.come("C",8);
q1.come("D",9);
q1.come("E");
q1.go();
q1.go();
q1.show();
}
Lab 5[Continue] : Task#1 - Class House
class House
{
int x;
int y;
int size;
Window l;
Window r;
Door door;
House(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size;
this.l=new Window(x-(size/4),y-(size/4),size);
this.r=new Window(x+(size/4),y-(size/4),size);
this.door=new Door(x,y,size);
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y,this.size,this.size);
triangle(this.x-(this.size/2),this.y-(this.size/2),this.x,this.y-(this.size/2)-(this.size/2),this.x+(this.size/2),this.y-(this.size/2));
l.show();
r.show();
door.show();
}
void moveUp()
{
this.y--;
}
void moveDown()
{
this.y++;
}
void moveLeft()
{
this.x--;
}
void moveRight()
{
this.x++;
}
}
class Window
{
int x;
int y;
int size;
Window(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size/4;
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y,this.size,this.size);
line(this.x-(this.size/2),this.y,this.x+(this.size/2),this.y);
line(this.x,this.y-(this.size/2),this.x,this.y+(this.size/2));
}
}
class Door
{
int x;
int y;
int size;
Door(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size;
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y+(this.size/2)-(this.size/10*2),this.size/3.5,this.size/5*2);
ellipse(this.x+(this.size/11),this.y+(this.size/2)-(this.size/10*2),this.size/20,this.size/20);
}
}
void setup()
{
size(500,500);
strokeWeight(3);
}
void draw()
{
background(155);
House a=new House(250,300,200);
a.show();
}
{
int x;
int y;
int size;
Window l;
Window r;
Door door;
House(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size;
this.l=new Window(x-(size/4),y-(size/4),size);
this.r=new Window(x+(size/4),y-(size/4),size);
this.door=new Door(x,y,size);
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y,this.size,this.size);
triangle(this.x-(this.size/2),this.y-(this.size/2),this.x,this.y-(this.size/2)-(this.size/2),this.x+(this.size/2),this.y-(this.size/2));
l.show();
r.show();
door.show();
}
void moveUp()
{
this.y--;
}
void moveDown()
{
this.y++;
}
void moveLeft()
{
this.x--;
}
void moveRight()
{
this.x++;
}
}
class Window
{
int x;
int y;
int size;
Window(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size/4;
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y,this.size,this.size);
line(this.x-(this.size/2),this.y,this.x+(this.size/2),this.y);
line(this.x,this.y-(this.size/2),this.x,this.y+(this.size/2));
}
}
class Door
{
int x;
int y;
int size;
Door(int x,int y,int size)
{
this.x=x;
this.y=y;
this.size=size;
}
void show()
{
rectMode(CENTER);
rect(this.x,this.y+(this.size/2)-(this.size/10*2),this.size/3.5,this.size/5*2);
ellipse(this.x+(this.size/11),this.y+(this.size/2)-(this.size/10*2),this.size/20,this.size/20);
}
}
void setup()
{
size(500,500);
strokeWeight(3);
}
void draw()
{
background(155);
House a=new House(250,300,200);
a.show();
}
Lab 5 : Task#3 - Save Table Balloon
int w=500,h=500,count=0;
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[10];
void setup()
{
for(int i=0;i<a.length;i++)
{
a[i]=new balloon(50,random(-r,w+r),random(-r,h+r));
}
Table table=new Table();
table.addColumn("X");
table.addColumn("Y");
TableRow row[]=new TableRow[a.length];
for(int i=0;i<a.length;i++)
{
row[i]=table.addRow();
row[i].setFloat("X",a[i].px);
row[i].setFloat("Y",a[i].py);
}
saveTable(table,"C:/Users/Thanaphat/Desktop/table.csv");
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[10];
void setup()
{
for(int i=0;i<a.length;i++)
{
a[i]=new balloon(50,random(-r,w+r),random(-r,h+r));
}
Table table=new Table();
table.addColumn("X");
table.addColumn("Y");
TableRow row[]=new TableRow[a.length];
for(int i=0;i<a.length;i++)
{
row[i]=table.addRow();
row[i].setFloat("X",a[i].px);
row[i].setFloat("Y",a[i].py);
}
saveTable(table,"C:/Users/Thanaphat/Desktop/table.csv");
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
Lab 5 : Task#2 - Load Table Balloon
int w=500,h=500,count=0;
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[10];
void setup()
{
Table table = loadTable("C:/Users/Thanaphat/Desktop/table.csv", "header");
int i=0;
for(TableRow row:table.rows())
{
float px=row.getInt("X");
float py=row.getInt("Y");
a[i]=new balloon(50,px,py);
i++;
}
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[10];
void setup()
{
Table table = loadTable("C:/Users/Thanaphat/Desktop/table.csv", "header");
int i=0;
for(TableRow row:table.rows())
{
float px=row.getInt("X");
float py=row.getInt("Y");
a[i]=new balloon(50,px,py);
i++;
}
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
Lab 5 : Task#1 - Class Balloon
int w=500,h=500,count=0;
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[100];
void setup()
{
for(int i=0;i<100;i++)
{
a[i]=new balloon(50,0,height+999);
}
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
void mousePressed()
{
count++;
a[count]=new balloon(50,mouseX,mouseY);
}
float v=1,r=50;
class balloon
{
float r;
float px;
float py;
balloon(float r,float px,float py)
{
this.r=r;
this.px=px;
this.py=py;
}
void moveUp()
{
if(py!=height+999)
{
this.py=this.py-v;
}
if(this.py<0-r)
{
this.py=height+r;
}
}
void display()
{
line(this.px,this.py,this.px,this.py+100);
ellipse(this.px,this.py,r*2,r*2);
}
}
balloon[] a=new balloon[100];
void setup()
{
for(int i=0;i<100;i++)
{
a[i]=new balloon(50,0,height+999);
}
size(w,h);
}
void draw()
{
background(155);
for(int i=0;i<a.length;i++)
{
a[i].display();
a[i].moveUp();
}
}
void mousePressed()
{
count++;
a[count]=new balloon(50,mouseX,mouseY);
}
วันจันทร์ที่ 13 ตุลาคม พ.ศ. 2557
Lab 4 : Task#2 - X-O game
int[][] pos=new int [3][3];
int count=2;
void setup()
{
size(300,300);
strokeWeight(3);
noFill();
}
void draw()
{
background(135);
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
if(pos[x][y]==1)
{ellipse(50+(x*100),50+(y*100),85,85);}
if(pos[x][y]==2)
{draw_x(x,y);}
}
}
draw_grid();
}
void mousePressed()
{
check_pos();
}
void check_pos()
{
int x=-1,y=-1;
for(int i=0;i<3;i++)
{
if(mouseX>i*100&&mouseX<(i*100)+100)
{x=i;}
if(mouseY>i*100&&mouseY<(i*100)+100)
{y=i;}
}
if(count==1&&pos[x][y]!=1&&pos[x][y]!=2)
{
pos[x][y]=1;
if(count==1)
{count=2;}
else if(count==2)
{count=1;}
}
if(count==2&&pos[x][y]!=1&&pos[x][y]!=2)
{
pos[x][y]=2;
if(count==1)
{count=2;}
else if(count==2)
{count=1;}
}
}
void draw_x(int x,int y)
{
line(50+(x*100)-40,50+(y*100)-40,50+(x*100)+40,50+(y*100)+40);
line(50+(x*100)-40,50+(y*100)+40,50+(x*100)+40,50+(y*100)-40);
}
void draw_grid()
{
for(int i=0;i<2;i++)
{
line((i+1)*100,0,(i+1)*100,height);
line(0,(i+1)*100,width,(i+1)*100);
}
}
Lab 4 : Task#1 - 2D Array
Work only on Processing app.
Download Link : https://processing.org/download/
void setup()
{
String[] r1=loadStrings("https://www.dropbox.com/s/7wh06mhx5ri6t2o/New%20Text%20Document%20%281%29.txt?dl=1");
r1=split(r1[0],',');
String[] r2=loadStrings("https://www.dropbox.com/s/hbs648z96jemqqg/New%20Text%20Document%20%282%29.txt?dl=1");
r2=split(r2[0],',');
String[][] table={r1,r2};
print_table(table);
find_and_print_max(r1,r2,table);
find_and_print_min(r1,r2,table);
find_and_print_sum_and_avg(r1,r2,table);
}
void print_table(String[][] table)
{
for (int i=0; i<table.length; i++)
{
for (int j=0; j<table[i].length; j++)
{
print(table[i][j]+" ");
}
println();
}
println();
}
void find_and_print_max(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
}
int m=max(ir2);
int $index=-1;
for(int i=0;i<r2.length;i++)
{
if(m==ir2[i])
{
$index=i;
}
}
println("max : "+r1[$index]+" = "+m);
}
void find_and_print_min(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
}
int m=min(ir2);
int $index=-1;
for(int i=0;i<r2.length;i++)
{
if(m==ir2[i])
{
$index=i;
}
}
println("min : "+r1[$index]+" = "+m);
}
void find_and_print_sum_and_avg(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
float sum=0,avg;
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
sum=sum+ir2[i];
}
avg=sum/(ir2.length);
println("sum = "+sum);
println("avg = "+avg);
}
Download Link : https://processing.org/download/
{
String[] r1=loadStrings("https://www.dropbox.com/s/7wh06mhx5ri6t2o/New%20Text%20Document%20%281%29.txt?dl=1");
r1=split(r1[0],',');
String[] r2=loadStrings("https://www.dropbox.com/s/hbs648z96jemqqg/New%20Text%20Document%20%282%29.txt?dl=1");
r2=split(r2[0],',');
String[][] table={r1,r2};
print_table(table);
find_and_print_max(r1,r2,table);
find_and_print_min(r1,r2,table);
find_and_print_sum_and_avg(r1,r2,table);
}
void print_table(String[][] table)
{
for (int i=0; i<table.length; i++)
{
for (int j=0; j<table[i].length; j++)
{
print(table[i][j]+" ");
}
println();
}
println();
}
void find_and_print_max(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
}
int m=max(ir2);
int $index=-1;
for(int i=0;i<r2.length;i++)
{
if(m==ir2[i])
{
$index=i;
}
}
println("max : "+r1[$index]+" = "+m);
}
void find_and_print_min(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
}
int m=min(ir2);
int $index=-1;
for(int i=0;i<r2.length;i++)
{
if(m==ir2[i])
{
$index=i;
}
}
println("min : "+r1[$index]+" = "+m);
}
void find_and_print_sum_and_avg(String[] r1,String[] r2,String[][] table)
{
int[] ir2=new int[r2.length];
float sum=0,avg;
for(int i=0;i<ir2.length;i++)
{
ir2[i]=int(r2[i]);
sum=sum+ir2[i];
}
avg=sum/(ir2.length);
println("sum = "+sum);
println("avg = "+avg);
}
วันจันทร์ที่ 6 ตุลาคม พ.ศ. 2557
A1 : Link
https://bitbucket.org/a5720098/assignment_1_my_story_like-dislike/src/caae035bf73edb85197f023747a2ea588c7fc3c8/my_story.java
A1 : Bug Report
ปัญหา
1.) มีปัญหาเกี่ยวกับตำแหน่ง - ตำแหน่งไม่สมดุลกันทั้ง 2 ด้าน,ตำแหน่งปุ่มไม่ตรงกับที่เมาส์ต้องกด
2.) การเคลื่อนไหวผิดทิศทาง
เกิดขึ้นได้อย่างไร
1.) ตั้งตำแหน่งโดยใช้ตัวเลขที่ให้ค่าไม่สัมพันธ์กัน
2.) เงื่อนไขในการเคลื่อนไหวผิดพลาด ( "<" สลับกับ ">" )
วิธีการแก้ไข
1.) ใช้ตัวแปลในการเก็บค่าและคำนวณตำแหน่ง
2.) แก้ไขเงื่อนไขให้ถูกต้อง
1.) มีปัญหาเกี่ยวกับตำแหน่ง - ตำแหน่งไม่สมดุลกันทั้ง 2 ด้าน,ตำแหน่งปุ่มไม่ตรงกับที่เมาส์ต้องกด
2.) การเคลื่อนไหวผิดทิศทาง
เกิดขึ้นได้อย่างไร
1.) ตั้งตำแหน่งโดยใช้ตัวเลขที่ให้ค่าไม่สัมพันธ์กัน
2.) เงื่อนไขในการเคลื่อนไหวผิดพลาด ( "<" สลับกับ ">" )
วิธีการแก้ไข
1.) ใช้ตัวแปลในการเก็บค่าและคำนวณตำแหน่ง
2.) แก้ไขเงื่อนไขให้ถูกต้อง
วันจันทร์ที่ 29 กันยายน พ.ศ. 2557
EX 2 : Task#1 - Word Counter
Work only on Processing app.
Download Link : https://processing.org/download/
void setup()
{
String s="Hello World";
int count=count_word(s);
println("Sentence : "+s);
println("Number of words : "+count);
}
int count_word(String s)
{
if(s.length()==0)
{
return 0;
}
int c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' '||s.charAt(i)=='.'||s.charAt(i)=='?')
{
c++;
}
}
if(s.charAt(s.length()-1)!=' '&&s.charAt(s.length()-1)!='.'&&s.charAt(s.length()-1)!='?')
{
c++;
}
return c;
}
Download Link : https://processing.org/download/
void setup()
{
String s="Hello World";
int count=count_word(s);
println("Sentence : "+s);
println("Number of words : "+count);
}
int count_word(String s)
{
if(s.length()==0)
{
return 0;
}
int c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' '||s.charAt(i)=='.'||s.charAt(i)=='?')
{
c++;
}
}
if(s.charAt(s.length()-1)!=' '&&s.charAt(s.length()-1)!='.'&&s.charAt(s.length()-1)!='?')
{
c++;
}
return c;
}
วันอาทิตย์ที่ 28 กันยายน พ.ศ. 2557
Lab 3[continue] : Task#3 - write a function to create random values in an array (with parameter to specify whether duplicate values are allowed)
boolean d=false;
int n=10;
void setup()
{
int[] x=new int[n];
for(int i=0;i<n;i++)
{x[i]=-1;}
for(int i=0;i<n;i++)
{x[i]=random_array(x);}
for(int i=0;i<n;i++)
{println("x["+i+"] = "+x[i]);}
}
int random_array(int[] x)
{
int y;
y=round(random(0,n));
if(!d)
{
for(int i=0;i<n;i++)
{
if(y==x[i])
{
y=random_array(x);
}
}
}
return y;
}
Lab 3[continue] : Task#2 - Write a function to create random values in an array (duplicate values are allowed)
int n=10;
void setup()
{
int[] x=new int[n];
for(int i=0;i<n;i++)
{x[i]=-1;}
for(int i=0;i<n;i++)
{x[i]=random_array(x);}
for(int i=0;i<n;i++)
{println("x["+i+"] = "+x[i]);}
}
int random_array(int[] x)
{
int y;
y=round(random(0,n));
return y;
}
Lab 3[continue] : Task#1 - Find the index of a value in an array
void setup()
{
int[] x={4,8,5};
int value,pos;
value=5;
pos=find_index(x,value);
print_index(x,value,pos);
}
int find_index(int[] x,int value)
{
int pos=-1;
for(int i=0;i<x.length;i++)
{
if(x[i]==value)
{
pos=i;
}
}
return pos;
}
void print_index(int[] x,int value, int pos)
{
print("X = {");
for(int i=0;i<x.length;i++)
{
print(x[i]);
if(i<x.length-1)
{
print(",");
}
else
{
println("}");
}
}
if(pos==-1)
{
print(value+" is not in X[]");
}
else
{
print(value+" has index = "+pos+" in X");
}
}
วันจันทร์ที่ 15 กันยายน พ.ศ. 2557
Lab 3 : Task#2 - Graph (with Min & Max & Avg)
Warning >>> A little text position bug on Processing
int n=8;
float w_box;
float[] y=new float[n];
void setup()
{
size(480,300);
background(0);
w_box=width/n;
for(int i=0;i<n;i++)
{y[i]=0;}
}
void draw()
{
background(0);
draw_graph();
draw_max();
draw_min();
draw_avg();
draw_grid();
}
void mousePressed()
{
for(int i=0;i<n;i++)
{
if(mouseX>i*w_box&&mouseX<(i+1)*w_box)
{
y[i]=height-mouseY;
}
}
}
void mouseDragged()
{
for(int i=0;i<n;i++)
{
if(mouseX>i*w_box&&mouseX<(i+1)*w_box)
{
y[i]=height-mouseY;
}
}
}
void draw_graph()
{
stroke(0,255,0);
textAlign(CENTER);
for(int i=0;i<n;i++)
{
fill(0,255,0,50);
rect(i*w_box,height,w_box,-y[i]);
fill(0,255,0,255);
text(round(y[i]/3),(i*w_box)+(w_box/2),height-5);
}
}
void draw_max()
{
float $max;
$max=max(y);
line(0,height-$max,width,height-$max);
textAlign(RIGHT);
text("max = "+round($max/3),width-2,height-$max+13);
}
void draw_min()
{
float $min;
$min=min(y);
line(0,height-$min,width,height-$min);
textAlign(RIGHT);
text("min = "+round($min/3),width-2,height-$min+13);
}
void draw_avg()
{
float sum=0,avg;
for(int i=0;i<n;i++)
{
sum=sum+y[i];
}
avg=(sum/n);
line(0,height-avg,width,height-avg);
textAlign(RIGHT);
text("avg = "+round(avg/3),width-2,height-avg+13);
}
void draw_grid()
{
stroke(0,255,0,80);
for(int i=height;i>=0;i=i-10)
{
line(0,i,width,i);
}
for(float i=width;i>=0;i=i-w_box)
{
line(i,0,i,height);
}
}
Lab 3 : Task#1 - Balloon
Warning >>> Bug on firefox
int num=100,stop_r_p=0,count=0,click_fix=0;
float[] px=new float[num],py=new float[num];
float a;
void setup()
{
size(500,500);
strokeWeight(3);
for(int i=0;i<num;i++)
{py[i]=height+100;}
}
void draw()
{
background(150);
if(stop_r_p<=num)
{
for(int i=0;i<num;i++)
{px[i]=random(0,width);stop_r_p++;}
}
if(mousePressed&&click_fix==0)
{
click_fix=1;
count=count+1;
if(count==num)
{count=0;}
py[count]=height+100;;
}
for(int i=0;i<count;i++)
{
draw_balloon(px[i],py[i]);
a=0.005*(py[i]+height);
py[i]=py[i]-a;
}
}
void draw_balloon(float px,float py)
{
fill(255,190,210,200);
ellipse(px,py,100,120);
line(px,py+60,px,py+180);
fill(255,190,210);
triangle(px,py+63,px+7,py+75,px-7,py+75);
}
void mouseReleased()
{
click_fix=0;
}
วันจันทร์ที่ 1 กันยายน พ.ศ. 2557
EX 1 : Task#3 - Tax Calculator
byte i;
long income;
double tax,total;
double ft0,ft1,ft2,ft3,ft4,ft5,ft6,t;
void setup()
{
size(500,100);
}
void draw()
{
size(500,100);
background(0);
fill(0);
textSize(15);
fill(20,255,20);
input_income();
print_income();
cal_tax();
print_tax();
}
void input_income()
{
if(i==0)
{
if(keyPressed)
{
if(key == 48) {income = income*10+0;i=1;}
if(key == 49) {income = income*10+1;i=1;}
if(key == 50) {income = income*10+2;i=1;}
if(key == 51) {income = income*10+3;i=1;}
if(key == 52) {income = income*10+4;i=1;}
if(key == 53) {income = income*10+5;i=1;}
if(key == 54) {income = income*10+6;i=1;}
if(key == 55) {income = income*10+7;i=1;}
if(key == 56) {income = income*10+8;i=1;}
if(key == 57) {income = income*10+9;i=1;}
}
}
}
void keyPressed()
{
if(keyCode==BACKSPACE)
{
reset();
}
}
void keyReleased()
{
i=0;
}
void cal_tax()
{
ft0=150000*0.00;
ft1=150000*0.05;
ft2=200000*0.10;
ft3=250000*0.15;
ft4=250000*0.20;
ft5=1000000*0.25;
ft6=2000000*0.30;
if (income>4000000) {t=(income-4000000)*0.35; tax=t+ft0+ft1+ft2+ft3+ft4+ft5+ft6;}
else if (income>2000000) {t=(income-2000000)*0.30; tax=t+ft0+ft1+ft2+ft3+ft4+ft5;}
else if (income>1000000) {t=(income-1000000)*0.25; tax=t+ft0+ft1+ft2+ft3+ft4;}
else if (income >750000) {t=(income-750000) *0.20; tax=t+ft0+ft1+ft2+ft3;}
else if (income >500000) {t=(income-500000) *0.15; tax=t+ft0+ft1+ft2;}
else if (income >300000) {t=(income-300000) *0.15; tax=t+ft0+ft1;}
else if (income >150000) {t=(income-150000) *0.05; tax=t+ft0;}
else {t=(income) *0.00; tax=t;}
}
void print_income()
{
if(income>29000000){text("Error : Income out of range(max 29000000)", 20, 40);}
else{text("Please input your income here(max 29000000) = "+income, 20, 40);}
}
void print_tax()
{
if(income>29000000){text("Tax = N/A", 20, 70);}
else{text("Tax = "+tax, 20, 70);}
}
void reset()
{
income=0;
i=0;
tax=0;
total=0;
ft0=0;
ft1=0;
ft2=0;
ft3=0;
ft4=0;
ft5=0;
ft6=0;
t=0;
}
EX 1 : Task#2 - Quadrant Check
void setup(){
background(255);
size(500,500);
textAlign(CENTER);
}
void draw()
{
background(255);
fill(255);
strokeWeight(8);
rect(0,0,width,height);
strokeWeight(5);
line(0,height/2, width,height/2);
line(width/2,0, width/2, height);
if(mouseX > width/2 && mouseY < height/2)
{fill(150);rect(width/2,0,width/2,height/2);fill(0);text("Quadrant 1", width*3/4,height/4);}
else if(mouseX < width/2 && mouseY < height/2)
{fill(150);rect(0,0,width/2,height/2);fill(0);text("Quadrant 2",width*1/4,height/4);}
else if(mouseX < width/2 && mouseY > height/2)
{fill(150);rect(0,height/2,width/2,height/2);fill(0);text("Quadrant 3",width*1/4,height*3/4);}
else
{fill(150);rect(width/2,height/2,width/2,height/2);fill(0);text("Quadrant 4",width*3/4,height*3/4);}
}
EX 1 : Task#1 - Grade Calculator
int score,i;
String grade;
void setup()
{
size(300,100);
score = 0;
i = 0;
}
void draw()
{
size(300,100);
background(0);
fill(0);
textSize(15);
fill(20,255,20);
input_score();
print_score();
cal_grade();
print_grade();
if(score>=1000){reset();}
}
void input_score()
{
if(i==0)
{
if(keyPressed)
{
if(key == 48) {score = score*10+0;i=1;}
if(key == 49) {score = score*10+1;i=1;}
if(key == 50) {score = score*10+2;i=1;}
if(key == 51) {score = score*10+3;i=1;}
if(key == 52) {score = score*10+4;i=1;}
if(key == 53) {score = score*10+5;i=1;}
if(key == 54) {score = score*10+6;i=1;}
if(key == 55) {score = score*10+7;i=1;}
if(key == 56) {score = score*10+8;i=1;}
if(key == 57) {score = score*10+9;i=1;}
}
}
}
void keyPressed()
{
if(keyCode==BACKSPACE)
{
reset();
}
}
void keyReleased()
{
i=0;
}
void print_score()
{
text("Please input your score here = "+score, 20, 40);
}
void cal_grade()
{
if (score >=80) {grade = "A" ;}
else if (score >= 75 && score < 80) {grade = "B+";}
else if (score >= 70 && score < 75) {grade = "B" ;}
else if (score >= 65 && score < 70) {grade = "C+";}
else if (score >= 60 && score < 65) {grade = "C" ;}
else if (score >= 55 && score < 60) {grade = "D+";}
else if (score >= 50 && score < 55) {grade = "D" ;}
else {grade = "F" ;}
}
void print_grade()
{
if (score > 100) {text("Error : your score is more than 100", 20, 72);}
else {text("Your grade is "+grade, 20, 70);}
}
void reset()
{
score = 0;
i = 0;
}
วันจันทร์ที่ 25 สิงหาคม พ.ศ. 2557
Lab 2 : Task#5 - draw() is called without setup() [Order]
int c;
int c_change,c_range,c_range_low,c_range_high,c_mean;
int count=0;
float px,py;
void setup()
{
c_range_low=0;
c_range_high=255;
c_range=c_range_high-c_range_low;
c_mean=(c_range_low+c_range_high)/2;
c=c_mean;
size(500,500);
frameRate(60);
py=height-30;
noStroke();
}
void draw()
{
background(255-c);
for(int i=0; i<520; i=i+20)
{
draw_circle_py(i,py);
draw_circle_py(i,py-50);
draw_circle_py(i,py-100);
draw_circle_py(i,py-150);
draw_circle_py(i,py-200);
draw_circle_py(i,py-250);
draw_circle_py(i,py-300);
draw_circle_py(i,py-350);
draw_circle_py(i,py-400);
draw_circle_py(i,py-450);
draw_circle_py(i,py-500);
draw_circle_px(i,py);
draw_circle_px(i,py-50);
draw_circle_px(i,py-100);
draw_circle_px(i,py-150);
draw_circle_px(i,py-200);
draw_circle_px(i,py-250);
draw_circle_px(i,py-300);
draw_circle_px(i,py-350);
draw_circle_px(i,py-400);
draw_circle_px(i,py-450);
draw_circle_px(i,py-500);
}
py=py+1;
if(py>=height+20){py=height-30;}
if(count<c_range){change_colour_P();}
if(count>=c_range){change_colour_N();}
if(count<c_range*2){count=count+c_change;}
if(count>=c_range*2){count=0;}
}
void draw_circle_py(int px,float py)
{
float w,h;
float r;
r=15; //set radius
c_change=10; //set colour change per frame
w=r*2;
h=r*2;
fill(c);
ellipse(px,py,w,h);
}
void draw_circle_px(int py,float px)
{
float w,h;
float r;
r=15; //set radius
c_change=10; //set colour change per frame
w=r*2;
h=r*2;
fill(c);
ellipse(px,py,w,h);
}
void change_colour_P()
{
c=c+c_change;
if(c>c_range_high){c=c_range_high;}
}
void change_colour_N()
{
c=c-c_change;
if(c<c_range_low){c=c_range_low;}
}
Lab 2 : Task#4 - draw() is called after setup() [Disorder]
int c;
int c_change,c_range,c_range_low,c_range_high,c_mean;
int count=0;
void setup()
{
c_range_low=55;
c_range_high=200;
c_range=c_range_high-c_range_low;
c_mean=(c_range_low+c_range_high)/2;
c=c_mean;
size(500,500);
background(c_mean);
frameRate(60);
}
void draw()
{
for(int i=0; i<width*height/5000; i=i+1){draw_circle();}
if(count<c_range){change_colour_P();}
if(count>=c_range){change_colour_N();}
if(count<c_range*2){count=count+c_change;}
if(count>=c_range*2){count=0;}
}
void draw_circle()
{
float px,py,w,h;
float r;
r=20; //set radius
c_change=50; //set colour change per frame
w=r*2;
h=r*2;
fill(c,50);
px=random(0,width);
py=random(0,height);
ellipse(px,py,w,h);
}
void change_colour_P()
{
c=c+c_change;
if(c>c_range_high){c=c_range_high;}
}
void change_colour_N()
{
c=c-c_change;
if(c<c_range_low){c=c_range_low;}
}
Lab 2 - Task#3 : Bicycle
int s=400;
void setup() {
size(400, 400);
}
void draw() {
stroke(150);
strokeWeight(1);
background(206,229,241);
for (int i=0; i<width; i=i+10) {
line(0, i, width, i);
}
for (int i=0; i<height; i=i+10) {
line(i, 0, i, height);
}
//floor
fill(0);
if(frameCount>=20)
{if(frameCount<=40){for(int i=0-10;i<=s;i=i+30){rect(i,300,10,100);}}}
if(frameCount>=40)
{for(int i=0-20;i<=s;i=i+30){rect(i,300,10,100);}}
if(frameCount<=20)
{for(int i=0-30;i<=s;i=i+30){rect(i,300,10,100);}}
fill(220,0,0);
if(frameCount>=20)
{if(frameCount<=40){for(int i=0-20;i<=s;i=i+30){rect(i,300,10,100);}}}
if(frameCount>=40)
{for(int i=0-30;i<=s;i=i+30){rect(i,300,10,100);}}
if(frameCount<=20)
{for(int i=0-10;i<=s;i=i+30){rect(i,300,10,100);}}
fill(130,0,0);
if(frameCount>=20)
{if(frameCount<=40){for(int i=0-30;i<=s;i=i+30){rect(i,300,10,100);}}}
if(frameCount>=40)
{for(int i=0-10;i<=s;i=i+30){rect(i,300,10,100);}}
if(frameCount<=20)
{for(int i=0-20;i<=s;i=i+30){rect(i,300,10,100);}}
if(frameCount==60){frameCount=0;}
draw_bike(200, 200);
}
void draw_bike(int x, int y) {
translate(x-100, y+50);
for (int i=0; i<=8; i=i+1) {
noFill();
stroke(255);
strokeWeight(2);
rotate (45);
line(-50, 0, 50, 0);
stroke(0);
strokeWeight(10);
ellipse(0, 0, 100, 100);
}
rotate (-405);
translate(x, 0);
for (int i=0; i<=8; i=i+1) {
noFill();
strokeWeight(2);
stroke(255);
line(-50, 0, 50, 0);
rotate (45);
strokeWeight(10);
stroke(0);
ellipse(0, 0, 100, 100);
}
rotate (-405); //Body
translate(-x/2, -y/4);
stroke(255,0,0);
strokeWeight(10);
line(-100, 50, -50, -50);
line(-25, -50, -50, -50);
line(-60, -25, 50, -25);
line(-60, -25, 0, 50);
line(100, 50, 50, -25);
line(100, 50, 0, 50);
line(55, -35, 0, 50);
line(70, -40, 40, -40);
}
สมัครสมาชิก:
บทความ (Atom)