class Bar{
ArrayList rectVertex;
float w = 10;
color c = color(0,200,0,200);
Bar(){
rectVertex = new ArrayList();
}
void addRect(float x,float y,float z,float px,float py,float pz){
this.rectVertex.add(new Rectangle(x,y,z,px,py,pz));
if(rectVertex.size() > 500){
rectVertex.remove(0);
}
}
void draw(){
int len = rectVertex.size();
for(int i = 0;i < len;i++ ){
Rectangle rec = (Rectangle)rectVertex.get(i);
rec.draw();
}
}
}
class Rectangle{
PVector pos;
PVector ppos;
float w = 10;
color c = color(random(255),random(255),random(255),random(255));
Rectangle(float x,float y,float z,float px,float py,float pz){
this.pos = new PVector(x,y,z);
this.ppos = new PVector(px,py,pz);
}
void draw(){
stroke(0);
fill(this.c);
beginShape();
vertex(this.pos.x,this.pos.y-this.w,this.pos.z);
vertex(this.pos.x,this.pos.y+this.w,this.pos.z);
vertex(this.ppos.x,this.ppos.y+this.w,this.ppos.z);
vertex(this.ppos.x,this.ppos.y-this.w,this.ppos.z);
endShape(CLOSE);
}
}
float u;
float v;
float vu = 0.;
float vv = 0.;
float au = 0.;
float av = 0.;
float px;
float py;
float pz;
boolean firstDraw = true;
Bar l;
void setup(){
size(450,450,P3D);
background(255);
l = new Bar();
}
void draw(){
background(255,30);
vu += au;
vv += av;
u += vu;
v += vv;
au *= 0.96;
av *= 0.96;
vu *= 0.92;
vv *= 0.92;
translate(width/2,height/2);
rotateY(v);
rotateX(u);
l.draw();
}
void mouseDragged(){
float x = (mouseX-width/2)*cos(v);
float y = (mouseX-width/2)*sin(u)*sin(v)+(mouseY-height/2)*cos(u);
float z = (mouseX-width/2)*cos(u)*sin(v)-(mouseY-height/2)*sin(u);
if(firstDraw){
firstDraw = false;
}
else{
l.addRect(px,py,pz,x,y,z);
}
px = x;
py = y;
pz = z;
}
void keyPressed(){
if(keyCode == UP){
au += PI/200.;
}
if(keyCode == RIGHT){
av += PI/200.;
}
if(keyCode == DOWN){
au -= PI/200.;
}
if(keyCode == LEFT){
av -= PI/200.;
}
}