image mosaic - random x,y position particle movement
in
Programming Questions
•
3 months ago
Hi,
I've been looking at some code previously written and uploaded on this forum to load an image and create a mosaic.
Currently the code creates mosaic identical to the image which is correct. What i'm hoping to now do is add some animation, almost like a particle animation. therefore all of the generated mosaic pixels would randomly move around the stage very slowly, then when a button is pressed they all come together to form the mosaic image.
I guess i need two functions: 1 for the random movement and 1 for the formation?
Can anyone help with this?
A good example of motion would be this - Brownian motion - but a lot slower
Here' the code:
PImage img1;
int cellSize = 2;
void setup() {
size(800, 800); // frame is 500 x 500
// Make a new instance of a PImage by loading an image file
img1 = loadImage("chap.jpg"); // stormtrooper image, 300 x 300
img1.loadPixels();
println(img1.width + " " + img1.height);
}
void draw() {
//image(img1, 0,0, 150,150);
pushMatrix();
// move to the center of the screen
translate(width/2, height/2);
// rotate 45 degrees
rotate(radians(0));
// move up from there to center the image
translate(-img1.width/2,-img1.height/2);
for (int y = 0; y < img1.height; y += cellSize ) {
for (int x = 0; x < img1.width; x += cellSize) {
int i = (x+cellSize/2) + (y+cellSize/2)*img1.width;
color c = color(img1.pixels[i]);
fill(c); // make each pixel black and white
// stroke(100); // bounded by a box, dark grey
//noStroke();
rect(x, y, cellSize, cellSize);
}
}
popMatrix();
}
Thanks
1