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
'+ 100'?這可能是我。 –
@JoopEggen他不能因爲聲譽? –
抱歉,+100是像素實際保存位置的偏移量,如果自此以後它將顯示在屏幕上的位置。 – user3590350