binarymax / floodfill.js

HTML5 Canvas Floodfill via JavaScript
MIT License
57 stars 14 forks source link

Three dimensional flood fill? #18

Closed gigablox closed 6 years ago

gigablox commented 6 years ago

I have a question here on stack: https://stackoverflow.com/questions/48140264/in-a-mesh-made-of-cubes-with-different-colors-how-do-i-find-the-matching-cluste

I feel like I might be able to use your algorithm to solve for.

Would really appreciate your opinion if you think this implementation would help.

binarymax commented 6 years ago

Thanks for the note. The problem is an interesting one and indeed you could modify the algorithm to add an extra dimension.

First thing you would need to do is modify how the data structure maps to incorporate the dimension z. With 2D array mapping to 1D it is i = (x+y*width)*4. You'd need to do this incorporating z as well.

Then the heart of the matter would be lines 20 to 29. 'e' and 'w' are short for 'east' and 'west'. Starting from a given point they go in those directions comparing pixels until they hit nonmatching pixels or bounds (me and mw). Afterwich they move up and down adding the rows to the queue for the next scan. You would need to get this to also add a 'near' and 'far' to the queue that would decrement and increment on the dimension z.

Afterwards it is a matter of including the z param in the rest of the methods.

Hope this helps.

gigablox commented 6 years ago

Thanks for your time @binarymax I'll play around with this.