Bitmap/Flood fill: Difference between revisions
Content added Content deleted
m (→{{header|Processing Python mode}}: style fix: constants and globals assigned earlier) |
|||
Line 1,942: | Line 1,942: | ||
import java.util.Queue; |
import java.util.Queue; |
||
import java.util.LinkedList; |
import java.util.LinkedList; |
||
PImage img; |
PImage img; |
||
int tolerance; |
int tolerance, start_time; |
||
color fill_color; |
color fill_color; |
||
boolean allowed; |
boolean allowed; |
||
void setup() { |
void setup() { |
||
size(600, 400); |
size(600, 400); |
||
Line 1,959: | Line 1,959: | ||
text("Right click to reset", 100, height-10); |
text("Right click to reset", 100, height-10); |
||
} |
} |
||
void draw() { |
void draw() { |
||
if (allowed) { |
if (allowed) { |
||
Line 1,968: | Line 1,968: | ||
} |
} |
||
} |
} |
||
void mousePressed() { |
void mousePressed() { |
||
img.loadPixels(); |
img.loadPixels(); |
||
Line 1,974: | Line 1,974: | ||
img.updatePixels(); |
img.updatePixels(); |
||
allowed = true; |
allowed = true; |
||
start_time = millis(); |
|||
if(mouseButton == RIGHT) img = loadImage("image.png"); |
|||
} |
} |
||
void mouseWheel(MouseEvent event) { |
void mouseWheel(MouseEvent event) { |
||
float e = event.getCount(); |
float e = event.getCount(); |
||
tolerance += 2*e; |
tolerance += 2*e; |
||
if(tolerance > |
if (tolerance > 128) tolerance = 128; |
||
if (tolerance < 0) tolerance = 0; |
if (tolerance < 0) tolerance = 0; |
||
allowed = true; |
allowed = true; |
||
} |
} |
||
void flood(int x, int y) { |
void flood(int x, int y) { |
||
color target_color = img.pixels[pixel_position(mouseX, mouseY)]; |
color target_color = img.pixels[pixel_position(mouseX, mouseY)]; |
||
if (target_color != fill_color) { |
|||
Queue<Point> queue = new LinkedList<Point>(); |
|||
queue |
Queue<Point> queue = new LinkedList<Point>(); |
||
queue.add(new Point(x, y)); |
|||
while (!queue.isEmpty()) { |
|||
Point p = queue.remove(); |
|||
if (check(p.x, p.y, target_color)) { |
|||
if (check(p.x, p.y, target_color)) { |
|||
queue.add(new Point(p.x, p.y |
queue.add(new Point(p.x, p.y-1)); |
||
queue.add(new Point(p.x |
queue.add(new Point(p.x, p.y+1)); |
||
queue.add(new Point(p.x |
queue.add(new Point(p.x-1, p.y)); |
||
queue.add(new Point(p.x+1, p.y)); |
|||
} |
|||
} |
} |
||
} |
} |
||
} |
} |
||
int pixel_position(int x, int y) { |
int pixel_position(int x, int y) { |
||
return x + (y * img.width); |
return x + (y * img.width); |
||
} |
} |
||
boolean check(int x, int y, color target_color) { |
boolean check(int x, int y, color target_color) { |
||
if (x < 0 || y < 0 || y >= img.height || x >= img.width) return false; |
if (x < 0 || y < 0 || y >= img.height || x >= img.width) return false; |
||
int pp = img.pixels[pixel_position(x, y)]; |
int pp = img.pixels[pixel_position(x, y)]; |
||
boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance |
boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance |
||
&& abs( red(target_color)- red(pp)) < tolerance |
|||
&& abs( blue(target_color)- blue(pp)) < tolerance); |
|||
if (!test_tolerance) return false; |
if (!test_tolerance) return false; |
||
img.pixels[pixel_position(x, y)] = fill_color; |
img.pixels[pixel_position(x, y)] = fill_color; |
||
return true; |
return true; |
||
} |
|||
</lang> |
|||
==={{header|Processing Python mode}}=== |
==={{header|Processing Python mode}}=== |