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