











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












So far we have covered some output primitives that is drawing primitives like point, line, circle, ellipse and some other variations of curves. Also we can draw certain other shapes with the combinations of lines like triangle, rectangle, square and other polygons (we will have some discussion on polygons coming ahead). Also we can draw some shapes using mixture of lines and curves or circles etc. So we are able to draw outline/ sketch of certain models but need is there to make a solid model. Therefore, in this section we will see what are filled area primitives and what are the different issues related to them. There are two basic approaches to area filling on raster systems. One way is to draw straight lines between the edges of polygon called scan-line polygon filling. As said earlier there are several issues related to scan line polygon, which we will discuss in detail. Second way is to start from an interior point and paint outward from this point till we reach the boundary called boundary-fill. A slight variation of this technique is used to fill an area specified by cluster (having no specific boundary). The technique is called flood-fill and having almost same strategy that is to start from an interior point and start painting outward from this point till the end of cluster. Now having an idea we will try to see each of these one by one, starting from scan-line polygon filling.
Scan-line Polygon Fill
Before we actually start discussion on scan-line polygon filling technique, it is useful to discuss what is polygon? Besides polygon definition we will discuss the following topics one by one to have a good understanding of the concept and its implementation.
Polygon Definition
Filled vs. Unfilled Polygons
Parity Definition
Scan-Line Polygon Fill Algorithm
Special Cases Handled By the Fill
Polygon Fill Example
Polygon
A polygon can be defined as a shape that is formed by line segments that are placed end to end, creating a continuous closed path. Polygons can be divided into three basic types: convex , concave , and complex.
I. Convex polygons are the simplest type of polygon to fill. To determine whether or not a polygon is convex, ask the following question:
Does a straight line connecting ANY two points that are inside the polygon intersect any edges of the polygon?
If the answer is no, the polygon is convex. This means that for any scan-line, the scan-line will cross at most two polygon edges (not counting any horizontal edges). Convex polygon edges also do not intersect each other.
II. Concave polygons are a superset of convex polygons, having fewer restrictions than convex polygons. The line connecting any two points that lie inside the polygon may intersect more than two edges of the polygon. Thus, more than two edges may intersect any scan line that passes through the polygon. The polygon edges may also touch each other, but they may not cross one another.
Complex polygons are just what their name suggests: complex. Complex polygons are basically concave polygons that may have self-intersecting edges. The complexity arises from distinguishing which side is inside the polygon when filling it.
Difference between Filled and Unfilled Polygon
When an unfilled polygon is rendered, only the points on the perimeter of the polygon are drawn. Examples of unfilled polygons are shown in the next page.
However, when a polygon is filled, the interior of the polygon must be considered. All of the pixels within the boundaries of the polygon must be set to the specified color or pattern. Here, we deal only with solid colors. The following figure shows the difference between filled and unfilled polygons.
So, you can see if we have a sorted list of x-intersections of all of the polygon edges with the scan line, we can simply draw from the first x to the second, the third to the forth and so on.
Polygon Filling
In order to fill a polygon, we do not want to have to determine the type of polygon that we are filling. The easiest way to avoid this situation is to use an algorithm that works for all three types of polygons. Since both convex and concave polygons are subsets of the complex type, using an algorithm that will work for complex polygon filling should be sufficient for all three types. The scan-line polygon fill algorithm, which employs the odd/even parity concept previously discussed, works for complex polygon filling.
Reminder: The basic concept of the scan-line algorithm is to draw points from edges of odd parity to even parity on each scan-line.
b) What is a scan-line?
A scan-line is a line of constant y value, i.e., y=c, where c lies within our drawing region, e.g., the window on our computer screen.
The scan-line algorithm is outlined next.
Algorithm
When filling a polygon, you will most likely just have a set of vertices, indicating the x and y Cartesian coordinates of each vertex of the polygon. The following steps should be taken to turn your set of vertices into a filled polygon.
1. Initializing All of the Edges:
The first thing that needs to be done is determine how the polygon's vertices are related. The all_edges table will hold this information.
Each adjacent set of vertices (the first and second, second and third, similarly last and first) defines an edge. In above figure vertices are shown by small lines and edges are numbered from 1 to 9 each between successive vertices.
For each edge, the following information needs to be kept in a table:
The slope of the edge can be calculated from the formula for a line:
y = mx + b;
where m = slope, b = y-intercept,
y 0 = maximum y value,
y 1 = minimum y value,
x 0 = maximum x value,
x 1 = minimum x value The formula for the slope is as follows:
m = (y 0 - y 1 ) / (x 0 - x 1 ).
For example, the edge values may be kept as follows, where N is equal to the total number of edges - 1 (starting from 0) and each index into the all_edges array contains a pointer to the array of edge values.
Index Y-min Y-max X-val 1/m
0 10 16 10 0
1 16 20 10 1.
N 10 16 28 0
Table: All_edges
2. Initializing the Global Edge Table:
The global edge table will be used to keep track of the edges that are still needed to complete the polygon. Since we will fill the edges from bottom to top and left to right. To do this, the global edge table should be inserted with edges grouped by increasing minimum y values. Edges with the same minimum y values are sorted on minimum x values as follows:
Next, Increase the index to the global edge table once each time the current edge's x value is greater than and the y value is less than or equal to that of the edge at the current index in the global edge table.
If the index, at any time, is equal to the number of edges currently in the global edge table, do not increase the index.
Place the edge information for minimum y value, maximum y value, x value, and 1/m in the global edge table at the index.
Special Cases
There are some special cases, the scan-line polygon fill algorithm covers these cases, but you may not understand how or why. The following will explain the handling of special cases to the algorithm.
1. Horizontal Edges :
Here we follow the minimum y value rule during scan-line polygon fill. If the edge is at the minimum y value for all edges, it is drawn. Otherwise, if the edge is at the maximum y value for any edge, we do not draw it. (See the next section containing information about top vs. bottom edges.)
This is easily done in the scan-line polygon fill implementation. Horizontal edges are removed from the edge table completely.
Question arises that how horizontal lines are are filled then? Since each horizontal line meets exactly two other edge end-points on the scan-line, the algorithm will allow a fill of the pixels between those two end-point vertices when filling on the scan-line which the horizontal line is on, if it meets the top vs. bottom edge criteria.
As can be seen above, if we start with a polygon with horizontal edges, we can remove the horizontal edges from the global edge table. The two endpoints of the edge will still exist and a line will be drawn between the lower edges following the scan-line polygon fill algorithm. (The blue arrowed line is indicating the scan-line for the bottom horizontal edge.)
2. Bottom and Left Edges vs. Top and Right Edges:
If polygons, having at least one overlapping edge the other, were filled completely from edge to edge, these polygons would appear to overlap and/or be distorted. This would be especially noticeable for polygons in which edges have limited space between them.
In order to correct for this phenomenon, our algorithm does not allow fills of the right or top edges of polygons. This distortion problem could also be corrected by not drawing either the left or right edges and not drawing either the top or bottom edges of the polygon. Either way, a consistent method should be used with all polygons. If some polygons are filled with the left and bottom edges and others with the bottom and right edges, this problem will still occur.
As can be seen above, if we remove the right and top edges from both polygons, the polygons no longer appear to be different shapes. For polygons with more overlap than just one edge, the polygons will still appear to overlap as was meant to happen.
3. How do we deal with two edges meeting at a vertex when counting parity? This is a scenario which needs to be accounted for in one of the following ways:
When dealing with two edges; which meet at a vertex and for both edges the vertex is the minimum point, the pixel is drawn and is counted twice for parity.
Essentially, the following occurs. In the scan-line polygon fill algorithm, the vertex is drawn for the first edge, since it is a minimum value for that edge, but not for the second edge, since it is a right edge and right edges are not drawn in the scan-line fill algorithm. The parity is increased once for the first edge and again for the second edge.
When dealing with two edges; which meet at a vertex and for both edges the vertex is the maximum point, the pixel is not drawn and is counted twice for parity.
Basically, this occurs because the vertex is not drawn for the first edge, since it is a maximum point for that edge, and parity is increased. The vertex is then not drawn for the
1. Initializing All of the Edges:
We want to determine the minimum y value, maximum y value, x value, and 1/m for each edge and keep them in the all_edges table. We determine these values for the first edge as follows:
Y-min: Since the first edge consists of the first and second vertex in the array, we use the y values of those vertices to choose the lesser y value. In this case it is 10.
Y-max: In the first edge, the greatest y value is 16.
X-val: Since the x value associated with the vertex with the highest y value is 10, 10 is the x value for this edge.
1/m: Using the given formula, we get (10-10)/ (16-10) for 1/m.
The edge value results are in the form of Y-min, Y-max, X-val, and Slope for each edge array pointed to in the all_edges table. As a result of calculating all edge values, we get the following in the all_edges table.
Index Y-min Y-max X-val 1/m
0 10 16 10 0
1 16 20 10 1.
2 10 20 28 -1.
3 10 16 28 0
4 10 16 22 1
5^10 10 10 Inf
Table: All_edges
2. Initializing the Global Edge Table:
We want to place all the edges in the global edge table in increasing y and x values, as long as slope is not equal to zero. For the first edge, the slope is not zero so it is placed in the global edge table at index=0.
Index Y-min Y-max X-val 1/m
0 10 16 10 0
Table: global
For the second edge, the slope is not zero and the minimum y value is greater than that at zero, so it is placed in the global edge table at index=1.
Index Y-min Y-max X-val 1/m
0 10 16 10 0
1 16 20 10 1.
Table: global
For the third edge, the slope is not zero and the minimum y value is equal the edge's at index zero and the x value is greater than that at index 0, so the index is increased to 1. Since the third edge has a lesser minimum y value than the edge at index 2 of the global edge table, the index for the third edge is not increased again. The third edge is placed in the global edge table at index=1.
Index Y-min Y-max X-val 1/m
0 10 16 10 0
1 10 20 28 -1.
2 16 20 10 1.
Table: global
We continue this process until we have the following:
Index Y-min Y-max X-val 1/m
0 10 16 10 0
1 10 16 22 1
2 10 16 28 0
3 10 20 28 -1.
4 16 20 10 1.
Table: global
Notice that the global edge table has only five edges and the all_edges table has six. This is due to the fact that the last edge has a slope of zero and, therefore, is not placed in the global edge table.
3. Initializing Parity
Parity is initially set to even.
4. Initializing the Scan-Line
Since the lowest y value in the global edge table is 10, we can safely choose 10 as our initial scan-line.
First, we update the x values in the active edge table using the formula x 1 = x 0 + 1/m to get the following:
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
The edges then need to be reordered since the edge at index 3 of the active edge table has a lesser x value than that of the edge at index 2. Upon reordering, we get:
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
2. Scan-line = 11:
Once the first edge is encountered at x=10, parity = odd. All points are drawn from this point until the next edge is encountered at x=23. Parity is then changed to even. The next edge is reached at x=27 and parity is changed to odd. The points are then drawn until the next edge is reached at x=28. We are now done with this scan-line. The polygon is now filled as follows:
Upon updating the x values, the edge tables are as follows:
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
It can be seen that no reordering of edges is needed at this time.
3. Scan-line = 12:
Once the first edge is encountered at x=10, parity = odd. All points are drawn from this point until the next edge is encountered at x=24. Parity is then changed to even. The next edge is reached at x=26 and parity is changed to odd. The points are then drawn until the next edge is reached at x=28. We are now done with this scan-line. The polygon is now filled as follows:
Updating the x values in the active edge table gives us:
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
We can see that the active edges need to be reordered since the x value of 24.4 at index 2 is less than the x value of 25 at index 1. Reordering produces the following:
next edge is reached at x=28. We are now done with this scan-line. The polygon is now filled as follows:
Upon updating the x values for the active edge table, we can see that the edges still do not need to be reordered.
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
6. Scan-line = 15:
Once the first edge is encountered at x=10, parity = odd. All points are drawn from this point until the next edge is encountered at x=22. Parity is then changed to even. The next edge is reached at x=27 and parity is changed to odd. The points are then drawn until the next edge is reached at x=28. We are now done with this scan-line. The polygon is now filled as follows:
Since the maximum y value is equal to the next scan-line for the edges at indices 0, 2, and 3, we remove them from the active edge table. This leaves us with the following:
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
We then need to update the x values for all remaining edges.
Index Y- max
val
1/m Index Y-min Y-max X-val 1/m
Table: active Table: global
Now we can add the last edge from the global edge table to the active edge table since its minimum y value is equal to the next scan-line. The active edge table now looks as follows (the global edge table is now empty):
Index Y- max
val
1/m
Table: active
These edges obviously need to be reordered. After reordering, the active edge table contains the following:
Index Y- max
val
1/m
Table: active
7. Scan-line = 16:
Once the first edge is encountered at x=10, parity = odd. All points are drawn from this point until the next edge is reached at x=21. We are now done with this scan-line. The
Once the first edge is encountered at x=13, parity = odd. All points are drawn from this point until the next edge is reached at x=19. We are now done with this scan-line. The polygon is now filled as follows:
Upon updating the x values we get:
Index Y- max
val
1/m
Table: active
10. Scan-line = 19:
Once the first edge is encountered at x=15, parity = odd. All points are drawn from this point until the next edge is reached at x=18. We are now done with this scan-line. Since the maximum y value for both edges in the active edge table is equal to the next scan-line, we remove them. The active edge table is now empty and we are now done.
The polygon is now filled as follows:
Now that we have filled the polygon, let's see what it looks like to the human eye: