在扫描线填充中识别水平线 [英] identifying a Horizontal line in Scan Line Filling

查看:111
本文介绍了在扫描线填充中识别水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试在屏幕上渲染某些字体.其实我已经做到了.
现在我正在尝试填充字符,即填充有界区域.

我唯一拥有的是一个2-d缓冲区,该缓冲区存储每个像素的信息,即如果要照亮像素则设置该像素,否则它包含``0''.

我想不出使用此逻辑来填充边界区域的逻辑.
这是我尝试过的方法,但由于无法识别水平线而失败.

Hey I am trying to render some fonts on the screen. Actually I have done that.
Now what I am trying is to fill the characters i.e. fill the bounded regions.

The only thing I have is a 2-d buffer which stores information for each pixel i.e. it is set if the pixel is to be illuminated otherwise it contains ''0''.

I cant think of a logic to fill the bounded regions using this.
This is what I tried but it failed as it cant recognize the horizontal lines.

for(int p=0 ; p<401 ; ++p){
            for(int q=0 ; q<401 ; ++q){
                if( buffer[p][q] == 1 ){
                    ++q ;
                    while (buffer[p][q] == 0){
                          buffer[p][q]  = 1 ;
                          ++q ;
                    }
                }
            }
        }

推荐答案

为什么不看 Wikipedia的Flood Flood算法 [ ^ ]?
Why don''t you have a look at Flood Fill algorithm[^] at Wikipedia?


搜索填充多边形"或扫描线填充算法".

这是扫描线问题.如果可以绘制字体轮廓,则还可以将点存储在Y/X排序列表中.如果未绘制轮廓,则可以对其进行扫描以创建列表.

[y1] [x1],[y1] [x2],[y1] [x3],[y1] [x4],...
[y2] [x1],[y2] [x2],[y2] [x3],[y2] [x4] ...
...

x1 =>最左
x2 =>第一右
x3 =>左下
x4 =>下一个右
...

[y1] [x1]行至[y1] [x2]无行[y1] [x3]行至[y1] [x4]

我只是写了以下内容-因此尚未经过测试.但是它应该使您了解扫描线填充的工作方式.

Search for "fill polygon" or "scanline fill algorithm".

It is a scan-line problem. If you can draw the font outline, then you can also store the points in a Y/X sorted list. If you did not draw the outline, then you can scan it to create the list.

[y1][x1], [y1][x2], [y1][x3], [y1][x4], ...
[y2][x1], [y2][x2], [y2][x3], [y2][x4]...
...

x1 => far left
x2 => first right
x3 => next left
x4 => next right
...

[y1][x1] line to [y1][x2] no line [y1][x3] line to [y1][x4]

I just wrote the following - so it has not been tested. But it should give you an idea of how a scan-line fill works.

struct MyPoint {
	int x, y;
};
// Fill horizontal lines between points in given point list
// - does not draw on outline (border)
void FillBetweens(MyPoint* list, size_t list_size)
{
	// assumes list is sorted from top to bottom and left to right
	int current_y = 0;
	for (size_t i=0; i<list_size-1; ++i)
	{
		if( list[i].y != current_y )
			current_y = list[i].y; // or ++current_y (depends on your choices)
		if( list[i+1].y == current_y )
		{
			// your horizontal line drawing function (y,x1,x2)
			// - assumes drawing between end points
			draw_betweens(list[i].y, list[i].x, list[i+1].x);
			++i;
		}
		// else we skip single point lines
	}
}


这篇关于在扫描线填充中识别水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆