-
- Notifications
You must be signed in to change notification settings - Fork 359
Added flood fill in Coconut #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a18e7cc 7d2739b 5da3ae8 d21dacd aa08db0 a4dbc2a 9dcc06f 627aed9 fee7e83 85f8e9a 18944ea f1aa057 7bc0e22 91d5625 5a6ebd0 153317d 0ca5ac6 7ada7da c72caaf 70822e9 9d0a5bb 2793edf 8426216 e92bcb7 e631b57 02dd353 f9a6eda b49f034 9d7349a 5be74ec 1949a10 a5c0f6b db00314 f312994 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -23,52 +23,53 @@ def colour(canvas, location is Point, old_value, new_value): | |
| | ||
| | ||
| def find_neighbours(canvas, location is Point, old_value, new_value): | ||
| possible_neighbours = (Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)) |> map$(location.__add__) | ||
| possible_neighbours = ((Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)) | ||
| |> map$(location.__add__)) | ||
| | ||
| for neighbour in possible_neighbours: | ||
| if inbounds(canvas.shape, neighbour) and canvas[neighbour] == old_value: | ||
| yield neighbour | ||
| yield from possible_neighbours |> filter$(x -> (inbounds(canvas.shape, x) and canvas[x] == old_value)) | ||
| | ||
| | ||
| def stack_fill(canvas, location is Point, old_value, new_value): | ||
| if new_value == old_value: | ||
| if new_value == old_value or not inbounds(canvas.shape, location): | ||
| return | ||
| | ||
| stack = [location] | ||
| | ||
| while stack: | ||
| current_location = stack.pop() | ||
| colour(canvas, current_location, old_value, new_value) | ||
| canvas[current_location] = new_value | ||
| for neighbour in find_neighbours(canvas, current_location, old_value, | ||
| ||
| new_value): | ||
| stack.append(neighbour) | ||
| | ||
| | ||
| def queue_fill(canvas, location is Point, old_value, new_value): | ||
| if new_value == old_value: | ||
| if new_value == old_value or not inbounds(canvas.shape, location): | ||
| return | ||
| | ||
| queue = deque() | ||
| queue.append(location) | ||
| | ||
| colour(canvas, location, old_value, new_value) | ||
| canvas[location] = new_value | ||
| | ||
| while queue: | ||
| current_location = queue.popleft() | ||
| for neighbour in find_neighbours(canvas, current_location, old_value, | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing as above, you could do Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I see how to do that right away. I probably need to work on it a bit more, but that seems out of my capabilities for now. | ||
| new_value): | ||
| queue.append(neighbour) | ||
| colour(canvas, neighbour, old_value, new_value) | ||
| canvas[neighbour] = new_value | ||
| | ||
| | ||
| def recursive_fill(canvas, location is Point, old_value, new_value): | ||
| if new_value == old_value: | ||
| if new_value == old_value or not inbounds(canvas.shape, location): | ||
| return | ||
| colour(canvas, location, old_value, new_value) | ||
| | ||
| for neighbour in find_neighbours(canvas, location, old_value, new_value): | ||
| recursive_fill(canvas, neighbour, old_value, new_value) | ||
| | ||
| canvas[location] = new_value | ||
| # consume is important here, because otherwise, the recursive function is not called again | ||
| consume( | ||
| find_neighbours(canvas, location, old_value, new_value) | ||
| |> map$(recursive_fill$(canvas, ?, old_value, new_value)) | ||
| ) | ||
| | ||
| if __name__ == '__main__': | ||
| # Testing setup | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe try to make the tests similair to the Julia tests. Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean "similar to the Julia tests"? Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disregard the thing I said. I meant to say that you should probably use the same test as in the Julia implementation, namely: But I just realize that you are already doing so. It's just that Julia indexes it's arrays starting at 1. Got confused there. | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll agree with you that it's better to do stack work with imperative code. Don't try to squeeze functional code into this.