Bitmap/Flood fill: Difference between revisions
Content added Content deleted
Line 2,023: | Line 2,023: | ||
<lang Python>from collections import deque |
<lang Python>from collections import deque |
||
⚫ | |||
fill_color = color(250, 0, 0) |
fill_color = color(250, 0, 0) |
||
⚫ | |||
allowed = False |
allowed = False |
||
Line 2,042: | Line 2,042: | ||
if allowed: |
if allowed: |
||
image(img, 0, 0, width, height) |
image(img, 0, 0, width, height) |
||
text("Tolerance = {} (Use mouse wheel to change)".format( |
text("Tolerance = {} (Use mouse wheel to change)".format( |
||
tolerance), 100, height - 30) |
|||
text("Right click to reset", 100, height - 10) |
text("Right click to reset", 100, height - 10) |
||
allowed = False |
allowed = False |
||
def mousePressed(): |
def mousePressed(): |
||
global |
global allowed, img |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if mouseButton == RIGHT: |
if mouseButton == RIGHT: |
||
img = loadImage("image.png") |
img = loadImage("image.png") |
||
else: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
def mouseWheel(event): |
def mouseWheel(event): |
||
Line 2,059: | Line 2,061: | ||
e = event.getCount() |
e = event.getCount() |
||
tolerance += 2 * e |
tolerance += 2 * e |
||
if tolerance > |
if tolerance > 128: |
||
tolerance = |
tolerance = 128 |
||
if tolerance < 0: |
if tolerance < 0: |
||
tolerance = 0 |
tolerance = 0 |
||
Line 2,067: | Line 2,069: | ||
def flood(x, y): |
def flood(x, y): |
||
target_color = img.pixels[pixel_position(mouseX, mouseY)] |
target_color = img.pixels[pixel_position(mouseX, mouseY)] |
||
if target_color != fill_color: |
|||
queue = deque() |
|||
queue |
queue = deque() |
||
queue.append((x, y)) |
|||
while len(queue) > 0: |
|||
while len(queue) > 0: |
|||
p_x, p_y = queue.popleft() |
|||
if (check(p_x, p_y, target_color)): |
|||
queue.append((p_x, p_y |
queue.append((p_x, p_y - 1)) |
||
queue.append((p_x |
queue.append((p_x, p_y + 1)) |
||
queue.append((p_x |
queue.append((p_x - 1, p_y)) |
||
queue.append((p_x + 1, p_y)) |
|||
def pixel_position(x, y): |
def pixel_position(x, y): |