Help with flood fill algorithm.

perps

Member
Joined
Sep 8, 2005
Messages
14
Programming Experience
Beginner
Greetings to all.

Can anyone help me with regards to implementing a flood fill algorithm? My code thus far is below, I keep getting an invalid parameter used error as soon as the algorithm approaches the bottom boundary of the shape I wish to fill (i've stepped it through and discovered this).

VB.NET:
Dim xCord As Integer = e.X
            Dim yCord As Integer = e.Y
            Dim xCount As Integer
            Dim yCount As Integer
            Dim BottomEdge As Boolean = False



            ' for xCount at the x mouse co-ordinate to the right hand edge of the bmp
            ' compare the current pixel colour to the background colour by comparing toString.
            ' if current pixel is the same as the background colour, then change it's colour to the selected colour.
            For yCount = yCord To bmp.Height Step +1


                ' Handles all pixels to the right of the mouse co-ordinates.
                For xCount = xCord To bmp.Width Step +1

                    ' If got pixel and background pixel are the same, then make got pixel the selected colour.
                    If bmp.GetPixel(xCount, yCount).ToArgb = picMain.BackColor.ToArgb Then
                        bmp.SetPixel(xCount, yCount, pnlColour.BackColor)
                        ' If got pixel and background colour AREN'T the same, i.e outside the right edge of
                        ' the shape, then exit for loop.
                    Else 
                        Exit For
                    End If
                Next
                ' Handles all pixels to the left of the mouse co-ordinates.
                For xCount = xCord - 1 To 0 Step -1
                    If bmp.GetPixel(xCount, yCount).ToArgb = picMain.BackColor.ToArgb Then
                        bmp.SetPixel(xCount, yCount, pnlColour.BackColor)
                        ' Else case where pixel colour is not the same as the background (i.e already filled), then exit.
                    Else
                        Exit For
                    End If
                Next
            Next
        End If
If anyone has any idea what could be causing the problem, please let me know.

Thanks in advance,

- perps.
 
*bump*

I've also tried creating a recursive sub that is called when the user hits a button with;

VB.NET:
            Dim xCord As Int32 = e.X
            Dim yCord As Int32 = e.Y


            floodFill4(xCord, yCord)

And then the sub itself;

VB.NET:
    ' Flood fill algorithm. Does not work properly, floodFill4 calls where the yvalue is changed (intY + 1 and intY - 1)
    ' result in System.StackOverflowException for unknown reason.
    ' Algorithm will quite happily fill a row of pixels, but won't do multiple rows.

    Private Sub floodFill4(ByVal intX As Int32, ByVal intY As Int32)
        Try
            If bmp.GetPixel(intX, intY).ToArgb = picMain.BackColor.ToArgb Then

                bmp.SetPixel(intX, intY, pnlColour.BackColor)

                floodFill4(intX + 1, intY)
                floodFill4(intX - 1, intY)
                floodFill4(intX, intY + 1)
                floodFill4(intX, intY - 1)

            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub

It will correctly colour all the pixels in a row, but as soon as any changes to the y value occur (i.e. in the lines floodFill4(intX, intY +1) and floodFill4(intX, intY + 1)), it produces System.StackOverflowException. How is it that any recursive calls to the x value are fine, but recursive calls to the y values cause failure?? Incidentally, this is the same error I experienced with the code in my first post as well.

Any ideas on this one?? I've been pulling my hair out trying to figure why this isn't working??

Thanks,

- perps.
 
Back
Top