problem with class variables inside a "for" sequence
in
Programming Questions
•
7 months ago
Hi,
I would like to change every dark pixel of an image in moving bubbles, using a class.
after loading an image, I do a "for" sequence to get the current color of each pixel and convert it to a greyscale, and then I load my class. The bubble size is mapped to the greyscale value.
What isn't working is the vector part to make my "bubble" class move: I want all the bubbles to move in random directions, but they don't move at all.
I think I messed up with my vector variables inside the "for" sequence, but I can't figure out how to make this work... Any idea??
(I am using toxiclibs library for the vectors)
my sketch:
my "bubble" class :
Thanks!
I would like to change every dark pixel of an image in moving bubbles, using a class.
after loading an image, I do a "for" sequence to get the current color of each pixel and convert it to a greyscale, and then I load my class. The bubble size is mapped to the greyscale value.
What isn't working is the vector part to make my "bubble" class move: I want all the bubbles to move in random directions, but they don't move at all.
I think I messed up with my vector variables inside the "for" sequence, but I can't figure out how to make this work... Any idea??
(I am using toxiclibs library for the vectors)
my sketch:
- import toxi.geom.*;
PImage img;
bubble myBubble;
void setup() {
size(600, 800);
smooth();
img = loadImage("antoine-small-edit.jpg");
}
void draw() {
background(255);
float threshold = 127;
for (int gridX = 0; gridX < img.width; gridX++) {
for (int gridY = 0; gridY < img.height; gridY++) {
// grid position + tile size
float tileWidth = width / (float)img.width;
float tileHeight = height / (float)img.height;
float posX = tileWidth*gridX;
float posY = tileHeight*gridY;
int loc = gridX + gridY*img.width;
// Test the brightness against the threshold
if (brightness(img.pixels[loc]) > threshold) {
img.pixels[loc] = color(255); // White
}
else {
// get current color
color c = img.pixels[gridY*img.width+gridX];
// greyscale conversion
int greyscale =round(red(c)*0.222+green(c)*0.707+blue(c)*0.071);
//map bubble size to greyscale value
float r2 = map(greyscale, 0, 255, 25, 0);
r2 = r2*0.5;
Vec3D origin = new Vec3D (posX, posY, 0);
//load class
myBubble = new bubble (origin, r2, r2);
myBubble.run();
}
}
}
}
my "bubble" class :
class bubble {
float dimX = 0;
float dimY = 0;
Vec3D loc = new Vec3D (0, 0, 0);
Vec3D speed = new Vec3D(random(-2, 2), random(-2, 2), 0);
bubble (Vec3D _loc, float _dimX, float _dimY) {
loc = _loc;
dimX = _dimX;
dimY = _dimY;
}
void run() {
display();
move();
}
void display() {
noStroke();
fill(0);
ellipse(loc.x, loc.y, dimX, dimY);
}
void move() {
loc.addSelf(speed);
}
}
Thanks!
1