Bitmap/Flood fill: Difference between revisions
→{{header|Processing}}
Line 1,942:
import java.util.Queue;
import java.util.LinkedList;
PImage img;
int tolerance;
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();
flood(mouseX, mouseY);
img.updatePixels();
Line 1,976:
if(mouseButton == RIGHT) img = loadImage("image.png");
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
Line 1,984:
allowed = true;
}
void flood(int x, int y) {
color target_color = img.pixels[pixel_position(mouseX, mouseY)];
Queue<Point> queue = new LinkedList<Point>();
queue.add(new Point(x, y));
while (!queue.isEmpty()) {
Point p = queue.remove();
if (check(
queue.add(new Point(p.x, p.y-1));
queue.add(new Point(p.x, p.y+1));
Line 2,001 ⟶ 2,000:
}
}
int pixel_position(int x, int y) {
return x + (y * img.width);
}
boolean check(
if (x < 0 || y < 0 || y >= img.height || x >= img.width
int pp = img.pixels[pixel_position(x, y)];
boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance
Line 2,014 ⟶ 2,013:
if (!test_tolerance) return false;
img.pixels[pixel_position(x, y)] = fill_color;
return true;
}</lang>
=={{header|PureBasic}}==
|