Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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 ;
                    }
                }
            }
        }
Posted
Updated 15-Jul-11 2:20am
v2
Comments
R. Erasmus 15-Jul-11 9:53am    
This piece of code is prone to having bugs....
incrementing q will cause array out of bounds exception when q = 400 if buffer size contains 401 elements.
Loop is also way to complex... Its better to use functions that devides the program up into simpler segments. Its a testing nightmare.

Why don't you have a look at Flood Fill algorithm[^] at Wikipedia?
 
Share this answer
 
Comments
Espen Harlinn 15-Jul-11 8:13am    
Right :)
Member 8086596 15-Jul-11 8:17am    
That's entirely different thing i guess. I don't have any idea about the behaviour of the edges or you can say the bounded regions. I only have the pixel info in the 2-d buffer , but for standard flood fill algorithm i need to know the coordinates of corners.
CPallini 15-Jul-11 8:23am    
I guess your guess is wrong. Please read carefully the linked page.
Member 8086596 15-Jul-11 8:28am    
Suppose i have rendered "ABC" on the canvas. Firstly i dont have start node to start with , secondly, even if I get the start node by scanning the buffer[][] that would become very inefficient in case the text is lenghty coz i have to find the start node for each and every character.
CPallini 15-Jul-11 8:34am    
Could you please post one (or more) image(s)?
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
	}
}
 
Share this answer
 
v2
Comments
Member 8086596 15-Jul-11 13:28pm    
that means for characters like "C" which have a curve I need to get the cordinates corresponding to each pixel.
John R. Shaw 15-Jul-11 13:58pm    
Basically - you do need to create a scan-line list to use the normal method - which relies on knowing the coordinates of the edges.

Example line to fill:
[0][0] = 1, ..., [0][10] = 1, [0][11] = 1, [0][12] = 1

in MyPoint list that would be
list[0] = {0,0}; // left
list[1] = {0,10}; // right

In this case we do not want [0][11] or [0][12] in our list.

The method you are currently using would work, but resembles a flood fill. May be a look at the diagrams in my QuickFill article at codeproject would give you a better idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900