public void trace(float x, float y) { println(x); if (frameCounter > 2 && mousePressed) { coords.add(new PVector(x, y)); frameCounter = 0; if (coordCounter > 0) { PVector tempCoord = (PVector) coords.get(coordCounter - 1); line(x, y, tempCoord.x, tempCoord.y); } coordCounter++; } // println(coordCounter); for (int i = 0; i < coords.size(); i++) { PVector tempCoordnear = (PVector) coords.get(i); float coordDist = dist(x, y, tempCoordnear.x, tempCoordnear.y); if (coordDist < distance) { stroke(255, 0, 0); line(x, y, tempCoordnear.x, tempCoordnear.y); } } frameCounter++; }
public void setup() { size(600, 400); smooth(); f = createFont("Georgia", 12, true); obstacles = new ArrayList(); boids = new ArrayList(); // A little algorithm to pick a bunch of random obstacles that don't overlap for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float r = random(50 - i / 2, 50); boolean ok = true; for (int j = 0; j < obstacles.size(); j++) { Obstacle o = (Obstacle) obstacles.get(j); if (dist(x, y, o.loc.x, o.loc.y) < o.radius + r + 20) { ok = false; } } if (ok) obstacles.add(new Obstacle(x, y, r)); } // Starting with three boids boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.2f)); boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.1f)); boids.add(new Boid(new PVector(random(width), random(height)), 2f, 0.05f)); }
public ArrayList getPastObjectStates() { ArrayList alPast = new ArrayList(); for (int i = intTimeIdx; i > 0 && i > intTimeIdx - 100; i--) { alPast.add(this.alObjectStateArchive.get(i)); } return alPast; }
// Does a deepcopy of an array list public static ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { alNew.add(((CelestialObject) al.get(i)).clone()); } return alNew; }
// Does a deepcopy of an array list public ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { PVector pv = (PVector) al.get(i); alNew.add(new PVector(pv.x, pv.y)); } return alNew; }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
public void mouseClicked() { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { println(obj.getName() + " clicked!"); break; } } }
public void draw() { background(255); for (int i = 0; i < balls.size(); i++) { Ball ball = (Ball) balls.get(i); ball.calc(); ball.display(); } }
public void render(PGraphics g) { if (parent != null) rot = PApplet.lerp(rot, parent.rot, 0.25f); g.rotate(rot); g.textSize(txtSize); g.text(txt, 0, 0); g.translate(g.textWidth(txt) * kerning, 0); for (int i = 0; i < children.size(); i++) children.get(i).render(g); }
public static ArrayList<BChar> buildString(PApplet app, String str, float wander) { ArrayList<BChar> bchars = new ArrayList<BChar>(str.length()); for (int i = 0, n = str.length(); i < n; i++) { bchars.add(new BChar(str.substring(i, i + 1), app.random(-wander, wander))); if (i > 0) bchars.get(i - 1).addChild(bchars.get(i)); } return bchars; }
public ArrayList getFutureObjectStates() { ArrayList alFutureArchive = new ArrayList(); ArrayList alFutureObjects = cloneArrayList(alStatefulObjects); for (int i = 0; i < 100; i++) { sim.calculateForces(alFutureObjects); alFutureArchive.add(cloneArrayList(alFutureObjects)); } return alFutureArchive; }
/** Update all of the Ball's and draw them */ public void update() { if (balls.size() != 0) { for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.update(); b.attract = kelly; b.drawBall(); } } }
public void draw() { // background(255); fill(255); rect(-4, -4, width + 4, height + 4); for (int i = skaters.size() - 1; i >= 0; i--) { Skater skater = (Skater) skaters.get(i); // if (mousePressed) { skater.trace(mouseX, mouseY); // blob[i].x etc. println(i); } }
// maailmassa on välttämättä myös pisteitä joissa ei ole haaroja, laitetaan niiden päihin // kolikkoja // akseli ulospäin siitä suorasta osasta // paluuarvot paikka1, akseli1, ... ArrayList<PVector> endpoints() { ArrayList<PVector> pts = new ArrayList<PVector>(); for (int z = 0; z < size; z += space + 1) { for (int y = 0; y < size; y += space + 1) { for (int x = 0; x < size; x += space + 1) { PVector pos = head(x, y, z); if (pos != null) { pts.add(pos); pts.add(PVector.sub(pos, new PVector(x, y, z))); } } } } return pts; }
public void draw() { background(0); fill(255); if (!paused) timeline.moveForward(); if (dragging) drawOrbits(timeline.getFutureObjectStates()); else drawOrbits(timeline.getPastObjectStates()); ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); obj.display(); } }
/** If a key is pressed perform the respective actions */ public void keyPressed() { // Add 'stems' to the balls if (keyCode == SHIFT) { stems = !stems; for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.STEM = stems; } } // toggle repaint background else if (key == 'b') REPAINT = !REPAINT; // Empty the ArrayList of Balls else if (key == 'x') balls.clear(); // Add a ball else if (key == 'f') addBall(); }
public int moveForward() { // println("forward!"); intTimeIdx++; // if the future values have already been calculated, just fetch them instead of calculating // them again if (alObjectStateArchive.size() > intTimeIdx) setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); else { // println("calculating..."); sim.calculateForces(alStatefulObjects); alObjectStateArchive.add(cloneArrayList(alStatefulObjects)); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public void calculateForces(ArrayList objects) { for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); ArrayList forces = obj.getForces(); float totalForceX = 0; float totalForceY = 0; for (int j = 0; j < forces.size(); j++) { totalForceX += ((PVector) forces.get(j)).x; totalForceY += ((PVector) forces.get(j)).y; } PVector newAccel = new PVector(totalForceX / obj.getMass(), totalForceY / obj.getMass()); obj.setAcceleration(newAccel); } for (int i = 0; i < objects.size(); i++) { CelestialObject obj1 = (CelestialObject) objects.get(i); float forceX = 0; float forceY = 0; obj1.clearForces(); if (obj1.getClass() == Star.class) { println(obj1.getVelocity()); continue; } for (int j = 0; j < objects.size(); j++) { CelestialObject obj2 = (CelestialObject) objects.get(j); if (i == j) continue; PVector pvDistance = PVector.sub(obj2.getPosition(), obj1.getPosition()); // println("distance: x:" + pvDistance.x + " y:" + pvDistance.y); float distance = sqrt(sq(pvDistance.y) + sq(pvDistance.x)); float angle = degrees(atan2(pvDistance.y, pvDistance.x)); float force = (G * obj1.getMass() * obj2.getMass()) / sq(distance); forceX = force * cos(radians(angle)); forceY = force * sin(radians(angle)); // println("FORCES on " + obj1.getName() + ":" + forceX + "," + forceY); obj1.addForce(new PVector(forceX, forceY)); println(); } } }
public void mouseDragged() { if (paused) { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { dragging = true; PVector pos = obj.getPosition(); pos.x = mouseX; pos.y = mouseY; timeline.reset(); timeline.setCurrentState(objects); sliderTimeline.setValue(0); break; } } } }
public void draw() { if (_pointLists.size() <= 0) return; pushStyle(); noFill(); PVector vec; PVector firstVec; PVector screenPos = new PVector(); int colorIndex = 0; // draw the hand lists Iterator<Map.Entry> itrList = _pointLists.entrySet().iterator(); while (itrList.hasNext()) { strokeWeight(2); stroke(_colorList[colorIndex % (_colorList.length - 1)]); ArrayList curList = (ArrayList) itrList.next().getValue(); // draw line firstVec = null; Iterator<PVector> itr = curList.iterator(); beginShape(); while (itr.hasNext()) { vec = itr.next(); if (firstVec == null) firstVec = vec; // calc the screen pos context.convertRealWorldToProjective(vec, screenPos); vertex(screenPos.x, screenPos.y); } endShape(); // draw current pos of the hand if (firstVec != null) { strokeWeight(8); context.convertRealWorldToProjective(firstVec, screenPos); point(screenPos.x, screenPos.y); } colorIndex++; } popStyle(); }
public void setup() { size(800, 600); smooth(); strokeWeight(1); skaters = new ArrayList(); skaters.add(new Skater()); }
static public ArrayList<File> findFilesInFolder(File folder, String extension, boolean recurse) { ArrayList<File> files = new ArrayList<File>(); if (folder.listFiles() == null) return files; for (File file : folder.listFiles()) { if (file.getName().startsWith(".")) continue; // skip hidden files if (file.getName().endsWith("." + extension)) files.add(file); if (recurse && file.isDirectory()) { files.addAll(findFilesInFolder(file, extension, true)); } } return files; }
public void setup() { size(320, 240); smooth(); // Start with 10 balls balls = new ArrayList(); for (int i = 0; i < 10; i++) { Ball ball = new Ball(random(width), random(height)); balls.add(ball); } }
public void draw() { background(255); // Turn off highlighting for all obstalces for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.highlight(false); } // Act on all boids for (int i = 0; i < boids.size(); i++) { Boid b = (Boid) boids.get(i); b.avoid(obstacles); b.run(); } // Display the obstacles for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.display(); } // Instructions textFont(f); fill(0); text( "Hit space bar to toggle debugging lines.\nClick the mouse to generate a new boids.", 10, height - 30); }
/** Add a ball to the ArrayList at current mouse position */ public void addBall() { // Store current mouse position x1 = mouseX; y1 = mouseY; // Get a random offset for the ball int xDiff = (int) random(-10, 10); int yDiff = (int) random(-10, 10); // Add the mouse's (x,y) and the random offset int x = mouseX + xDiff; int y = mouseY + yDiff; // Create new ball Ball b = new Ball(x, y, ballSize); b.STEM = stems; b.attract = kelly; balls.add(b); }
public int moveBackward() { intTimeIdx--; if (intTimeIdx < 0) { intTimeIdx = 0; } else { // println("before:" + ((CelestialObject)alStatefulObjects.get(0)).getPosition().x + // "," + ((CelestialObject)alStatefulObjects.get(0)).getPosition().y); setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); // println("before:" + ((CelestialObject)alStatefulObjects.get(0)).getPosition().x + // "," + ((CelestialObject)alStatefulObjects.get(0)).getPosition().y); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public void addPoint(long handId, PVector handPoint) { ArrayList curList = getPointList(handId); curList.add(0, handPoint); if (curList.size() > _maxPoints) curList.remove(curList.size() - 1); }
protected String[] getSketchParams(boolean presenting) { ArrayList<String> params = new ArrayList<String>(); // It's dangerous to add your own main() to your code, // but if you've done it, we'll respect your right to hang yourself. // http://processing.org/bugs/bugzilla/1446.html if (build.getFoundMain()) { params.add(build.getSketchClassName()); } else { params.add("processing.core.PApplet"); // get the stored device index (starts at 0) int runDisplay = Preferences.getInteger("run.display"); // If there was a saved location (this guy has been run more than once) // then the location will be set to the last position of the sketch window. // This will be passed to the PApplet runner using something like // --location=30,20 // Otherwise, the editor location will be passed, and the applet will // figure out where to place itself based on the editor location. // --editor-location=150,20 if (editor != null) { // if running processing-cmd, don't do placement GraphicsDevice editorDevice = editor.getGraphicsConfiguration().getDevice(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] devices = ge.getScreenDevices(); // Make sure the display set in Preferences actually exists GraphicsDevice runDevice = editorDevice; if (runDisplay >= 0 && runDisplay < devices.length) { runDevice = devices[runDisplay]; } else { runDevice = editorDevice; for (int i = 0; i < devices.length; i++) { if (devices[i] == runDevice) { runDisplay = i; break; // Don't set the pref, might be a temporary thing. Users can // open/close Preferences to reset the device themselves. // Preferences.setInteger("run.display", runDisplay); } } } Point windowLocation = editor.getSketchLocation(); // if (windowLocation != null) { // // could check to make sure the sketch location is on the device // // that's specified in Preferences, but that's going to be annoying // // if you move a sketch to another window, then it keeps jumping // // back to the specified window. //// Rectangle screenRect = //// runDevice.getDefaultConfiguration().getBounds(); // } if (windowLocation == null) { if (editorDevice == runDevice) { // If sketches are to be shown on the same display as the editor, // provide the editor location so the sketch's main() can place it. Point editorLocation = editor.getLocation(); params.add( PApplet.ARGS_EDITOR_LOCATION + "=" + editorLocation.x + "," + editorLocation.y); } else { // The sketch's main() will set a location centered on the new // display. It has to happen in main() because the width/height // of the sketch are not known here. // Set a location centered on the other display // Rectangle screenRect = // runDevice.getDefaultConfiguration().getBounds(); // int runX = // params.add(PApplet.ARGS_LOCATION + "=" + runX + "," + runY); } } else { params.add(PApplet.ARGS_LOCATION + "=" + windowLocation.x + "," + windowLocation.y); } params.add(PApplet.ARGS_EXTERNAL); } params.add(PApplet.ARGS_DISPLAY + "=" + runDisplay); if (presenting) { params.add(PApplet.ARGS_FULL_SCREEN); // if (Preferences.getBoolean("run.present.exclusive")) { // params.add(PApplet.ARGS_EXCLUSIVE); // } params.add(PApplet.ARGS_STOP_COLOR + "=" + Preferences.get("run.present.stop.color")); params.add(PApplet.ARGS_BGCOLOR + "=" + Preferences.get("run.present.bgcolor")); } params.add(build.getSketchClassName()); params.add(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath()); // Adding sketch path in the end coz it's likely to // contain spaces and things go wrong on UNIX systems. } // String outgoing[] = new String[params.size()]; // params.toArray(outgoing); // return outgoing; return params.toArray(new String[0]); }
protected String[] getMachineParams() { ArrayList<String> params = new ArrayList<String>(); // params.add("-Xint"); // interpreted mode // params.add("-Xprof"); // profiler // params.add("-Xaprof"); // allocation profiler // params.add("-Xrunhprof:cpu=samples"); // old-style profiler // TODO change this to use run.args = true, run.args.0, run.args.1, etc. // so that spaces can be included in the arg names String options = Preferences.get("run.options"); if (options.length() > 0) { String pieces[] = PApplet.split(options, ' '); for (int i = 0; i < pieces.length; i++) { String p = pieces[i].trim(); if (p.length() > 0) { params.add(p); } } } // params.add("-Djava.ext.dirs=nuffing"); if (Preferences.getBoolean("run.options.memory")) { params.add("-Xms" + Preferences.get("run.options.memory.initial") + "m"); params.add("-Xmx" + Preferences.get("run.options.memory.maximum") + "m"); } if (Base.isMacOS()) { params.add("-Xdock:name=" + build.getSketchClassName()); // params.add("-Dcom.apple.mrj.application.apple.menu.about.name=" + // sketch.getMainClassName()); } // sketch.libraryPath might be "" // librariesClassPath will always have sep char prepended params.add( "-Djava.library.path=" + build.getJavaLibraryPath() + File.pathSeparator + System.getProperty("java.library.path")); params.add("-cp"); params.add(build.getClassPath()); // params.add(sketch.getClassPath() + // File.pathSeparator + // Base.librariesClassPath); // enable assertions // http://dev.processing.org/bugs/show_bug.cgi?id=1188 params.add("-ea"); // PApplet.println(PApplet.split(sketch.classPath, ':')); String outgoing[] = new String[params.size()]; params.toArray(outgoing); // PApplet.println(outgoing); // PApplet.println(PApplet.split(outgoing[0], ":")); // PApplet.println(); // PApplet.println("class path"); // PApplet.println(PApplet.split(outgoing[2], ":")); return outgoing; // return (String[]) params.toArray(); // System.out.println("sketch class path"); // PApplet.println(PApplet.split(sketch.classPath, ';')); // System.out.println(); // System.out.println("libraries class path"); // PApplet.println(PApplet.split(Base.librariesClassPath, ';')); // System.out.println(); }
public void removeChild(BChar childNode) { children.remove(childNode); }