This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LDDestroier's profile picture

[Help] How to code paint bucket fill tool for PAIN

Started by LDDestroier, 28 April 2016 - 11:00 AM
LDDestroier #1
Posted 28 April 2016 - 01:00 PM
Current code is here (wJQ7jav0)

My problem (as in the title) is I want a fill tool for my paint program, PAIN. Trouble is, I haven't the slightest idea of how it works.
The paint variable (paintEncoded) is stored in a table format, like this:

paintEncoded = {
    {
	    x = 1,
	    y = 4,
	    char = " "
	    txtcolor = 1,
	    bgcolor = 16,
	    metadata = 2,
    },
    {
	    x = 2,
	    y = 4,
	    char = " "
	    txtcolor = 1,
	    bgcolor = 16,
	    metadata = 2,
    },
}

I already got a function to make sure that no value in paintEncoded appears twice, so keep that in mind, I guess
CometWolf #2
Posted 28 April 2016 - 01:30 PM
It's a pretty straightforward procedure really. Assuming you know which color the user wants to replace/fill, and where it's located. You start by saving the clicked pixel's color, then replace it with the desired color. Afterwards you check it's 4 neighboring pixels for the original color, if they match you replace them. This last bit is repeated until no more matches are found.

I handled it using a table, where i add the neighbors to this table, and loop it until it's empty. Here's the implementation in my program, if you're interested https://github.com/CometWolf/TurtleArchitectV2/blob/master/TAFiles/Tools/Fill.Lua specifically the function called renderFunc
Edited on 28 April 2016 - 11:35 AM
H4X0RZ #3
Posted 28 April 2016 - 01:40 PM
You should take a look at this. https://en.m.wikipedia.org/wiki/Flood_fill
Dragon53535 #4
Posted 29 April 2016 - 12:02 AM
You could set it up so that the x and y values are encoded in the array's index…

The length of the table is thus the area of the full paint area. 64x128 for example, so 8192 and to reference a specific point you would then have to do [x + (y * width)]. That would allow you to logically change the x and y to reference other positions.

Another way you could do it is set up a two dimensional table that acts itself as a coordinate system that holds the pixel data so that you can loop that way. Either way, you're going to want to be able to find adjacent pixels easily without having to loop through the table a million times.
Edited on 28 April 2016 - 10:02 PM