Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: Re: filling area by color atack safety Date: Sun, 17 Mar 2024 13:23:55 +0000 Organization: A noiseless patient Spider Lines: 78 Message-ID: References: <20240317144625.00002011@yahoo.com> <20240317151519.000057d0@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 17 Mar 2024 13:23:52 -0000 (UTC) Injection-Info: dont-email.me; posting-host="86cdd72051d773a92e0c8f332750da77"; logging-data="3726685"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sDcRCE949noLQoxoDye+1" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Of2gPKj6NFpkXd2IkK03WSvN6N4= In-Reply-To: <20240317151519.000057d0@yahoo.com> Content-Language: en-GB Bytes: 4161 On 17/03/2024 13:15, Michael S wrote: > On Sun, 17 Mar 2024 12:54:34 +0000 > bart wrote: > >> On 17/03/2024 12:46, Michael S wrote: >>> On Sat, 16 Mar 2024 11:33:20 +0000 >>> Malcolm McLean wrote: >>> >>>> On 16/03/2024 04:11, fir wrote: >>>>> i was writing simple editor (something like paint but more custom >>>>> for my eventual needs) for big pixel (low resolution) drawing >>>>> >>>>> it showed in a minute i need a click for changing given drawed >>>>> area of of one color into another color (becouse if no someone >>>>> would need to do it  by hand pixel by pixel and the need to >>>>> change color of given element is very common) >>>>> >>>>> there is very simple method of doing it - i men i click in given >>>>> color pixel then replace it by my color and call the same function >>>>> on adjacent 4 pixels (only need check if it is in screen at all >>>>> and if the color to change is that initial color >>>>> >>>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned >>>>> old_color, unsigned new_color) >>>>> { >>>>>   if(old_color == new_color) return 0; >>>>> >>>>>   if(XYIsInScreen( x,  y)) >>>>>   if(GetPixelUnsafe(x,y)==old_color) >>>>>   { >>>>>     SetPixelSafe(x,y,new_color); >>>>>     RecolorizePixelAndAdjacentOnes(x+1, y,  old_color, >>>>> new_color); RecolorizePixelAndAdjacentOnes(x-1, y,  old_color, >>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y-1,  old_color, >>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y+1,  old_color, >>>>> new_color); return 1; >>>>>   } >>>>> >>>>>   return 0; >>>>> } >>>>> >>>>> it work but im not quite sure how to estimate the safety of this >>>>> - incidentally as i said i use this editor to low res graphics >>>>> like 200x200 pixels or less, and it is only a toll of private use, >>>>> yet i got no time to work on it more than 1-2-3 days i guess but >>>>> still >>>>> >>>>> is there maybe simple way to improve it? >>>> > >>>> This is a cheap and cheerful fllod fill. And it's easy to get right >>>> and shouldn't afall over. >>> >>> Except I don't understand why it works it all. >>> Can't fill area have sub-areas that only connected through >>> diagonal? >> >> Suppose you have an image which is a chessboard. You want to fill one >> of the black squares so that it is red. >> >> If you allow connectivity through the diagonals (so two notionally >> square pixels that only meet at their corners would be connected), >> then all the black squares would turn red, not just one. >> > > That's what I want. > Do fir wants something else? > His algorithm is the same as that presented in my textbook, where it is called FloodFill4. If I reread the notes I see now the significance of the '4', as it talks about 4-connected and 8-connected versions. Presumably you want the 8-connected version, which will have 4 extra calls for the pixels at each corner.