我没有使用任何类型的洪水填充算法的经验。但我给了我最好的一击。起初,我会在用户点击洋红色的任何地方填充一个像素,然后经过一个循环,在每个洋红色像素的顶部,左侧,底部和右侧绘制一个像素。这将需要8秒钟才能完成特别大面积的填充。于是我又添加了一个循环,它画出了直线洋红色的线条,上下左右。这个时间减半了。我让电脑画了更多的线......左上,左上,左上,左下左......等等。现在它已经下降到了2秒钟之间。我还能做些什么来减少完成填充所需的时间?精炼自定义洪水填充算法(Java)
for(Point p:shiftDownPoints)
{
//down
for(int y = p.y+1; y<SCREEN_DIM.height; y++)
{
if(capture.getRGB(p.x,y)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x,y,Color.MAGENTA.getRGB());
}
//up
for(int y = p.y-1; y>0; y--)
{
if(capture.getRGB(p.x,y)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x,y,Color.MAGENTA.getRGB());
}
//right
for(int x = p.x+1; x<SCREEN_DIM.width; x++)
{
if(capture.getRGB(x,p.y)==Color.BLACK.getRGB())break;
else capture.setRGB(x,p.y,Color.MAGENTA.getRGB());
}
//left
for(int x = p.x-1; x>0; x--)
{
if(capture.getRGB(x,p.y)==Color.BLACK.getRGB())break;
else capture.setRGB(x,p.y,Color.MAGENTA.getRGB());
}
//down-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y+i,Color.MAGENTA.getRGB());
}
//down-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y+i,Color.MAGENTA.getRGB());
}
//up-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y-i,Color.MAGENTA.getRGB());
}
//up-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y-i,Color.MAGENTA.getRGB());
}
//up-up-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y-i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y-i*2,Color.MAGENTA.getRGB());
}
//up-left-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i*2,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i*2,p.y-i,Color.MAGENTA.getRGB());
}
//down-left-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i*2,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i*2,p.y+i,Color.MAGENTA.getRGB());
}
//down-down-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y+i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y+i*2,Color.MAGENTA.getRGB());
}
//down-down-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y+i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y+i*2,Color.MAGENTA.getRGB());
}
//down-right-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i*2,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i*2,p.y+i,Color.MAGENTA.getRGB());
}
//up-right-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i*2,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i*2,p.y-i,Color.MAGENTA.getRGB());
}
//up-up-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y-i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y-i*2,Color.MAGENTA.getRGB());
}
}
while(pixelsDrawn)
{
pixelsDrawn=false;
for(int x = 0; x<SCREEN_DIM.width; x++)
for(int y = 0; y<SCREEN_DIM.height; y++)
{
if(capture.getRGB(x,y)==Color.MAGENTA.getRGB())
{
if(capture.getRGB(x-1,y)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x-1,y)!=Color.BLACK.getRGB())
{
capture.setRGB(x-1,y,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x+1,y)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x+1,y)!=Color.BLACK.getRGB())
{
capture.setRGB(x+1,y,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x,y-1)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x,y-1)!=Color.BLACK.getRGB())
{
capture.setRGB(x,y-1,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x,y+1)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x,y+1)!=Color.BLACK.getRGB())
{
capture.setRGB(x,y+1,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
}
}
}
通常它的工作原理是,您只需检查刚刚更改的像素旁边的像素,而不是重复检查整个图像。 – immibis
immibis,如果这是一个答案,我会投它为最好的。感谢那!这是一个“哦,是的!”有时候我读了你的评论,现在我把它降到半秒钟。再次感谢! – user3376587