Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | exuin |
Points that are OUTSIDE the image size keep getting appended to the array even though I have multiple checks to prevent it!!!
func flood_region(i: int) -> Array:
var visited := []
mutex.lock()
image.lock()
var boxes := []
for y in range(regions[i].position.y, regions[i].end.y):
var x: int = regions[i].position.x
while x < regions[i].end.x:
var point := Vector2(x, y)
if point in visited or image.get_pixelv(point).a == 0.0:
x += 1
continue
var current_box := Rect2(point, Vector2.ONE)
var queue := [point]
while queue.size() > 0:
var new_point: Vector2 = queue.pop_back()
visited.append(new_point)
current_box = current_box.expand(new_point)
if new_point.x + 1 > current_box.end.x:
current_box.end.x = new_point.x + 1
if new_point.y + 1 > current_box.end.y:
current_box.end.y = new_point.y + 1
if not new_point.x - 1 == -1 and image.get_pixelv(new_point + Vector2.LEFT).a != 0.0 and not new_point + Vector2.LEFT in visited and new_point.x - 1 >= regions[i].position.x:
queue.append(new_point + Vector2.LEFT)
if not new_point.x + 1 == image.get_width() and image.get_pixelv(new_point + Vector2.RIGHT).a != 0.0 and not new_point + Vector2.RIGHT in visited and new_point.x + 1 < regions[i].end.x:
queue.append(new_point + Vector2.RIGHT)
if not new_point.y == -1 and image.get_pixelv(new_point + Vector2.UP).a != 0.0 and not new_point + Vector2.UP in visited and new_point.y - 1 < regions[i].position.y:
queue.append(new_point + Vector2.UP)
if not new_point.y + 1 == image.get_height() and image.get_pixelv(new_point + Vector2.DOWN).a != 0.0 and not new_point + Vector2.DOWN in visited and new_point.y + 1 < regions[i].end.y:
queue.append(new_point + Vector2.DOWN)
boxes.append(current_box)
x += current_box.size.x
image.unlock()
mutex.unlock()
# call_deferred("thread_done", i)
return boxes
I think you should refine your conditionals to simplify them and your bug will pop out at you. I think creating an is_inside_image(point: Vector2)
helper function would do wonders for your simplifying your logic.
timothybrentwood | 2022-12-02 01:09