对象头实例数据对齐填充
什么是边界填充算法? (What are Boundary Fill Algorithms?)
Boundary fill is the algorithm used frequently in computer graphics to fill a desired color inside a closed polygon having the same boundary color for all of its sides.
边界填充是在计算机图形学中经常使用的算法,用于在其所有边都具有相同边界颜色的封闭多边形内填充所需颜色。
The most approached implementation of the algorithm is a stack-based recursive function.
该算法最接近的实现是基于堆栈的递归函数。
工作示例: (Working Example:)
The problem is pretty simple and usually follows these steps:
该问题非常简单,通常按照以下步骤操作:
- Take the position of the starting point and the boundary color. 取起点的位置和边界颜色。
- Decide whether you want to go in 4 directions (N, S, W, E) or 8 directions (N, S, W, E, NW, NE, SW, SE). 决定要沿4个方向(N,S,W,E)还是沿8个方向(N,S,W,E,NW,NE,SW,SE)走。
- Choose a fill color. 选择一种填充颜色。
- Travel in those directions. 朝那些方向旅行。
- If the pixel you land on is not the fill color or the boundary color, replace it with the fill color. 如果您着陆的像素不是填充颜色或边界颜色,则将其替换为填充颜色。
- Repeat 4 and 5 until you've been everywhere within the boundaries. 重复4和5,直到到达边界内的任何地方。
某些限制: (Certain Restrictions:)
- The boundary color should be the same for all the edges of the polygon. 多边形的所有边缘的边界颜色应相同。
- The starting point should be within the polygon. 起点应在多边形内。
代码段: (Code Snippet:)
void boundary_fill(int pos_x, int pos_y, int boundary_color, int fill_color)
{
current_color= getpixel(pos_x,pos_y); //get the color of the current pixel position
if( current_color!= boundary_color || currrent_color != fill_color) // if pixel not already filled or part of the boundary then
{
putpixel(pos_x,pos_y,fill_color); //change the color for this pixel to the desired fill_color
boundary_fill(pos_x + 1, pos_y,boundary_color,fill_color); // perform same function for the east pixel
boundary_fill(pos_x - 1, pos_y,boundary_color,fill_color); // perform same function for the west pixel
boundary_fill(pos_x, pos_y + 1,boundary_color,fill_color); // perform same function for the north pixel
boundary_fill(pos_x, pos_y - 1,boundary_color,fill_color); // perform same function for the south pixel
}
}
From the given code you can see that for any pixel that you land on, you first check whether it can be changed to the fill_color and then you do so for its neighbors till all the pixels within the boundary have been checked.
从给定的代码中可以看到,对于您着陆的任何像素,首先要检查是否可以将其更改为fill_color,然后对其相邻像素进行更改,直到检查了边界内的所有像素为止。
Here's a video tutorial that will walk you through these in detail in about 10 minutes:
这是一个视频教程,将在大约10分钟的时间内详细指导您:
翻译自: https://www.freecodecamp.org/news/boundary-fill-algorithm-pixel-filling-squares/
对象头实例数据对齐填充