Bug
- ต้องการให้คลิกเปลี่ยนเฟรม แล้วมันกระโดดข้ามไปหลายเฟรม
Why
- เวลาจับเงื่อนไขการคลิกเมาส์ กรณีใช้ mousePressed ในเฟรมใกล้ๆกัน จะเกิดปัญหา เช่น
ต้องการให้คลิกเมาส์แต่ละครั้ง เปลี่ยนจากเฟรม 1 ไป 2 จาก 2 ไป 3 จาก 3 ไป 4
เมื่อใช้ mousePressed() กลายเป็นว่าคลิกแล้วเปลี่ยนจากเฟรม 1 ไป 4
How to fix bug
- เลือกใช้เป็นฟังก์ชัน mousePressed() หรือ mouseClicked() แทน
ไม่เช่นนั้นก็ใส่เงื่อนไขเพิ่มเติม เวลาใช้ mousePressed
วันอาทิตย์ที่ 28 กันยายน พ.ศ. 2557
วันเสาร์ที่ 27 กันยายน พ.ศ. 2557
Assignment 1 Bug Report - ปัญหาเกี่ยวกับ frameRate
Bug
- เวลานำค่า frameRate มาใช้ในการคำนวนเพื่อหน่วงเวลาแล้วมีปัญหา
Why ?
- ตัวแปรที่นำค่า frameRate มาคำนวน มีการ update ไวมากจนใช้ == ไม่ได้
How To Fix
- ใช้เป็น >= , <= แทน
- เวลานำค่า 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;
}
}
}
}
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("}");
}
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;
}
/* 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;
}
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;
}
Calculate Grade
โปรแกรมคำนวนเกรด !!
void setup(){
//initialization
size(300,100);
background(255);
//variable
int point;
//assign variable
point = 80;
string grade;
//calculate grade
if(point>100){
grade = "YOU ARE CHEAT !!";
}
else{
if(point>=80){
grade = "Grade A";
}
else{
if(point>=70){
grade = "Grade A";
}
else{
if(point>=60){
grade = "Grade A";
}
else{
if(point>=50){
grade = "Grade A";
}
else{
if(point>=0){
grade = "Grade A";
}
else{
grade = "YOU ARE FOOL !!!";
}}}}}}
println(grade);
fill(0);
textSize(30);
text(grade,20,70);
}
วันอาทิตย์ที่ 7 กันยายน พ.ศ. 2557
Show Quadrant
เบื่อจริงๆ กับ Processing
ที่เวลาวาดแล้วเลื่อนขึ้นด้านบนเป็น -Y แทนที่จะเป็น +Y
ก็เลยวาดแกนใหม่ซะเองเลย
ที่เวลาวาดแล้วเลื่อนขึ้นด้านบนเป็น -Y แทนที่จะเป็น +Y
ก็เลยวาดแกนใหม่ซะเองเลย
void setup(){
size(500,500);
background(255);
stroke(0);
strokeWeight(3);
noFill();
translate(width/2,height/2);
frameRate(80);
}
void draw(){
translate(width/2,height/2);
background(255);
float RealMouseX = mouseX-(width/2);
float RealMouseY = mouseY-(height/2);
DrawAxis();
DrawPoint(RealMouseX,RealMouseY);
ShowQuadrant(RealMouseX,RealMouseY);
ShowCoordinate(RealMouseX,RealMouseY);
}
void DrawAxis(){
fill(0);
//draw X axis
line(-(width/2),0,width/2,0);
//draw left arrow
line(-(width/2),0,-(width/2)+20,-10);
line(-(width/2),0,-(width/2)+20,10);
text("-X",-(width/2)+5,-15);
//draw right arrow
line(width/2,0,(width/2)-20,-10);
line(width/2,0,(width/2)-20,10);
text("+X",(width/2)-20,-15);
//draw Y axis
line(0,-(height/2),0,height/2);
//draw top arrow
line(0,-(height/2),-10,-(height/2)+20);
line(0,-(height/2),10,-(height/2)+20);
text("+Y",10,-(height/2)+15);
//draw bottom arrow
line(0,height/2,-10,(height/2)-20);
line(0,height/2,10,(height/2)-20);
text("-Y",10,(height/2)-5);
noFill();
}
void DrawPoint(X,Y){
noStroke();
fill(255,0,0);
ellipse(X,Y,5,5);
stroke(0);
}
void ShowQuadrant(X,Y){
int TSize = 40;
textSize(TSize);
fill(0,0,255);
//check Q1
if((X>0)&&(Y<0)){
text("Q1",width/4-(TSize/2),-(height/4)+(TSize/2));
}
//check Q2
if((X<0)&&(Y<0)){
text("Q2",-(width/4)-(TSize/2),-(height/4)+(TSize/2));
}
//check Q3
if((X<0)&&(Y>0)){
text("Q3",-(width/4)-(TSize/2),height/4+(TSize/2));
}
//check Q4
if((X>0)&&(Y>0)){
text("Q4",width/4-(TSize/2),height/4+(TSize/2));
}
}
void ShowCoordinate(X,Y){
float CalX = X;
float CalY = -Y;
fill(255,0,0);
textSize(10);
text("("+CalX+","+CalY+")",X+5,Y-5);
}
สมัครสมาชิก:
บทความ (Atom)