Bitmap/Flood fill: Difference between revisions

+ D entry
(+ D entry)
Line 589:
}
</lang>
 
=={{header|D}}==
This version uses the second implementation of the Bitmap Task, matches exact colours only, and is derived from the Go version (but it's iterative because unlike Go the D stack is not segmented, so it causes stack overflow if you don't set the stack large enough).
 
<lang d>import bitmap2: Color, Image;
import std.array;
 
void floodFill(Image img, in uint x, in uint y, in Color color)
/*pure nothrow*/ in {
assert (y < img.ny && x < img.nx);
} body {
immutable target = img[y][x];
static struct Pos { uint x, y; }
auto stack = [Pos(x, y)];
 
while (!stack.empty) {
immutable p = stack.back;
stack.popBack();
if (p.y < img.ny && p.x < img.nx && img[p.y][p.x] == target) {
img[p.y][p.x] = color;
stack.assumeSafeAppend(); // Not pure nor nothrow.
stack ~= Pos(p.x - 1, p.y);
stack ~= Pos(p.x + 1, p.y);
stack ~= Pos(p.x, p.y - 1);
stack ~= Pos(p.x, p.y + 1);
}
}
}
 
void main() {
auto img = new Image();
img.loadBinaryPPM("Unfilledcirc.ppm");
img.floodFill(200, 200, Color(127, 0, 0));
img.saveBinaryPPM("flooded.ppm");
}</lang>
 
=={{header|E}}==