I have a 2D array of pixels, size is WIDTH × HEIGHT.

Each coordinate will get a random int between 0 and 255.

On each draw loop, I call the `move()`

function, which gets the coordinates of the lowest neighbor (up, down, left, right, if lower than the source coordinates)

For example, in this table, the middle would be swapped with the bottom.

- | 181 | - |
---|---|---|

212 | 127 | 63 |

- | 41 | - |

However, in this example, the middle would swap with itself (unchanged) because it itself is the lowest:

- | 147 | - |
---|---|---|

32 | 12 | 91 |

- | 127 | - |

Processing doesn't seem to always commit the changes to the 2D array. Running the code as-is seems to leave quite a bit of artifacts in the form of squiggly lines.

Anyone know what's up?

`int[][] pix; void setup() { size(600, 600); pix = new int[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { pix[i][j] = floor(random(256)); } } } void draw() { swaps = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { move(i,j); } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { set(i,j,color(pix[i][j])); } } } void move(int x, int y){ boolean left = false; boolean right = false; boolean up = false; boolean down = false; int first = pix[x][y]; int xTarget = x; int yTarget = y; int min = pix[x][y]; if(x == 0) left = true; if(y == 0) up = true; if(x == width-1) right = true; if(y == height-1) down = true; if(!left){ if(pix[x-1][y] < min){ xTarget = x-1; yTarget = y; min = pix[x-1][y]; } } if(!right){ if(pix[x+1][y] < min){ xTarget = x+1; yTarget = y; min = pix[x+1][y]; } } if(!up){ if(pix[x][y-1] < min){ xTarget = x; yTarget = y-1; min = pix[x][y-1]; } } if(!down){ if(pix[x][y+1] < min){ xTarget = x; yTarget = y+1; min = pix[x][y+1]; } } pix[xTarget][yTarget] = first; pix[x][y] = min; } `

[link] [comments]