我有一些正在使用的旧基本代码。它是这样的:
PAINT (200, 200), 2, 10
这实际上意味着:用深绿色填充从坐标X 200 Y 200开始的区域,直到该点达到浅绿色的边界颜色,然后停止填充。
这个想法是,可以使用一种颜色绘制轮廓(例如状态),然后用另一种颜色填充整个轮廓。它不会填满整个屏幕,因为轮廓是填充停止处的颜色。
您可以使用洪水填充来填充区域。它以起点(或种子点)作为输入,并通过尝试填充其空的邻居来递归填充该区域。
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个连接的社区。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句