xxxxxxxxxx
/* @pjs preload="emma.jpg", "obama.jpg", "clint.jpg", "fuse.jpg", "momo.jpg", "toffee.jpg"; */
/*
Art gallery painter
Maps the image's brightness to determine the stroke's direction and the color's hue.
Controls:
- Mouse click to switch to the next image.
Author:
Jason Labbe
Site:
jasonlabbe3d.com
*/
// Add your image here and on line 1 to preload it properly.
// Works best with images that don't exceed 700px.
String[] imgNames = {"emma.jpg", "obama.jpg", "clint.jpg", "fuse.jpg", "momo.jpg", "toffee.jpg"};
PImage img;
int imgIndex = -1;
PGraphics pg;
PGraphics pgDebug;
int distThreshold;
int spawn1 = 150;
int spawn2 = 200;
float opacity;
ArrayList<Bob> bobs = new ArrayList<Bob>();
bool debug = false;
bool stop = false;
void setup() {
nextImage();
}
void draw() {
if (!stop) {
pgDebug.beginDraw();
pgDebug.image(img, 0, 0);
if (frameCount == 1) {
image(img, 0, 0);
} else if (frameCount == 40) {
distThreshold = 30;
opacity = 5;
bobs.clear();
int spacing = 20;
for (int x = 0; x < width + spacing; x+=spacing) {
for (int y = 0; y < height + spacing; y+=spacing) {
Bob bob = new Bob();
bob.pos.set(x, y);
bob.vel.mult(0.4);
bobs.add(bob);
}
}
} else if (frameCount > 100) {
stop = true;
}
for (int i = 0; i < bobs.size(); i++) {
pg.beginDraw();
pg.noFill();
Bob bob = bobs.get(i);
int pixelIndex = int(bob.pos.x) + (int(bob.pos.y) * img.width);
color pixelColor = img.pixels[pixelIndex];
bob.pos.add(bob.vel);
if (bob.pos.x < 0 || bob.pos.x > width) {
bob.vel.x *= -1;
bob.pos.x = constrain(bob.pos.x, 0, width);
}
if (bob.pos.y < 0 || bob.pos.y > height) {
bob.vel.y *= -1;
bob.pos.y = constrain(bob.pos.y, 0, height);
}
ArrayList<PVector> neighbors = new ArrayList<PVector>();
neighbors.add(bob.pos);
pgDebug.stroke(0, 255, 0, 50);
pgDebug.strokeWeight(1);
for (int j = 0; j < bobs.size(); j++) {
if (i == j) {
continue;
}
Bob otherBob = bobs.get(j);
float distance = dist(bob.pos.x, bob.pos.y, otherBob.pos.x, otherBob.pos.y);
if (distance < distThreshold) {
neighbors.add(otherBob.pos);
pg.strokeWeight(map(distance, 0, distThreshold, 2.0, 0));
pg.stroke(red(pixelColor), green(pixelColor), blue(pixelColor), opacity);
if (debug) {
pgDebug.line(bob.pos.x, bob.pos.y, otherBob.pos.x, otherBob.pos.y);
}
}
}
if (neighbors.size() > 2) {
pg.beginShape();
for (int j = 0; j < neighbors.size(); j++) {
pg.curveVertex(neighbors.get(j).x, neighbors.get(j).y);
}
pg.endShape(OPEN);
}
}
pg.endDraw();
}
image(pg, 0, 0);
if (debug) {
pgDebug.strokeWeight(4);
pgDebug.stroke(255, 0, 0);
for (int i = 0; i < bobs.size(); i++) {
pgDebug.point(bobs.get(i).pos.x, bobs.get(i).pos.y);
}
}
pgDebug.endDraw();
if (debug) {
image(pgDebug, 0, 0);
}
if (mousePressed && mouseButton == RIGHT) {
image(img, 0, 0);
}
}
void mousePressed() {
if (mouseButton == LEFT) {
nextImage();
} else if (mouseButton == CENTER) {
debug = !debug;
}
}
void nextImage() {
// Reset values.
opacity = 1;
distThreshold = 100;
background(200);
bobs.clear();
frameCount = 0;
stop = false;
// Load the next image.
imgIndex++;
if (imgIndex >= imgNames.length) {
imgIndex = 0;
}
img = loadImage(imgNames[imgIndex]);
img.loadPixels();
size(img.width, img.height);
pg = createGraphics(img.width, img.height);
pgDebug = createGraphics(img.width, img.height);
int spacing = 50;
for (int x = 0; x < width + spacing; x+=spacing) {
for (int y = 0; y < height + spacing; y+=spacing) {
Bob bob = new Bob();
bob.pos.set(x, y);
bobs.add(bob);
}
}
}
class Bob {
PVector pos;
PVector vel;
Bob() {
this.pos = new PVector(random(width), random(height));
this.vel = PVector.random2D();
this.vel.mult(random(4));
}
}