Download presentation
Presentation is loading. Please wait.
Published byKatrina Beasley Modified over 7 years ago
1
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A. Siddiqi
2
Filled Area Primitives
There are two basic approaches to area filling on raster systems. scan-line polygon filling Boundary-fill and Flood-fill
3
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 complex
4
Polygon types Does a straight line connecting ANY two points that are inside the polygon intersect any edges of the polygon?
5
Convex polygons If the answer is no then polygon is convex
6
Concave polygons Otherwise the polygon is concave
7
Complex polygons Complex polygons are basically concave polygons that may have self-intersecting edges
8
Unfilled polygons When an unfilled polygon is rendered, only the points on the perimeter of the polygon are drawn.
9
Filled Polygons 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.
10
Parity Parity is a concept used to determine which pixels lie within a polygon, i.e. which pixels should be filled for a given polygon.
11
ODD number of edges Point is inside the polygon filled area
12
Even number of edges I Point lies outside the polygon
13
Even number of edges II Point lies outside the circle
14
Polygon Filling 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.
15
1. Initialize all of the edges
16
For each edge, the following information needs to be kept in a table:
The minimum y value of the two vertices The maximum y value of the two vertices The x value associated with the minimum y value The slope of the edge
17
Cont… The slope of the edge can be calculated from the formula for a line y = mx + b
18
where m = slope b = y-intercept, y0 = maximum y value, y1 = minimum y value, x0 = maximum x value, x1 = minimum x value The formula for the slope is as follows: m = (y0 - y1) / (x0 - x1).
19
Table: All edges Index Y-min Y-max X-val 1/m 10 16 10 1 16 20 10 1.5 -
10 16 10 1 16 20 10 1.5 - - - - - - - - N 10 16 28 Table: All edges
20
2. Initialize the Global edge Table
Edges with the same minimum y values are sorted on minimum x values as follows: First edge with non zero slope. If the slope of an edge is zero, do not add it. For every other edge, start at index 0 and look for an appropriate position for the edge, in ascending order of minimum y-values
21
3. Initialize Parity The initial parity is even since no edges have been crossed yet.
22
4. Initializing the Scan-Line
The initial scan-line is equal to the lowest y value for all of the global edges.
23
5. Initializing the Active Edge Table
Index Y-max X-val 1/m 16 10 1 20 1.5 - N 28
24
6. Filling the Polygon Starting with the initial scan-line, until the active edge table is empty, do the following steps:
25
Step 1 Draw all pixels from the x value of odd to the x value of even parity edge pairs.
26
Step 2 Increase the scan-line by 1.
27
Step 3 Remove any edges from the active edge table for which the maximum y value is equal to the scan line.
28
Step 4 Update the x value for each edge in the active edge table using the formula x1 = x0 + 1/m (This is based on the line formula and the fact that the next scan-line equals the old scan-line plus one.)
29
Step 5 Remove any edges from the global edge table for which the minimum y value = the scan-line and place them in the active edge table.
30
Step 6 Reorder the edges in the active edge table according to increasing x value. This is done in case edges have crossed.
31
An Example (10, 10) 1 (10, 16) 2 (16, 20) 3 (28, 10) 4 (28, 16) 5 (22, 10)
32
Steps involved We will now walk through the steps of the algorithm to fill in the polygon:
33
1. Initializing All of the Edges
Index Y-min Y-max X-val 1/m 10 16 1 20 1.5 2 28 -1.2 3 4 22 5 Inf
34
2. Initializing the Global Edge Table:
Index Y-min Y-max X-val 1/m 10 16 1 22 2 28 3 20 -1.2 4 1.5
35
3. Initialize Parity Parity is initially set to Even
36
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.
37
5. Initializing the Active Edge Table
Index Y-max X-val 1/m 16 10 1 22 2 28 3 20 -1.2 4 1.5
38
6. Filling the polygon
39
6. Filling the polygon
40
6. Filling the polygon
41
6. Filling the polygon
42
6. Filling the polygon
43
6. Filling the polygon
44
6. Filling the polygon
45
6. Filling the polygon
46
6. Filling the polygon
47
6. Filling the polygon
49
Boundary fill Check the pixel for boundary color
Check the pixel for fill color Set the pixel in fill color Run the process for neighbors
50
Boundary fill Boundary fill is pixel checking and setting method that involves following steps: Check the width and height of area Get the current pixel Apply boundary checking Set the pixel Call this function recursively and set all neighboring pixels.
51
Process
52
Algorithm BoundaryFill (x, y, fillColor , boundaryColor)
if ((x < 0) || (x >= width)) then return if ((y < 0) || (y >= height)) then return current = GetPixel(x, y)
53
Algorithm if ((current != boundaryColor) && (current != fillColor))
setPixel(fillColor, x, y) BoundaryFill(x+1,y,fillColor,boundaryColor) BoundaryFill(x,y+1,fillColor,boundaryColor) BoundaryFill (x-1,y,fillColor,boundaryColor) BoundaryFill (x,y-1,fillColor,boundaryColor)
54
How about this? ?
55
Flood Fill An area fill algorithm that replaces all connected pixels of a selected color with a fill color.
58
Algorithm floodFill(x, y, fillColor, oldColor)
if ((x < 0) || (x >= width)) then return if ((y < 0) || (y >= height)) then return
59
Algorithm if ( getPixel (x, y) == oldColor) then
setPixel (fillColor, x, y) floodFill (x+1, y, fillColor, oldColor) floodFill (x, y+1, fillColor, oldColor) floodFill (x-1, y, fillColor, oldColor) floodFill (x, y-1, fillColor, oldColor)
60
Neighborhood
61
4-connected floodFill4 (x+1, y, fillColor, oldColor)
62
8-connected floodFill8 (x+1, y, fillColor, oldColor)
63
Recursion Recursive calls to functions… When can it cause problems?
Why? What can be done to prevent the problem?
64
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A. Siddiqi
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.