我有一些我正在使用的旧的基本代码。就像这样:
PAINT (200, 200), 2, 10
这意味着:用深绿色填充一个区域,从坐标:X200Y200开始,直到点达到浅绿色的边界颜色,然后停止填充。
其思想是,一个人可以用一种颜色画一个轮廓(比如一种状态),然后用另一种颜色填充整个轮廓。它不会填充整个屏幕,因为轮廓是填充停止的颜色。
发布于 2015-12-14 19:17:58
您可以使用洪水填埋场填充区域。它以一个起点(或种子点)作为输入,并通过尝试填充其空邻居来递归地填充该区域。
JavaScript中一个简单的基于堆栈的实现:
// Takes the seed point as input
var floodfill = function(point) {
var stack = Array();
stack.push(point); // Push the seed
while(stack.length > 0) {
var currPoint = stack.pop();
if(isEmpty(currPoint)) { // Check if the point is not filled
setPixel(currPoint); // Fill the point with the foreground
stack.push(currPoint.x + 1, currPoint.y); // Fill the east neighbour
stack.push(currPoint.x, currPoint.y + 1); // Fill the south neighbour
stack.push(currPoint.x - 1, currPoint.y); // Fill the west neighbour
stack.push(currPoint.x, currPoint.y - 1); // Fill the north neighbour
}
}
};
isEmpty(point)
是测试点(x, y)
是否填充边界颜色(在本例中为浅绿色)的函数。
setPixel(point)
用前景颜色填充点(x, y)
(在您的例子中是深绿色)。
履行这些职能是微不足道的,我把它留给您。
上面的实现使用了一个4-连通邻居。但它可以很容易地扩展到6或8个相连的社区。
https://stackoverflow.com/questions/34280412
复制