Java 部分递归洪水填充函数
这是一份作业。我们被要求制作完全递归和部分递归的洪水填充函数。我能够简单地完成完全递归函数,但我在部分递归函数上遇到了困难。我可以在这个问题上使用第二种意见,因为我确信自己做得对,不知道在哪里可以找到错误。我已经为每一行代码添加了关于我的逻辑的注释Java 部分递归洪水填充函数,java,recursion,flood-fill,Java,Recursion,Flood Fill,这是一份作业。我们被要求制作完全递归和部分递归的洪水填充函数。我能够简单地完成完全递归函数,但我在部分递归函数上遇到了困难。我可以在这个问题上使用第二种意见,因为我确信自己做得对,不知道在哪里可以找到错误。我已经为每一行代码添加了关于我的逻辑的注释 //setup function for the partially recursive function. void DoFloodFill( int x, int y ) { x -= m_nTestShapeX; y -= m
//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);
}
}
//部分递归函数的设置函数。
填空(整数x,整数y)
{
x-=m_stshapex;
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();
如果(m_nStartColor==m_SelectedColor)
{
返回;
}
填海(x、y、w、h、帆布);
}
空泛光填充(整数x、整数y、整数w、整数h、图形画布)
{
int xx=0,right=0;
//如果x或y值超出范围,则返回
如果(x>=w | | y>=h | | x<0 | | y<0)
返回;
//如果传入的像素不是起始颜色返回
//递归的基本情况
if(GetPixel(x,y)!=此.m\n启动颜色)
返回;
//用来向右走,直到撞到墙或被绑住为止。
xx=x;
//从当前像素向右移动,将其设置为所需颜色
而(xx=0&&this.m_nStartColor==GetPixel(xx,y))
{
此.SetPixel(xx+100,y+100,画布);
此.SetPixel(xx+100,y+100,此.m选定颜色);
xx--;
}
//从左墙所在的位置开始
对于(;xx
这就是当我点击一个像素时的样子。我单击的位置用红色标记。
上述代码更改:
this.SetPixel(xx+100, y+100, m_nSelectedColor);
到
+100
?可能是我。@JoopEggen他不能因为名声?对不起,+100是像素实际保存的位置与它应该显示在屏幕上的位置之间的偏移量,如果这样做的话。更新了while循环。SetPixel(xx+100,y+100,canvas);此.SetPixel(xx,y,此.m选定颜色);在某个点仍然出现堆栈溢出问题解决了,谢谢@约佩根
this.SetPixel(xx, y, m_nSelectedColor);