starsky51
MIS
- Mar 11, 2002
- 91
Hi All,
I've written a simple drawing program which, so far, consists of drawing shapes of various colours using the usual pen drag type method and then filling in these shapes using the recursive function below.
Filling in small shapes is not a problem, but filling in bigger shapes causes a stack overflow.
Are there any obvious problems with this code?
Dave.
(NB. m_cFromColor and m_cToColor are COLORREFs, m_cClientRect is a CRect which has been passed the GetClientRect of the window.)
void CMouseTrackDlg::RecursiveFill(CDC* pDC, int x, int y)
{
pDC->SetPixel(x, y, m_cToColor);
if(x>1 && pDC->GetPixel(x-1,y)==m_cFromColor)
RecursiveFill(pDC, x-1, y);
if(x<m_rClientArea.Width()-1 && pDC->GetPixel(x+1,y)==m_cFromColor)
RecursiveFill(pDC, x+1, y);
if(y>1 && pDC->GetPixel(x,y-1)==m_cFromColor)
RecursiveFill(pDC, x, y-1);
if(y<m_rClientArea.Height()-1 && pDC->GetPixel(x,y+1)==m_cFromColor)
RecursiveFill(pDC, x, y+1);
return;
}
I've written a simple drawing program which, so far, consists of drawing shapes of various colours using the usual pen drag type method and then filling in these shapes using the recursive function below.
Filling in small shapes is not a problem, but filling in bigger shapes causes a stack overflow.
Are there any obvious problems with this code?
Dave.
(NB. m_cFromColor and m_cToColor are COLORREFs, m_cClientRect is a CRect which has been passed the GetClientRect of the window.)
void CMouseTrackDlg::RecursiveFill(CDC* pDC, int x, int y)
{
pDC->SetPixel(x, y, m_cToColor);
if(x>1 && pDC->GetPixel(x-1,y)==m_cFromColor)
RecursiveFill(pDC, x-1, y);
if(x<m_rClientArea.Width()-1 && pDC->GetPixel(x+1,y)==m_cFromColor)
RecursiveFill(pDC, x+1, y);
if(y>1 && pDC->GetPixel(x,y-1)==m_cFromColor)
RecursiveFill(pDC, x, y-1);
if(y<m_rClientArea.Height()-1 && pDC->GetPixel(x,y+1)==m_cFromColor)
RecursiveFill(pDC, x, y+1);
return;
}