Side note to the above code: it is better to avoid drawing the same random number, there is a simple way for that: shuffle an array of increasing numbers.
- PImage src;
- PImage[] images;
- int cellSize;
- int cellNbX, cellNbY;
- int[] numbers;
-
- void setup() {
- size(640, 480);
- frameRate(10);
- src = loadImage("http://athena.cornell.edu/images/wallpaper/MER_640_480.jpg");
- cellSize = 80;
- cellNbX = src.width / cellSize;
- cellNbY = src.height / cellSize;
- images = new PImage[cellNbX * cellNbY];
- for (int i = 0; i < cellNbX; i++) {
- for (int j = 0; j < cellNbY; j++) {
- images[i + j * cellNbX] = src.get(i * cellSize, j * cellSize, cellSize, cellSize);
- }
- }
- numbers = new int[images.length];
- for (int i = 0; i < numbers.length; i++) numbers[i] = i;
- shuffle();
- // println(numbers);
- // println(sort(numbers));
- }
-
- int c;
- void draw() {
- if (c == numbers.length) { noLoop(); println("Done"); return; } else { print(c + " "); }
- int r = numbers[c++]; print(r + " - ");
- image(images[r], cellSize * (r % cellNbX), cellSize * (r / cellNbX));
- /*
- fill(0);
- text(c + " " + r, cellSize * (r % cellNbX) + cellSize / 2, cellSize * (r / cellNbX) + cellSize / 2);
- println((cellSize * (r % cellNbX) + cellSize / 2) + ", " + (cellSize * (r / cellNbY) + cellSize / 2));
- //*/
- }
-
- void shuffle()
- {
- for (int i = numbers.length - 1; i > 0; i--)
- {
- int j = int(random(i + 1));
- // Swap
- int t = numbers[i];
- numbers[i] = numbers[j];
- numbers[j] = t;
- }
- }
I also translated the variables named to be better understandable by everybody.