flood fill algorithm help making one
in
Programming Questions
•
10 months ago
how to make a fill bucket tool
I actually already found some code but is is not mine and I don't even understand it
I got from openProcessing here
http://www.
openprocessing.org/sketch/67453
I also found one done by
PhiLho
a user who has helped me about two or three times around here
the code from PhiLho can be found http://processing.org/discourse/beta/num_1138719727.html
truthfully I have not looked through PhiLhos code as I just found and I thought wow that is long as it is 3 .PDE files I just wanted a short method like the one below
so if you have any suggestions I will be gratefull
I understand the code below for most part I know it looks at the pixel
where the mouse has clicked and checks that its color is not the same as the fill color and changes it if not, then does that for the surrounding pixels
the part I don't get is
PXList and what it is used for and the shifting of vectors
I'm very at shifting I read about on the Processing site but it is still confusing
remember I have just started doing stuff on processing just a bit over a week ago
Copy code
- public void Fill(int x, int y) {
- if(get(x,y) != SelectedColour) {
- int PXList[] = new int[2 * height * width];
- int PXListSize = 0;
- PXList[0] = (y << 16) + x;
- PXListSize = 1;
- set(x, y, SelectedColour);
- while (PXListSize > 0){
- x = PXList[0] & 0xffff;
- y = (PXList[0] >> 16) & 0xffff;
- PXListSize--;
- PXList[0] = PXList[PXListSize];
- if (x > 0) {
- if ((get(x-1, y) == FillColour)) {
- set(x-1, y, SelectedColour);
- PXList[PXListSize] = (y << 16) + x-1;
- PXListSize++;
- }
- }
- if (y > 0) {
- if ((get(x, y-1) == FillColour)) {
- set(x, y-1, SelectedColour);
- PXList[PXListSize] = ((y-1) << 16) + x;
- PXListSize++;
- }
- }
- if (x < width) {
- if ((get(x+1, y) == FillColour)) {
- set(x+1, y, SelectedColour);
- PXList[PXListSize] = (y << 16) + x+1;
- PXListSize++;
- }
- }
- if (y < height) {
- if ((get(x, y+1) == FillColour)) {
- set(x, y+1, SelectedColour);
- PXList[PXListSize] = ((y+1) << 16) + x;
- PXListSize++;
- }
- }
- }
- }
- }
1