String careers[]; void setup(){ size(500,500); /* for(int i = 0; i < 20;i++){ unplaced.add(new Box()); }*/ frameRate(500); careers = loadStrings("careers.txt"); makeBoxes(); } void makeBoxes(){ for(int i = 0; i < careers.length; i++){ if(random(100) < 25){ unplaced.add(new Box(careers[i])); } } } void keyPressed(){ if(key == ' '){ placed = new ArrayList(); makeBoxes(); } } ArrayList unplaced = new ArrayList(); ArrayList placed = new ArrayList(); Box mover;// = new Box(); int counter = 50; void draw(){ background(255); if(mover != null){ mover.findLowest(); placed.add(mover); mover = null; } else { if(unplaced.size() > 0){ mover = unplaced.remove(0); } } for(Box b : placed){ b.draw(); } } Dir LEFT = new Dir(-2,0); Dir UP = new Dir(0,-2); Dir RIGHT = new Dir(2,0); Dir DOWN = new Dir(0,2); int BOTTOMISH = 400; int LEFTISH = 40; class Dir{ int xs,ys; Dir(int pxs,int pys){ xs = pxs; ys = pys; } } class Box{ int x,y,w,h; color c; Dir d; float sz; String s; boolean rot = false; Box(String ps){ x = 20;//random(0,500-w); y = 230;//random(0,500-h); c = color(random(100,255),random(100,255),random(100,255)); randomDir(); s = ps; sz = random(10,40); textSize(sz); w = (int)textWidth(s); h = (int)(textAscent() + textDescent()); if(random(100) < 50){ rot = true; h = (int)textWidth(s); w = (int)(textAscent() + textDescent()); } } void findLowest(){ int besty = -9999, bestx = -9999; for(x = width - w; x >= LEFTISH; x--){ int tryy = findLowestAtX(); if(tryy >= besty){ besty = y; bestx = x; } } if(bestx == LEFTISH){ bestx -= random(LEFTISH / 2); } x = bestx; y = besty; // println("found "+x+" and "+y); } int findLowestAtX(){ y = BOTTOMISH - h; while(hittingPlaced()){ y--; } if(y == BOTTOMISH - h){ y += random(h/2); } return y; } void randomDir(){ switch((int)random(3)){ case 0: d = UP; break; case 1: d = RIGHT; break; case 2: d = DOWN; break; } } void move(){ x += d.xs;//xs; y += d.ys;//ys; } void draw(){ noStroke(); fill(c); rect(x,y,w,h); pushMatrix(); textSize(sz); translate(x,y+h-textDescent()); if(rot){ translate(textDescent(),-h + textDescent()); rotate(PI/2); } fill(0); text(s,0,0); popMatrix(); } boolean hittingPlaced(){ for(Box b : placed){ if(hit(b)) { if(d == LEFT){ // if(b.x + b.w } return true; } } return false; } boolean hit(Box b) { if(overlap(this.x,this.x+this.w,b.x,b.x+b.w)&& overlap(this.y,this.y+this.h,b.y,b.y+b.h)) { return true; } return false; } boolean hit(float px, float py){ if(px >= x && px <= x + w && py >= y && py <= y + h){ return true; } return false; } boolean overlap(float line1val1,float line1val2,float line2val1,float line2val2) { float line1min = min(line1val1,line1val2); float line1max = max(line1val1,line1val2); float line2min = min(line2val1,line2val2); float line2max = max(line2val1,line2val2); if(line1min < line2min && line1max < line2min) { return false; } if(line1min > line2max && line1max > line2max) { return false; } return true; } } /* if(mover == null){ if(unplaced.size() > 0){ mover = unplaced.remove(0); } } if(mover != null){ if(mover.hittingPlaced()){ mover.move(); } else { placed.add(mover); mover = null; } } for(Box b : unplaced){ //b.draw(); }*/