A graphic Queue
- Karla G
- 28 mar 2015
- 3 Min. de lectura
My nyan queue.
This queue works in a circular form. Where each time you queue or dequeue and element, the line (or queue) will follow. In other words, if there is a dequeue, the objects in the array will not move, they will stay the same. But the front will change, so it all the array is full until the last position, the back will go to the first element of the queue that was empty. It's circular.
The code:
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[];
int b[];
int queueSize, info, front, back, c, d, t, p;
float x,y;
float R=125;
float centerR=125;
float z=PI/2;
float z1=PI;
float z2=3*PI/2;
float pathR=125;
float pathG=125;
float G=125;
float centerG=125;
float pathB=125;
float B=125;
float centerB=125;
PFont f;
void setup(){
scene= new Scene(this); // camera
scene.setAxesVisualHint(false); // hide axis
scene.setGridVisualHint(false); // hide grid
size(500,500,P3D); // window size
queueSize=6; //SIZE OF QUEUE
a=new int[queueSize];
b=new int[queueSize];
front=0;
back=0;
info=1;
t=0;
f = createFont("Arial",16,true); // Create Font
}
void draw(){
background(pathR,pathG,pathB);
//Changing color varibles
pathR=centerR+R*sin(z);
z=z+.01;
pathG=centerG+G*sin(z1);
z1=z1+.01;
pathB=centerB+B*sin(z2);
z2=z2+.01;
textFont(f,8);
options(); //DRAW THE OPTIONS FOR DRAWING
drawQueue(); //DRAW THE QUEUE
drawDequeued(); //DRAW THE OUTPUT
}
void queue(){ // Excecutes the queue pseudocode
c=0;
for(int l=0; l<queueSize; l++){ //Check for overflow
if(a[l]!=0){
c++;
}
}
if((back!=front || back!=(front-1)) && c!=6){
if(back < queueSize){
a[back]=info;
back++;
}
else{
back=0;
a[back]=info;
back++;
}
for(int i=0; i<queueSize; i++){
println("New Array "+i+": "+a[i]);
}
}
else{
println("QUEUE is full");
text("FULL",40,-20,0);
}
println("");
}
void dequeue(){ // Executes the dequeued in pseudocode
d=0;
for(int h=0; h< queueSize; h++){ //Checks for elements in the queue
if(a[h]!=0){
d=d+1;
}
}
if(front < 0 || front > queueSize || d==0){ // Overflow
println("The front is bad or full.");
text("ERROR",40,-20,0);
}
else{
info=a[front]; //The element dequeue
p=info;
println("The dequeue number: "+info);
a[front]=0; //Reset the dequeued position
for(int j=0; j<queueSize; j++){
println("Array "+j+": "+a[j]);
}
front=front+1; //New Front
if(front==queueSize){
front=0;
}
println("New Front: "+front);
println("");
}
}
void keyPressed(){
if(key=='1' || key=='2' || key=='3' || key=='4'){ // Queued options
info=((int)key)-48;
queue();
}
else{
if(key=='6'){ // Dequeue Option
dequeue();
}
}
}
void options(){ //Options for queueing
fill(255); // text color
text("4 \n \n 3 \n \n 2 \n \n 1",-80,-20,0);
text("For dequeueing: \n press number 6",40,0,0);
x=x+sin(45)/4;
y=y+cos(45)/4;
pushMatrix();
translate(-60,40,0);
rotate(x,x,x,x);
fill(24,24,24);
noStroke();
drawStar();
popMatrix();
pushMatrix();
translate(-60,20,0);
rotate(y,y,45,y);
fill(64,64,64);
drawStar();
popMatrix();
pushMatrix();
translate(-60,0,0);
rotate(y,x,y,x);
fill(96,96,96);
drawStar();
popMatrix();
pushMatrix();
translate(-60,-20,0);
rotate(x,y,x,y);
fill(160,160,160);
drawStar();
popMatrix();
}
void drawQueue(){ // draw the list
t=front;
if(t!=0){ // Check if front is at 0
for(int f=0; f < queueSize; f++){ //Rearrange the array order
b[f]=a[t];
t++;
if(t==queueSize){
t=0;
}
}
}
else{
for(int f=0; f < queueSize; f++){
b[f]=a[f];
}
}
for(int com=0; com<queueSize; com++){ //Draw the queue
switch(b[com]){
case 0:
fill(pathR,pathG,pathB);
break;
case 1:
fill(24,24,24);
break;
case 2:
fill(64,64,64);
break;
case 3:
fill(96,96,96);
break;
case 4:
fill(160,160,160);
break;
case 6:
fill(0,0,0);
break;
}
pushMatrix();
translate(0,-40+(com*20),0);
rotate(x,y,x,y);
noStroke();
drawStar();
fill(pathR,pathG,pathB);
popMatrix();
}
}
void drawDequeued(){ //Draw the dequeued element
switch(p){
case 0:
fill(pathR,pathG,pathB);
break;
case 1:
fill(24,24,24);
break;
case 2:
fill(64,64,64);
break;
case 3:
fill(96,96,96);
break;
case 4:
fill(160,160,160);
break;
case 6:
fill(pathR,pathG,pathB);
break;
default:
fill(pathR,pathG,pathB);
break;
}
pushMatrix();
translate(40,40,0);
noStroke();
drawStar();
popMatrix();
}
void drawStar(){
beginShape(TRIANGLES);
vertex(0,0,0);
vertex(2,-3,0);
vertex(-2,-3,0);
vertex(0,0,0);
vertex(-2,-3,0);
vertex(-3.3,0.6,0);
vertex(0,0,0);
vertex(2,-3,0);
vertex(3.3,0.6,0);
vertex(0,0,0);
vertex(0,3.2,0);
vertex(3.3,0.6,0);
vertex(0,0,0);
vertex(0,3.2,0);
vertex(-3.3,0.6,0);
vertex(2,-3,0);
vertex(0,-9,-2);
vertex(-2,-3,0);
vertex(-2,-3,0);
vertex(-9,-3,-2);
vertex(-3.3,0.6,0);
vertex(2,-3,0);
vertex(9,-3,-2);
vertex(3.3,0.6,0);
vertex(0,3.2,0);
vertex(6.5,7.2,-2);
vertex(3.3,0.6,0);
vertex(0,3.2,0);
vertex(-6.5,7.2,-2);
vertex(-3.3,0.6,0);
vertex(0,0,-4);
vertex(2,-3,-4);
vertex(-2,-3,-4);
vertex(0,0,-4);
vertex(-2,-3,-4);
vertex(-3.3,0.6,-4);
vertex(0,0,-4);
vertex(2,-3,-4);
vertex(3.3,0.6,-4);
vertex(0,0,-4);
vertex(0,3.2,-4);
vertex(3.3,0.6,-4);
vertex(0,0,-4);
vertex(0,3.2,-4);
vertex(-3.3,0.6,-4);
vertex(2,-3,-4);
vertex(0,-9,-2);
vertex(-2,-3,-4);
vertex(-2,-3,-4);
vertex(-9,-3,-2);
vertex(-3.3,0.6,-4);
vertex(2,-3,-4);
vertex(9,-3,-2);
vertex(3.3,0.6,-4);
vertex(0,3.2,-4);
vertex(6.5,7.2,-2);
vertex(3.3,0.6,-4);
vertex(0,3.2,-4);
vertex(-6.5,7.2,-2);
vertex(-3.3,0.6,-4);
vertex(0,-9,-2);
vertex(2,-3,-4);
vertex(2,-3,0);
vertex(0,-9,-2);
vertex(-2,-3,-4);
vertex(-2,-3,0);
vertex(-9,-3,-2);
vertex(-2,-3,-4);
vertex(-2,-3,0);
vertex(-9,-3,-2);
vertex(-3.3,0.6,-4);
vertex(-3.3,0.6,0);
vertex(9,-3,-2);
vertex(2,-3,-4);
vertex(2,-3,0);
vertex(9,-3,-2);
vertex(3.3,0.6,-4);
vertex(3.3,0.6,0);
vertex(6.5,7.2,-2);
vertex(0,3.2,-4);
vertex(0,3.2,0);
vertex(6.5,7.2,-2);
vertex(3.3,0.6,-4);
vertex(3.3,0.6,0);
vertex(-6.5,7.2,-2);
vertex(0,3.2,-4);
vertex(0,3.2,0);
vertex(-6.5,7.2,-2);
vertex(-3.3,0.6,-4);
vertex(-3.3,0.6,0);
endShape();
}
Comentários