Jump to content




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


3 replies to this topic

#1 LDDestroier

  • Members
  • 1,095 posts
  • LocationACDC Town

Posted 28 April 2016 - 12: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


#2 CometWolf

  • Members
  • 1,283 posts

Posted 28 April 2016 - 12: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/C.../Tools/Fill.Lua specifically the function called renderFunc

Edited by CometWolf, 28 April 2016 - 12:35 PM.


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 28 April 2016 - 12:40 PM

You should take a look at this. https://en.m.wikiped...wiki/Flood_fill

#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 28 April 2016 - 11:02 PM

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 by Dragon53535, 28 April 2016 - 11:02 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users