2016-03-04 72 views
0

这是用于分配的。我们被要求做完全递归和部分递归填充函数。我完全可以完全完成递归函数,但我在部分递归函数上挣扎。我可以对此采取第二种意见,因为我确信自己已经做对了,不知道该在哪里寻找错误了。我为每行代码添加了关于我的逻辑的评论。部分递归填充函数

//setup function for the partially recursive function. 
void DoFloodFill(int x, int y) 
{ 
    x -= m_nTestShapeX; 
    y -= m_nTestShapeY; 
    m_nStartColor = GetPixel(x,y) | 0xff000000; 
    Graphics canvas = getGraphics(); 
    canvas.setColor(m_objSelectedColor); 

    int w = m_objShape.getWidth(); 
    int h = m_objShape.getHeight(); 


    if(m_nStartColor == m_nSelectedColor) 
    { 
     return; 
    } 

    FloodFill(x, y, w, h, canvas); 
} 

void FloodFill(int x, int y, int w, int h, Graphics canvas) 
{ 
    int xx = 0, right = 0; 

    // if the x or y values are out of bounds return 
    if(x >= w || y >= h || x < 0 || y < 0) 
     return; 

    //if the passed in pixel is not the start color return 
    //base case for recursion 
    if(GetPixel(x,y) != this.m_nStartColor) 
     return; 

    //used to walk right untill a wall or bound is hit. 
    xx = x; 

    //walk right from the current pixel setting it to the desired color 
    while(xx < w && this.m_nStartColor == GetPixel(xx,y)) 
    { 
     this.SetPixel(xx+100, y+100, canvas); 
     this.SetPixel(xx+100, y+100, this.m_nSelectedColor); 
     xx++; 
    } 
    //save the x value of the the pixel where the wall is 
    right = xx; 

    //used to left starting one pixel to the left of the current pixel 
    xx = x-1; 

    //walk left of the current pixel setting it to the desired color 
    while(xx >= 0 && this.m_nStartColor == GetPixel(xx,y)) 
    { 
     this.SetPixel(xx+100, y+100, canvas); 
     this.SetPixel(xx+100, y+100, this.m_nSelectedColor); 
     xx--; 
    } 

    //start from where the left wall is 
    for(; xx < right; xx++) 
    { 
     //now this should go up one/down one and repeat the right and left walks 
     //the base cases should prevent this from running in most cases 
     //because when it tries to walk down and the start color is not == to the current pixel it will return. 
     FloodFill(xx,y+1,w,h,canvas); 
     FloodFill(xx,y-1,w,h,canvas); 
    } 
} 

这就是我点击一个像素时的样子。我点击的地方标记为红色。 eventual stack-overflows

+0

'+ 100'?这可能是我。 –

+0

@JoopEggen他不能因为声誉? –

+0

抱歉,+100是像素实际保存位置的偏移量,如果自此以后它将显示在屏幕上的位置。 – user3590350

回答

0

从上面的代码变化:

this.SetPixel(xx+100, y+100, m_nSelectedColor); 

this.SetPixel(xx, y, m_nSelectedColor);