import processing.video.*;
ArrayList <Searcher> image_searchers = new ArrayList <Searcher> ();
Capture cam;
PGraphics video_holder;
void setup(){
size(640, 480);
String[] cameras = Capture.list();
if (cameras.length > 0) {
cam = new Capture(this, cameras[16]);
cam.start();
video_holder = createGraphics(width,height);
}
}
void draw(){
if (cam.available() == true) {
cam.read();
}
video_holder.beginDraw();
video_holder.image(cam, 0, 0);
video_holder.endDraw();
image(video_holder,0,0);
for(int i = 0; i < image_searchers.size(); i++){
Searcher is = image_searchers.get(i);
is.display(); is.update();
}
}
//function to allow for a searcher to be added on mouse click
void mouseClicked() {
color c = video_holder.get(mouseX, mouseY);
//check to see if the position of the mouse is in a white or black pixel
//only add if a white pixel is clicked on
if(brightness(c) > 100){
image_searchers.add(new Searcher(mouseX, mouseY));
}
}
class Searcher {
ArrayList <PVector> position = new ArrayList <PVector> ();
ArrayList <PVector> collisions = new ArrayList <PVector> ();
FloatList pscale = new FloatList();
Searcher (float x, float y) {
float xpos = x;
float ypos = y;
position.add(new PVector(xpos,ypos,0));
pscale.append(15);
}
void display() {
noStroke();
for(int i = 0; i < position.size()-1; i++){
PVector p = position.get(i);
PVector p2 = position.get(i+1);
stroke(0,255,255,100);
line(p.x,p.y,p2.x,p2.y);
float sc = pscale.get(i);
fill(255,100);
noStroke();
ellipse(p.x,p.y,sc,sc);
pscale.set(i,sc*0.99);
}
for(int i = 0; i < collisions.size(); i++){
PVector p = collisions.get(i);
fill(255,0,0);
noStroke();
ellipse(p.x,p.y,5,5);
}
}
void update() {
PVector p = position.get(position.size()-1);
float ranx = random(-3,3);
float rany = random(-3,3);
color cx = video_holder.get(int(p.x + ranx), int(p.y));
color cy = video_holder.get(int(p.x), int(p.y+rany));
float col = 0;
if(brightness(cx) < 100){
ranx = -ranx;
col = 1;
}
if(brightness(cy) < 100){
rany = -rany;
col = 1;
}
if(col == 1){
collisions.add(new PVector(p.x,p.y,0));
}
float newx = p.x + ranx;
float newy= p.y + rany;
position.add(new PVector(newx,newy,0));
pscale.append(15);
}
}