A graphic Stack
- Karla G
- 28 mar 2015
- 2 Min. de lectura
How does this looks like:
import remixlab.bias.event.*;
import remixlab.bias.event.shortcut.*;
import remixlab.bias.agent.profile.*;
import remixlab.bias.agent.*;
import remixlab.dandelion.agent.*;
import remixlab.proscene.*;
import remixlab.dandelion.core.*;
import remixlab.dandelion.constraint.*;
import remixlab.fpstiming.*;
import remixlab.util.*;
import remixlab.dandelion.geom.*;
import remixlab.bias.core.*;
Scene scene;
int[] a; //array for stack
int STACK_SIZE; // size of array
int stack_top;
int i=0;
int popped=0;
int position;
PFont f; // Declare PFont variable
void setup(){
scene = new Scene(this); // CAMERA
scene.setAxesVisualHint(false); // hide axis
scene.setGridVisualHint(false); // hide grid
size(1000,800,P3D);
f = createFont("Arial",16,true); // Create Font
STACK_SIZE = 8;
a = new int[STACK_SIZE];
stack_top = 0; // The stack is empty
}
void draw(){
background(0);
textFont(f,8);
drawColors(); // The opstions for the user
drawStack(a); //Draw every element that is pushed
drawPopping(); //Draw the element that is popped
fill(255);
text("For popping, \n press number 6 \n in your keyborad",0,-60,0); //Instructions
}
void drawColors(){ //Options of colors
textFont(f,16);
pushMatrix();
fill(255);
text("1", -100, -35,0);
noStroke();
fill(52, 245, 54);
translate(-80,-40,0);
sphere(10);
popMatrix();
pushMatrix();
fill(255);
text("2", -100, -15,0);
noStroke();
fill(0, 76, 255);
translate(-80,-20,0);
sphere(10);
popMatrix();
pushMatrix();
fill(255);
text("3", -100, 5,0);
noStroke();
fill(#FF9933);
translate(-80,0,0);
sphere(10);
popMatrix();
pushMatrix();
fill(255);
text("4", -100, 25,0);
noStroke();
fill(204, 255, 0);
translate(-80,20,0);
sphere(10);
popMatrix();
pushMatrix();
fill(255);
text("5", -100, 45,0);
noStroke();
fill(179, 0, 255);
translate(-80,40,0);
sphere(10);
popMatrix();
}
int push(int number, int a[]){ //push function
if(stack_top < STACK_SIZE){ // Check for overflow
a[stack_top] = number; // Save the element in the array
println(stack_top);
println(a[stack_top]);
stack_top = stack_top + 1;//next position in stack
println("PUSH");
println(stack_top);
return 0; // success
}
else{
text("NOT POSSIBLE",0,10,0);
println("ERROR");
return -1; // error
}
}
void drawStack(int a[]){ //draw the elements inside the array
noFill();
for (int j=0; j<STACK_SIZE;j++){
switch(a[j]){
case 0:
fill(0);
break;
case 1:
fill(52, 245, 54);
break;
case 2:
fill(0, 76, 255);
break;
case 3:
fill(#FF9933);
break;
case 4:
fill(204, 255, 0);
break;
case 5:
fill(179, 0, 255);
break;
default:
break;
}
pushMatrix();
translate(-20,60-(j*20),0);
noStroke();
sphere(10);
fill(0);
popMatrix();
}
}
int pop(int a[]){ // Returns -1 if the stack is empty
if(stack_top > 0){
popped = a[stack_top-1]; //Save the element that will be popped
a[stack_top-1]=0; //Reset the stack top
stack_top = stack_top - 1;
println("POP");
println(popped);
println(stack_top);
return popped;
}
else{
println("ERROR");
text("NOT POSSIBLE",0,10,0);
return -1; // error
}
}
void drawPopping(){ //Draw the element that is popped
switch(popped){
case 0:
fill(0);
break;
case 1:
fill(52, 245, 54);
break;
case 2:
fill(0, 76, 255);
break;
case 3:
fill(#FF9933);
break;
case 4:
fill(204, 255, 0);
break;
case 5:
fill(179, 0, 255);
break;
default:
break;
}
pushMatrix();
translate(40,40,0);
noStroke();
sphere(10);
popMatrix();
}
void keyReleased(){ // Events of the keys being used
println("GotTheKey");
switch(key){
case '1':
push(1,a);
break;
case '2':
push(2,a);
break;
case '3':
push(3,a);
break;
case '4':
push(4,a);
break;
case '5':
push(5,a);
break;
case '6':
println("POP will enter");
pop(a);
break;
}
}
Comments