Attack of the Twitter Critter!
import java.util.ArrayList; import java.util.HashMap; import noc.Vector3D; import processing.core.PApplet; import processing.core.PImage; import twitter4j.Trend; public class Colony { private static final int FONT_SIZE = 15; public static final int TEXT_POINT_Y = 50; public static final int TEXT_POINT_X = 810; private static final int BACTERIAS_INITIAL_NUMBER = 70; private static final int MAX_AGE = 1000; private static final boolean DEATH_ENABLE = false; private ArrayList organisms; private HashMap hm; public Util util; private Organism mainBoid; public String action = ""; private Path path; public boolean drawS[]; public boolean drawHelp = false; public boolean drawDetails = false; public String menuSelection; private boolean didGoToInitialPoint = false; private String[] topics; public Colony(HashMap hm, Util util) { this.drawS = new boolean[11]; drawS[1] = true; drawS[2] = true; drawS[3] = true; drawS[4] = true; drawS[5] = true; drawS[6] = true; drawS[7] = true; drawS[8] = true; drawS[9] = true; drawS[10] = true; menuSelection = "0"; this.hm = hm; this.organisms = new ArrayList(); this.util = util; //mainBoid = addBoid(new Vector3D(0, 0)); this.path = new Path(); pointsOnPath(); } private void pointsOnPath() { path.addPoint(63,520); path.addPoint(74,119); path.addPoint(84,59); path.addPoint(145,34); path.addPoint(256,29); path.addPoint(332,29); path.addPoint(377,64); path.addPoint(390,118); path.addPoint(394,205); path.addPoint(398,420); path.addPoint(399,493); path.addPoint(388,540); path.addPoint(336,559); path.addPoint(232,565); path.addPoint(125,565); path.addPoint(78,541); path.addPoint(66,519); path.addPoint(65,517); } public void addInitialBacterias() { for (int i = 0; i < BACTERIAS_INITIAL_NUMBER; i++) { organisms.add(new Organism(new Vector3D(random(width), random(height)), 3.0f, 0.1f, util.randomGroup(), floor(random(1.0f, 3.0f)), hm)); } } public synchronized Organism addBoid(Vector3D initialPosition) { Organism b = new Organism(initialPosition, 3.0f, 0.1f, util.randomGroup(), PApplet.floor(random(1.0f, 4.0f)), hm); organisms.add(b); return b; } public synchronized Organism addBoid(Vector3D initialPosition, String group) { Organism b = new Organism(initialPosition, 1.5f, 0.1f, group, floor(random(1.0f, 4.0f)), hm); organisms.add(b); return b; } public synchronized void loop() { path.display(); textFont(font, 10); Iterator it = organisms.iterator(); //while(it.hasNext()){ for (int i = 0; i < organisms.size(); i++) { Organism b = (Organism)organisms.get(i); //Organism b = (Organism)it.next(); if (b.age > MAX_AGE && DEATH_ENABLE) { organisms.remove(i); } else { act(b); if(b.group.equals("1")){ b.selected = true; } if(checkGroupRun(b.group)){ b.run(); } mainBoid = b; } } this.displayMenu(); // if(organisms.size() > 120){ // util.duplicateControl = new Vector(); // organisms.clear(); // } } private boolean checkGroupRun(String group){ boolean ret = true; if(group.equals("1") && drawS[1]){ ret = true; } else if(group.equals("2") && drawS[2]){ ret = true; } else if(group.equals("3") && drawS[3]){ ret = true; } else if(group.equals("4") && drawS[4]){ ret = true; } else if(group.equals("5") && drawS[5]){ ret = true; } else if(group.equals("6") && drawS[6]){ ret = true; } else if(group.equals("7") && drawS[7]){ ret = true; } else if(group.equals("8") && drawS[8]){ ret = true; } else if(group.equals("9") && drawS[9]){ ret = true; } else if(group.equals("10") && drawS[10]){ ret = true; } else { ret = false; } return ret; } private Vector3D locationsOrganize(Organism b) { String group = b.group; Vector3D dest = new Vector3D(0, 0); if (group.equals("1")) { dest = new Vector3D(108, 97); } else if (group.equals("2")) { dest = new Vector3D(301, 83); } else if (group.equals("3")) { dest = new Vector3D(472, 102); } else if (group.equals("4")) { dest = new Vector3D(718, 184); } else if (group.equals("5")) { dest = new Vector3D(665, 386); } else if (group.equals("6")) { dest = new Vector3D(498, 326); } else if (group.equals("7")) { dest = new Vector3D(316, 326); } else if (group.equals("8")) { dest = new Vector3D(139, 382); } else if (group.equals("9")) { dest = new Vector3D(188, 559); } else { dest = new Vector3D(500, 556); } return dest; } private synchronized void act(Organism b) { if (action.equals("organize")) { Vector3D dest = locationsOrganize(b); b.seek(dest); } else if (action.equals("stopAll")) { b.stop(); } else if (action.equals("follow")) { b.follow(mainBoid); } else if (action.equals("goRound")) { b.goRound(mainBoid); } else if (action.equals("followPath")) { followPath(b); } else if(action.equals("killAll")) { this.organisms.removeAll(organisms); action = "wander"; } else if(action.equals("wander")) { b.wander(); } else { b.wander(); } if(!action.equals("followPath")){ didGoToInitialPoint = false; b.isLabelEnable = false; } } private void followPath(Organism b){ b.acceleration.normalize(); b.velocity.normalize(); moveEverybody(); obstacle(b); printLabel(b); b.follow(path); if(b.stopped){ if(st.blockade){ b.acceleration.limit(10); b.acceleration.add(new Vector3D(100,0)); b.velocity.limit(100); b.velocity.add(new Vector3D(100,0)); b.velocity.normalize(); b.acceleration.normalize(); b.stopped = false; st.blockade = false; } } } private void printLabel(Organism b) { float up = 60; float down = 550; float left = 300; float right = 425; if(b.location.x > left && b.location.x < right && b.location.y > up && b.location.y < down){ b.isLabelEnable = true; } else { b.isLabelEnable = false; } } private void moveEverybody() { if(!didGoToInitialPoint){ for (int i = 0; i <this.organisms.size(); i++) { Organism b = (Organism)organisms.get(i); b.location.x = 50; b.location.y = 50; } didGoToInitialPoint = true; } } public void obstacle(Organism b){ float up = 15; float down = 40; float left = 200; float right = 205; if(b.location.x > left && b.location.x < right && b.location.y > up && b.location.y < down){ b.stop(); } } public void setAction(String action) { this.action = action; } public void removeAllFromGroup(String group){ for (int i = 0; i < organisms.size(); i++) { Organism org = (Organism)organisms.get(i); if(org.group.equals(group)){ organisms.remove(i); } } } public void updateTopics(String[] topics, int rateLimitRemaining) { this.topics = topics; } public void displayMenu(){ int currentX = 10; int currentY = 90; PImage aux; textFont(font, 15); text(organisms.size() + " organisms", 800 + currentX + 15, currentY - 25); if(this.topics == null){ textFont(fontMenu,14); text("acquiring information...", 800 + currentX, currentY); } else { for(int i = 1; i <= 10; i++){ String trendTitle = topics[i-1].split("OR")[0].trim(); aux = (PImage)hm.get("ic" + i); pushMatrix(); translate(800 + currentX,currentY); image(aux, 0, 0); if(menuCollision(800 + currentX,currentY,200,40)){ menuSelection = i + ""; } fill(255); textFont(fontMenu,14); text(trendTitle, 40, 20); if(!drawS[i]){ stroke(255); color c = color(200, 0, 0, 70); fill(c); rect(-10,-5, 200, 40); } popMatrix(); currentY = currentY + 40; } if(menuCollision(930,525,70,30)){ menuSelection = "help"; } else if(menuCollision(910,560,80,30)){ menuSelection = "details"; } if(drawHelp){ image(help, 26,21); } else if (drawDetails){ image(details, 26,21); } // for (int i = 0; i < 6; i++) { // String trendTitle = topics[i].getQuery().split("OR")[0].trim(); // nextY = FONT_SIZE + TEXT_POINT_Y + (i+1)*FONT_SIZE; // noStroke(); // fill(pallette[i]); // rect(TEXT_POINT_X, nextY - FONT_SIZE + 4, 20, FONT_SIZE); // fill(255); // textFont(fontMenu,14); // text(trendTitle, TEXT_POINT_X + 25, nextY); // } } //text(this.rateLimitRemaining + " RLR", TEXT_POINT_X, nextY + FONT_SIZE); } private boolean menuCollision(int X, int Y, int thX, int thY) { boolean ret = false; float boundXR = X + thX; float boundXL = X;// - thX; float boundYU = Y;// - thY; float boundYD = Y + thY; if(mouseX > boundXL && mouseX < boundXR && mouseY > boundYU && mouseY < boundYD){ ret = true; } return ret; } }
import twitter4j.Trend; import twitter4j.Twitter; import twitter4j.TwitterException; public class Feeder extends Thread{ private static final int NUMBER_TRENDS = 9; private Twitter twitter; private Colony colony; public Feeder(Colony colony){ this.twitter = new Twitter(); this.colony = colony; } public void run() { String[] queryFile = loadStrings("query.txt"); String query = queryFile[0]; String[] topics = new String[NUMBER_TRENDS + 1]; while(true){ try { Trend[] trends = twitter.getCurrentTrends().getTrends(); int rateLimitRemaining = twitter.rateLimitStatus().getRemainingHits(); SearchAndAddThread[] threads = new SearchAndAddThread[NUMBER_TRENDS + 1]; for (int i = 0; i < NUMBER_TRENDS; i++) { String trend = trends[i].getQuery(); topics[i+1] = trend; threads[i+1] = new SearchAndAddThread("SAThread" + (i + 1), trend, colony, i+2); threads[i+1].start(); } topics[0] = query; threads[0] = new SearchAndAddThread("SAThread" + 0, query, colony, 1); threads[0].start(); colony.updateTopics(topics, rateLimitRemaining); sleep(25000); } catch (InterruptedException e) { e.printStackTrace(); } catch (TwitterException e) { e.printStackTrace(); } } } }
import noc.*; import java.util.HashMap; import processing.core.PApplet; import processing.core.PFont; import processing.core.PImage; int WINDOW_WIDTH = 1000; int WINDOW_HEIGHT = 600; Util util; HashMap hm; Colony colony; PFont font; PFont fontMenu; PFont fontCredits; boolean isLoaded; boolean background; PImage bg; PImage details; PImage help; Feeder fd; ParadeTimerThread st; PresenterThread presenter; void setup() { size(1000, 600); colorMode(RGB,255,255,255,100); this.util = new Util(); this.hm = new HashMap(); this.colony = new Colony(hm, util); fd = new Feeder(colony); fd.start(); //this.font = loadFont("CourierNewPSMT-15.vlw");//createFont("comfortaa.ttf", 15); this.fontMenu = createFont("comfortaa.ttf", 14); this.font = fontMenu; this.fontCredits = loadFont("fontCredits.vlw"); background = true; st = new ParadeTimerThread("ParadeTimer"); st.start(); presenter = new PresenterThread("PresenterThread", colony); presenter.start(); bg = loadImage("background3.png"); details = loadImage("details-menu2.png"); help = loadImage("about-menu2.png"); //smooth(); } void draw() { if(background){ background(0,0,127,50); image(bg, 0, 0); } if(!util.isLoaded){ textFont(this.font,15); text("Loading images...", 100, 100); this.util.preloadImages(hm); } line(800,0,800,600); this.colony.loop(); } void mousePressed() { //this.colony.addBoid(new Vector3D(mouseX,mouseY)); //println(mouseX + " | " + mouseY); if(mouseX < 800){ colony.menuSelection = "0"; } if(this.colony.menuSelection.equals("1")){ colony.drawS[1] = !colony.drawS[1]; } else if(this.colony.menuSelection.equals("2")){ colony.drawS[2] = !colony.drawS[2]; } else if(this.colony.menuSelection.equals("3")){ colony.drawS[3] = !colony.drawS[3]; } else if(this.colony.menuSelection.equals("4")){ colony.drawS[4] = !colony.drawS[4]; } else if(this.colony.menuSelection.equals("5")){ colony.drawS[5] = !colony.drawS[5]; } else if(this.colony.menuSelection.equals("6")){ colony.drawS[6] = !colony.drawS[6]; } else if(this.colony.menuSelection.equals("7")){ colony.drawS[7] = !colony.drawS[7]; } else if(this.colony.menuSelection.equals("8")){ colony.drawS[8] = !colony.drawS[8]; } else if(this.colony.menuSelection.equals("9")){ colony.drawS[9] = !colony.drawS[9]; } else if(this.colony.menuSelection.equals("10")){ colony.drawS[10] = !colony.drawS[10]; } else if(this.colony.menuSelection.equals("help")){ colony.drawHelp = !colony.drawHelp; colony.drawDetails = false; } else if(this.colony.menuSelection.equals("details")){ colony.drawDetails = !colony.drawDetails; colony.drawHelp = false; } else { } } void mouseReleased(){ colony.menuSelection = "0"; } void keyPressed(){ if(key == 'a' || key == 'A'){ this.colony.setAction("organize"); } else if (key == 's' || key == 'S'){ this.colony.setAction("stopAll"); } else if(key == 'e' || key == 'E'){ this.colony.setAction("follow"); } else if(key == 'd' || key == 'D'){ this.colony.setAction("wander"); } else if(key == 'w' || key == 'W'){ this.colony.setAction("goRound"); } else if(key == 'q' || key == 'Q'){ this.colony.setAction("followPath"); } else if(key == 'r' || key == 'R'){ background = !background; } else if(key == 'f' || key == 'F'){ this.colony.setAction("killAll"); } else if(key == '1'){ colony.drawS[1] = !colony.drawS[1]; } else if(key == '2'){ colony.drawS[2] = !colony.drawS[2]; } else if(key == '3'){ colony.drawS[3] = !colony.drawS[3]; } else if(key == '4'){ colony.drawS[4] = !colony.drawS[4]; } else if(key == '5'){ colony.drawS[5] = !colony.drawS[5]; } else if(key == '6'){ colony.drawS[6] = !colony.drawS[6]; } else if(key == '7'){ colony.drawS[7] = !colony.drawS[7]; } else if(key == '8'){ colony.drawS[8] = !colony.drawS[8]; } else if(key == '9'){ colony.drawS[9] = !colony.drawS[9]; } else if(key == '0'){ colony.drawS[10] = !colony.drawS[10]; } }
import java.util.HashMap; import noc.Vector3D; import processing.core.PApplet; import processing.core.PImage; //Seek_Arrive // Daniel Shiffman <http://www.shiffman.net> // The "Boid" class // Created 2 May 2005 /** * Class Boid created by Daniel Shiffman <http://www.shiffman.net> * * Class Boid modified by Filipe Calegario <http://filipecalegario.com> * @author fcac */ public class Organism { private static final boolean AGING_ENABLE = true; public String text; Vector3D location; Vector3D lastLocation; Vector3D velocity; Vector3D acceleration; float r; float wandertheta; float maxforce; // Maximum steering force float maxspeed; // Maximum speed PImage img; int choose; String group; int subspecie; int age; boolean stopped = false; boolean selected = false; public long pseudoTimer = 0; HashMap preloadedImgs; public boolean isLabelEnable; public Organism(Vector3D l, float ms, float mf, String g, int s, HashMap hm) { this.acceleration = new Vector3D(0,0); this.velocity = new Vector3D(0,0); this.location = l.copy(); this.r = 2.0f; this.wandertheta = 0.0f; this.maxspeed = ms; this.maxforce = mf; this.choose = 1; this.group = g; this.subspecie = s; this.age = 0; this.preloadedImgs = hm; this.text = "waiting information..."; this.img = loadImage("new_species/" + group + "-1.png"); } public void run() { update(); if(AGING_ENABLE){ age++; } borders(); render(); } // Method to update location public void update() { // Update velocity velocity.add(acceleration); // Limit speed velocity.limit(maxspeed); lastLocation = location.copy(); location.add(velocity); // Reset acceleration to 0 each cycle acceleration.setXYZ(0,0,0); } public void seek(Vector3D target) { acceleration.add(steer(target,false)); } public void arrive(Vector3D target) { acceleration.add(steer(target,true)); } public void follow(Organism followee){ acceleration.add(steer(followee.location, true)); } // This function implements Craig Reynolds' path following algorithm // http://www.red3d.com/cwr/steer/PathFollow.html public void follow(Path p) { // Predict location 25 (arbitrary choice) frames ahead Vector3D predict = velocity.copy(); predict.normalize(); predict.mult(25); Vector3D predictLoc = Vector3D.add(location, predict); // Now we must find the normal to the path from the predicted location // We look at the normal for each line segment and pick out the closest one Vector3D target = null; Vector3D dir = null; float record = 1000000; // Start with a very high record distance that can easily be beaten // Loop through all points of the path for (int i = 0; i < p.points.size()-1; i++) { // Look at a line segment Vector3D a = (Vector3D) p.points.get(i); Vector3D b = (Vector3D) p.points.get(i+1); // Get the normal point to that line Vector3D normal = getNormalPoint(predictLoc,a,b); // Check if normal is on line segment float da = Vector3D.distance(normal,a); float db = Vector3D.distance(normal,b); Vector3D line = Vector3D.sub(b,a); // If it's not within the line segment, consider the normal to just be the end of the line segment (point b) if (da + db > line.magnitude()+1) { normal = b.copy(); } // How far away are we from the path? float d = Vector3D.distance(predictLoc,normal); // Did we beat the record and find the closest line segment? if (d < record) { record = d; // If so the target we want to steer towards is the normal target = normal; // Look at the direction of the line segment so we can seek a little bit ahead of the normal dir = line; dir.normalize(); // This is an oversimplification // Should be based on distance to path & velocity dir.mult(10); } } // Draw the debugging stuff if (false) { // Draw normal location fill(0); noStroke(); line(predictLoc.x,predictLoc.y,target.x,target.y); ellipse(target.x,target.y,4,4); stroke(0); // Draw actual target (red if steering towards it) line(predictLoc.x,predictLoc.y,target.x,target.y); if (record > Path.RADIUS) fill(255,0,0); noStroke(); ellipse(target.x+dir.x, target.y+dir.y, 8, 8); } // Only if the distance is greater than the path's radius do we bother to steer if (record > Path.RADIUS) { target.add(dir); seek(target); } } public void stop(){ velocity.limit(0); acceleration.limit(0); this.stopped = true; } public void wander() { float wanderR = 16.0f; // Radius for our "wander circle" float wanderD = 60.0f; // Distance for our "wander circle" float change = 0.25f; wandertheta += random(-change,change); // Randomly change wander theta // Now we have to calculate the new location to steer towards on the wander circle Vector3D circleloc = velocity.copy(); // Start with velocity circleloc.normalize(); // Normalize to get heading circleloc.mult(wanderD); // Multiply by distance circleloc.add(location); // Make it relative to boid's location Vector3D circleOffSet = new Vector3D(wanderR*PApplet.cos(wandertheta),wanderR*PApplet.sin(wandertheta)); Vector3D target = Vector3D.add(circleloc,circleOffSet); acceleration.add(steer(target,false)); // Steer towards it } private Vector3D steer(Vector3D target, boolean slowdown) { Vector3D steer; // The steering vector Vector3D desired = Vector3D.sub(target,location); // A vector pointing from the location to the target float d = desired.magnitude(); // Distance from the target is the magnitude of the vector if (d > 0) { desired.normalize(); if ((slowdown) && (d < 100.0f)) { desired.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary } else { desired.mult(maxspeed); } steer = Vector3D.sub(desired,velocity); steer.limit(maxforce); // Limit to maximum steering force } else { steer = new Vector3D(0,0); } return steer; } public void goRound(Organism b){ this.acceleration.add(roundVec(b)); } public void render() { float theta = velocity.heading2D() + PApplet.radians(90); fill(255); stroke(255); // if(choose == 26){ // choose = 1; // } pushMatrix(); if(checkCollisionWithMouse() && mousePressed){ selected = !selected; } if(checkCollisionWithMouse() || isLabelEnable || selected){ highlight(); } translate(location.x,location.y); //rotate(PApplet.radians(-90)); rotate(theta); String animation = animation(); //println(animation); PImage img = (PImage)preloadedImgs.get(animation); //PImage img = loadImage("new_species/1-1.png"); image(img, -img.width/2, -img.height/2); popMatrix(); } private Vector3D roundVec(Organism b){ Vector3D direct = Vector3D.sub(b.location, this.location); Vector3D side = Vector3D.rotate2D(direct, PApplet.radians(90)); side.normalize(); direct.normalize(); Vector3D result = Vector3D.add(direct, side); result.normalize(); result.mult(0.4f); return result; } private void highlight(){ float recWidth = 30; float recHeight = 30; float lineM = 40; //noFill(); //fill(color(0, 255, 0, 80)); String[] pieces = this.text.split(" "); String finalText = ""; for(int i = 0; i < pieces.length; i++){ finalText = finalText + pieces[i] + " "; if(i == (pieces.length/2 - 1)){ finalText = finalText + "\n"; } } fill(color(35,64,245,90)); strokeWeight(4); ellipse(location.x, location.y, 70, 70); strokeWeight(1); Vector3D edge4 = new Vector3D(location.x + lineM, location.y - lineM); Vector3D edge1 = new Vector3D(edge4.x, edge4.y + recHeight); Vector3D edge2 = new Vector3D(edge1.x + recWidth, edge1.y); Vector3D edge3 = new Vector3D(edge1.x + recWidth, edge1.y + recHeight); line(location.x, location.y, edge1.x, edge1.y); line(edge1.x, edge1.y, edge4.x, edge4.y); line(edge1.x, edge1.y, edge2.x, edge2.y); rect(edge4.x, edge4.y - 30, this.text.length()*5, 60); fill(255); textFont(font, 15); text(finalText, edge1.x + 15, edge1.y - 40); } private String animation(){ if(choose == 4){ choose = 1; } choose++; return group + "-" + choose; } // private String animation(){ // String imgFile = ""; // int limit = 26; // if(subspecie == 3){ // limit = 6; // } // if(choose == limit){ // choose = 1; // } // if(choose < 10){ // imgFile = "0" + choose + ".png"; // } // else { // imgFile = choose + ".png"; // } // String ageAppear = ""; // if(age > 1000){ // ageAppear = "5"; // } // else if (age > 800){ // ageAppear = "4"; // } // else if (age > 600){ // ageAppear = "3"; // } // else if (age > 400){ // ageAppear = "2"; // } // else { // ageAppear = "1"; // } // if(AGING_ENABLE){ // age++; // } // choose++; // String ret = "species/" + group + "/" + subspecie + "/" + ageAppear + "/" + imgFile; // return ret; // } private Vector3D getNormalPoint(Vector3D p, Vector3D a, Vector3D b) { Vector3D ap = Vector3D.sub(p,a); Vector3D ab = Vector3D.sub(b,a); ab.normalize(); ab.mult(ap.dot(ab)); Vector3D normalPoint = Vector3D.add(a,ab); return normalPoint; } private boolean checkCollisionWithMouse() { boolean ret = false; float boundXR = location.x + 22; float boundXL = location.x - 22; float boundYU = location.y - 22; float boundYD = location.y + 22; if(mouseX > boundXL && mouseX < boundXR && mouseY > boundYU && mouseY < boundYD){ ret = true; } return ret; } private void borders() { float width = 800; float height = 600; if (location.x < -r) location.x = width+r; if (location.y < -r) location.y = height+r; if (location.x > width+r) location.x = -r; if (location.y > height+r) location.y = -r; // if ((location.y > height) || (location.y < 0)) { // velocity.y *= -1; // } // if ((location.x < 0) || (location.x > width)) { // velocity.x *= -1; // } } }
public class ParadeTimerThread extends Thread { public boolean blockade = true; public ParadeTimerThread(String name) { super(name); } public void run() { while(true){ try { sleep(700); this.blockade = true; sleep(700); this.blockade = false; } catch (InterruptedException e) { e.printStackTrace(); } blockade = true; } } }
import java.util.ArrayList; import noc.Vector3D; import processing.core.PApplet; // Path Following // Daniel Shiffman <http://www.shiffman.net> // The Nature of Code, Spring 2009 class Path { private static final boolean DEBUG = false; public static final float RADIUS = 8; ArrayList points; boolean first; public Vector3D initialPoint; public Path() { points = new ArrayList(); first = true; } public void addPoint(float x, float y) { Vector3D point = new Vector3D(x,y); if(first){ initialPoint = point.copy(); first = false; } points.add(point); } // Draw the path public void display() { // Draw the radius as thick lines and circles if (DEBUG) { // Draw end points for (int i = 0; i < points.size(); i++) { Vector3D point = (Vector3D) points.get(i); fill(175); noStroke(); ellipse(point.x,point.y,RADIUS*2,RADIUS*2); } // Draw Polygon around path for (int i = 0; i < points.size()-1; i++) { Vector3D start = (Vector3D) points.get(i); Vector3D end = (Vector3D) points.get(i+1); Vector3D line = Vector3D.sub(end,start); Vector3D normal = new Vector3D(line.y,-line.x); normal.normalize(); normal.mult(RADIUS); // Polygon has four vertices Vector3D a = Vector3D.add(start, normal); Vector3D b = Vector3D.add(end, normal); Vector3D c = Vector3D.sub(end, normal); Vector3D d = Vector3D.sub(start, normal); fill(175); noStroke(); beginShape(); vertex(a.x,a.y); vertex(b.x,b.y); vertex(c.x,c.y); vertex(d.x,d.y); endShape(); } // Draw Regular Line stroke(0); noFill(); beginShape(); for (int i = 0; i < points.size(); i++) { Vector3D loc = (Vector3D) points.get(i); vertex(loc.x,loc.y); } endShape(); } } }
public class PresenterThread extends Thread { public Colony colony; public final int WANDER_TIME = 20000; public final int PARADE_TIME = 40000; public final int SELECTION_TIME = 60000; public final int HELP_TIME = 15000; public final int DETAILS_TIME = 15000; public PresenterThread(String name, Colony colony) { super(name); this.colony = colony; } public void run() { try { while(true){ //---------------------- colony.action = "wander"; sleep(WANDER_TIME); //---------------------- colony.drawDetails = false; colony.action = "followPath"; sleep(PARADE_TIME); //---------------------- colony.action = "wander"; colony.drawS[2] = false; colony.drawS[3] = false; colony.drawS[4] = false; colony.drawS[5] = false; colony.drawS[6] = false; colony.drawS[7] = false; colony.drawS[8] = false; colony.drawS[9] = false; colony.drawS[10] = false; sleep(SELECTION_TIME); colony.drawS[2] = true; colony.drawS[3] = true; colony.drawS[4] = true; colony.drawS[5] = true; colony.drawS[6] = true; colony.drawS[7] = true; colony.drawS[8] = true; colony.drawS[9] = true; colony.drawS[10] = true; //---------------------- util.duplicateControl = new Vector(); colony.organisms.clear(); //---------------------- colony.drawHelp = true; sleep(HELP_TIME); //---------------------- colony.drawHelp = false; colony.drawDetails = true; sleep(DETAILS_TIME); //---------------------- util.duplicateControl = new Vector(); colony.organisms.clear(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.util.List; import java.util.Vector; import noc.Vector3D; import twitter4j.Query; import twitter4j.QueryResult; import twitter4j.Tweet; import twitter4j.Twitter; import twitter4j.TwitterException; public class SearchAndAddThread extends Thread { private String query; private Colony colony; private int group; private Twitter twitter; private Vector previousTweets; public SearchAndAddThread(String name, String query, Colony colony, int group) { super(name); this.query = query; this.colony = colony; this.group = group; this.twitter = new Twitter(); this.previousTweets = new Vector(); } public void run() { QueryResult result; try { this.query = this.query.replaceAll("\"", ""); this.query = this.query.replaceAll("#", ""); result = twitter.search(new Query(this.query)); List tweets = result.getTweets(); for (int i = 0; i < tweets.size(); i++) { Tweet tw = (Tweet)tweets.get(i); String text = tw.getText(); if(this.getName().equals("SAThread0")){ text = "@" + tw.getFromUser() + " " + text; } if(!util.duplicateControl.contains(text)){ util.duplicateControl.add(text); Organism b = colony.addBoid(new Vector3D(0,0), util.getGroup(this.group)); b.text = text; } Thread.sleep(10000); } } catch (TwitterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } }
import java.util.HashMap; import noc.Vector3D; import processing.core.PApplet; import processing.core.PImage; public class Util { boolean isLoaded; public Vector duplicateControl; public Util() { duplicateControl = new Vector(); isLoaded = false; } private String format(int animation){ String imgFile = ""; if(animation < 10){ imgFile = "0" + animation + ".png"; } else { imgFile = animation + ".png"; } return imgFile; } public void preloadImages(HashMap hm){ for(int i = 1; i <= 10; i++){ for(int j = 1; j<= 4; j++ ){ String imgPath = "new_species/" + i + "-" + j + ".png"; //println(imgPath); PImage img = loadImage(imgPath); hm.put(i + "-" + j, img); } String imgPath = "new_species/ic" + i + ".png"; PImage img = loadImage(imgPath); hm.put("ic" + i, img); } isLoaded = true; } // public void preloadImages(HashMap hm){ // String group = ""; // String imgPath = ""; // for(int i = 1; i <= 6; i++){ // switch(i){ // case 1: // group = "familie"; // break; // case 2: // group = "job"; // break; // case 3: // group = "orders"; // break; // case 4: // group = "school"; // break; // case 5: // group = "spam"; // break; // case 6: // group = "unclass"; // break; // } // for(int s = 1; s <= 3; s++){ // for (int a = 1; a <= 5; a++){ // switch(s){ // case 1: // case 2: // for(int animation = 1; animation <=25; animation++){ // imgPath = "species/" + group + "/" + s + "/" + a + "/" + format(animation); // PImage img = loadImage(imgPath); // hm.put(imgPath, img); // } // break; // case 3: // for(int animation = 1; animation <=5; animation++){ // imgPath = "species/" + group + "/" + s + "/" + a + "/" + format(animation); // PImage img = loadImage(imgPath); // hm.put(imgPath, img); // } // break; // } // } // } // // } // isLoaded = true; // } public String randomGroup(){ // int i = PApplet.floor(random(1.0f,7.0f)); // String rt = ""; // if(i == 1){ // rt = "familie"; // } // else if(i == 2){ // rt = "job"; // } // else if(i == 3){ // rt = "orders"; // } // else if(i == 4){ // rt = "school"; // } // else if(i == 5){ // rt = "spam"; // } // else { // rt = "unclass"; // } String ret = "" + floor(random(1,11)); // println(ret); return ret; } public void drawVector(Vector3D vector, Vector3D location, float scayl) { pushMatrix(); float arrowsize = 4; // Translate to location to render vector translate(location.x,location.y); stroke(255); // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate rotate(vector.heading2D()); // Calculate length of vector & scale it to be bigger or smaller if necessary float len = vector.magnitude()*scayl; // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) line(0,0,len,0); line(len,0,len-arrowsize,+arrowsize/2); line(len,0,len-arrowsize,-arrowsize/2); popMatrix(); } public String getGroup(int i) { String ret = ""; switch (i) { case 1: ret = "1"; break; case 2: ret = "2"; break; case 3: ret = "3"; break; case 4: ret = "4"; break; case 5: ret = "5"; break; case 6: ret = "6"; break; case 7: ret = "7"; break; case 8: ret = "8"; break; case 9: ret = "9"; break; default: ret = "10"; break; } return ret; } }
OpenProcessing is an online community platform devoted to sharing and discussing Processing sketches in a collaborative, open-source environment.
Download Processing
Terms of Service
To contact, send an email to:

See the feedback forum and vote!
Follow OpenProcessing on Twitter.
All sketches are licensed under Creative Commons Attribution-Share Alike 3.0.
Color coding engine by Florian Jenett.
All the source code is licensed under Creative Commons GNU GPL.
Comments engine by Scriptsmill Comments Script.



