–1 vote

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
Godot version 3.5.1
in Engine by (8,546 points)

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.

1 Answer

+1 vote

The answer was something outside of the code shown. I spent hours on this aaaaaaaaa

by (8,546 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.