class LightPath extends ArrayList { //---------------------------- // variables int maxPoints = 500; float ptDistance = 16.0; ArrayList bulbs; //---------------------------- LightPath() { bulbs = new ArrayList(); } //---------------------------- synchronized void update(int _newX, int _newY) { PVector currPt = new PVector(_newX, _newY); if (size() < 1) { // simply add the first point add(currPt); } else { // add interpolating points up until the current point over the distance from the last path point PVector lastPt = (PVector)get(size() - 1); float distance = lastPt.dist(currPt); for (int i=0; i < (distance/ptDistance); i++) { float a = atan2(currPt.y - lastPt.y, currPt.x - lastPt.x); if (random(1) < 0.5) a += PI; add(new PVector((currPt.x - lastPt.x)/distance*ptDistance*(i+1) + lastPt.x, (currPt.y - lastPt.y)/distance*ptDistance*(i+1) + lastPt.y), a); } } // remove extra points if there are too many while (size() > maxPoints) { remove(0); } } //---------------------------- void draw() { drawPath(); drawBulbs(); } //---------------------------- void drawPath() { // render the path as a curve noFill(); stroke(#797979); beginShape(); PVector currPt; for (int i=0; i < size(); i++) { currPt = (PVector)get(i); curveVertex(currPt.x, currPt.y); } endShape(); } //---------------------------- void drawBulbs() { // render the bulbs along the path LightBulb currBulb; for (int i=0; i < bulbs.size(); i++) { currBulb = (LightBulb)bulbs.get(i); currBulb.draw(); } } //---------------------------- void add(PVector _newPt) { add(_newPt, 0); } //---------------------------- void add(PVector _newPt, float _angle) { // add the point to the list super.add(_newPt); // add a new bulb at the point position bulbs.add(new LightBulb(_newPt.x, _newPt.y, _angle)); } //---------------------------- Object remove(int _index) { bulbs.remove(_index); return super.remove(_index); } //---------------------------- void clear() { bulbs.clear(); super.clear(); } }