Hello,
I hope someone is able and willing to aid me in figuring out how to approach a concept I want to try. The idea is rather simple: I want particles to interact with a loaded image.
I have a Processing sketch with a Particle class. This particle is essentially a moving point calculated with a radius and an angle from the center of the sketch. With an array I can make many particles that rotate around the center of the sketch with movement based on a noise-feed. I have managed this.
I want the particles to interact based on the brightness-values of pixels of a loaded black and white .png-image. For example changing the color or increasing/decreasing the scale of the particle:
If the pixel of the image is white -> color = red;
If the pixel of the image is black -> color = blue;
This has proven to be more difficult in practice than I had expected. Right now I have a sketch where I load the image, I read all its pixels, and store the x and y location of the pixels with a brightness value higher than 2 in an ArrayList of PVectors. Iâm trying to let the Particle class interact with it but it doesnât work the way I want it to work.
int amount = 200;
float maxUpperIncr = 0.001;
PImage letter_img;
public ArrayList<PVector> spots;
Balls[] b = new Balls[amount];
void setup() {
size(1000, 1000);
background(0);
spots = new ArrayList<PVector>();
letter_img = loadImage("aphex_l.png");
for (int y = 0; y < letter_img.height; y++) {
for (int x = 0; x < letter_img.width; x++) {
int index = x + y * width;
color c = letter_img.pixels[index];
float b = brightness(c);
if (b>2) {
spots.add(new PVector(x, y));
}
}
}
//Load class Balls
for (int i = 0; i<amount; i++) {
b[i] = new Balls(0, random(10), random(-maxUpperIncr, maxUpperIncr), random(1000), random(1));
}
println(spots.size());
}
void draw() {
background(0);
for (int i = 0; i!=amount; i++) {
b[i].show();
b[i].calc();
//println(b[i].upperIncr);
}
}
class Balls {
float num = 0;
float a;
float r = 0;
PVector loc;
float x;
float y;
float incr;
float Rincr = 1;
float scl = 10;
float upper;
float upperIncr;
float n;
float rad = 20;
PVector place;
Balls(float _a, float _num, float _upperIncr, float _upper, float _incr) {
a = _a;
upperIncr = _upperIncr;
upper = _upper;
num = _num;
incr = _incr;
int randomSpot = int(random(0, spots.size()));
place = spots.get(randomSpot);
}
void show() {
imageMode(CENTER);
pushMatrix();
translate(width/2, height/2);
noStroke();
fill(255, 255, 255, 255);
rectMode(CENTER);
rect(x, y, scl, scl);
popMatrix();
}
void calc() {
//ANGLE
num += incr;
a = radians(num);
//RADIUS
upper += upperIncr;
n = map((noise(upper)), 0, 1, -500, 500);
x = n * cos(a);
y = n * sin(a);
for (int i = 0; i < spots.size(); i++) {
if (int(x) == spots.get(i).x) {
//println("RIGHT NOW - 30");
} else {
//print("NOT NOW -2");
}
}
}
I have an image with 1000x1000 pixels. The size of the ArrayList after loading all the bright pixels is 176.252, which is obviously huge. What I tried initially (as you can somewhat see above in the âcalcâ function of the Particle class) was compare the x- and y-location of one Particle to all of these 176.252 spots to see if they match and therefor if itâs âcoveringâ a white pixel. This didnât go very well as you might imagine, as it has to do millions of comparisons if I want to start working with more than 1 particle.
What I am now trying to understand is how I should go about doing this. Should I work with coloured rectangles in stead of pixels? Is there a more clever way to achieve what Iâm trying to do?
I hope my idea and code are clear. Any help is much appreciated. Thanks in advance.