Computer Graphics Polygon Filling
Computer Graphics Polygon Filling
Polygon Filling
1
Polygon Filling
Types of filling
• Solid-fill
All the pixels inside the polygon’s boundary
are illuminated.
• Pattern-fill
the polygon is filled with an arbitrary
predefined pattern.
2
Polygon Representation
The polygon can be represented by listing its n
vertices in an ordered list.
P = {(x1, y1), (x2, y2), ……., (xn, yn)}.
The polygon can be displayed by drawing a line
between (x1, y1), and (x2, y2), then a line between
(x2, y2), and (x3, y3), and so on until the end vertex.
In order to close up the polygon, a line between (xn,
yn), and (x1, y1) must be drawn.
One problem with this representation is that if we
wish to translate the polygon, it is necessary to
apply the translation transformation to each vertex
in order to obtain the translated polygon.
3
Polygon Representation
For objects described by many polygons with many
vertices, this can be a time consuming process.
One method for reducing the computational
time is to represent the polygon by the (absolute)
location of its first vertex, and represent
subsequent vertices as relative positions from the
previous vertex. This enables us to translate the
polygon simply by changing the coordinates of the
first vertex.
4
Filling General Polygons
• Specifying the interior
– Must be able to determine which points are inside the polygon
• Need a fill rule
Filling General Polygons
• Specifying the interior
– There are two commonly used fill rules
• Even-odd parity rule
• Non-zero winding rule
Filled using even-odd parity rule Filled using none-zero winding rule
Even-odd Parity Rule
• To determine if a point P is inside or outside
– Draw a line from P to infinity
– Count the number of times the line crosses an edge
• If the number of crossing is odd, the point is inside
• If the number of crossing is even, the point is outside
Non-zero Winding Number
Rule
• The outline of the shape must be directed
– The line segments must have a consistent direction so that
they formed a continuous, closed path
Non-zero Winding Number
Rule
• To determine if a points is inside or outside
– Determine the winding number (i.e. the number of times the edge
winds around the point in either a clockwise or counterclockwise
direction)
• Points are outside if the winding number is zero
• Point are inside if the winding number is not zero
Non-zero Winding Number
Rule
• To determine the winding number at a point P
– Initialize the winding number to zero and draw a line (e.g.
horizontal) from P to infinity
– If the line crosses an edge directed bottom to up
• Add 1 to the winding number
– If the line crosses an edge directed top to bottom
• Subtract 1 from the winding number
Comparison between Odd Even Rule and Nonzero
Winding Rule
• For standard polygons and simple object both rule gives same result but for more
complicated shape both rule gives different result.
Vertex between two upwards edges Vertex between upwards and downwards edges
- Process a single crossing - Process no crossings
Dealing with Shared Vertices
2. Ensure that the line does not intersect a vertex
– Use a different line if the first line intersects a vertex
• Could be costly if you have to try several lines
25
Boundary Fill Algorithm
The following steps illustrate the idea of the
recursive boundary-fill algorithm:
26
Boundary Fill Algorithm
27
Boundary Fill Algorithm
4-connected (Example)
Start Position
28
Boundary Fill Algorithm
4-connected (Example)
3
2
1
1
2 3
29
Boundary Fill Algorithm
4-connected (Example)
4
2
1 4
1
2
30
Boundary Fill Algorithm
4-connected (Example)
2
1
1
2
31
Boundary Fill Algorithm
4-connected (Example)
5
5 1
1
32
Boundary Fill Algorithm
4-connected (Example)
1
1
33
Boundary Fill Algorithm
4-connected (Example)
34
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
Boundary Fill Algorithm
8-connected (Example)
Start Position
36
Boundary Fill Algorithm
8-connected (Example)
4 1 5
5
2 3
4
3
2
1
37
Boundary Fill Algorithm
8-connected (Example)
6
4 1
6
2 3
4
3
2
1
38
Boundary Fill Algorithm
8-connected (Example)
7 8
8
4 1
7
2 3
4
3
2
1
39
Boundary Fill Algorithm
8-connected (Example)
12
11 9 12
7 10 11
10
4 1
2 3
9
7
4
3
2
1
40
Boundary Fill Algorithm
8-connected (Example)
11 9
11
7 10
10
4 1
9
2 3
7
4
3
2
1
41
Boundary Fill Algorithm
8-connected (Example)
9
7 10
10
4 1 9
2 3 7
4
3
2
1
42
Boundary Fill Algorithm
8-connected (Example)
9
7
9
4 1
7
2 3
4
3
2
1
43
Boundary Fill Algorithm
8-connected (Example)
4 1
7
2 3
4
3
2
1
44
Boundary Fill Algorithm
8-connected (Example)
4 1
2 3
4
3
2
1
45
Boundary Fill Algorithm
8-connected (Example)
1
2 3
3
2
1
46
Boundary Fill Algorithm
8-connected (Example)
1
2
2
1
47
Boundary Fill Algorithm
8-connected (Example)
48
Boundary Fill Algorithm
8-connected (Example)
49
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}
Problem of Staking and Efficient
Method
▪ Same procedure can be modified according to 8 connected region
algorithm by including four additional statements to test diagonal
positions.
▪ This procedure requires considerable stacking of neighbouring
points more, efficient methods are generally employed.
▪ Efficient method fill horizontal pixel spans across scan lines,
instead of proceeding to 4 connected or 8 connected neighbouring
points.
▪ Then we need only stack a beginning position for each horizontal
pixel span, instead of stacking all unprocessed neighbouring
positions around the current position.
▪ Starting from the initial interior point with this method, we first fill
in the contiguous span of pixels on this starting scan line.
Contd.
▪ Then we locate and stack starting positions for spans on the
adjacent scan lines.
▪ Spans are defined as the contiguous horizontal string of positions
bounded by pixels displayed in the area border colour.
▪ At each subsequent step, we unstack the next start position and
repeat the process.
▪ For e.g.
Contd.
3
2
(a) (b)
1 2 1 3
1 1
5 6 5
6
(c) 4 5 (d) 4 5
1 4 1 4
1 1
Boundary Fill Algorithm
54
Flood Fill Algorithm
Sometimes we want to fill in (recolor) an area that is
not defined within a single color boundary.
We paint such areas by replacing a specified
interior color instead of searching for a boundary
color value.
55
Flood Fill Algorithm
We start from a specified interior pixel (x, y) and
reassign all pixel values that are currently set to a
given interior color with the desired fill color.
If the area has more than one interior color, we
can first reassign pixel values so that all interior
pixels have the same color.
Using either 4-connected or 8-connected
approach, we then step through pixel positions
until all interior pixels have been repainted.
56
Introduction to Flood-Fill
Algorithm
▪ Sometimes it is required to fill in an area that is not defined within a single
colour boundary.
▪ In such cases we can fill areas by replacing a specified interior colour instead of
searching for a boundary colour.
▪ This approach is called a flood-fill algorithm. Like boundary fill algorithm, here
we start with some seed and examine the neighbouring pixels.
▪ However, here pixels are checked for a specified interior colour instead of
boundary colour and they are replaced by new colour.
▪ Using either a 4-connected or 8-connected approach, we can step through pixel
positions until all interior point have been filled.
Procedure floodfill (x, y,fill_ color, old_color:
integer)
If (getpixel (x, y)=old_color)
{
setpixel (x, y, fill_color);
fill (x+1, y, fill_color, old_color);
fill (x-1, y, fill_color, old_color);
fill (x, y+1, fill_color, old_color);
fill (x, y-1, fill_color, old_color);
}
}
Scan-Line Polygon Fill Algorithm
▪ For each scan-line crossing a polygon, the algorithm locates the
intersection points are of scan line with the polygon edges.
▪ This intersection points are stored from left to right.
▪ Frame buffer positions between each pair of intersection point are
set to specified fill color.
Scan
line
Contd.
▪ Scan line intersects at vertex are required special handling.
▪ For vertex we must look at the other endpoints of the two line
segments which meet at this vertex.
• If these points lie on the same (up or down) side of the scan line, then that
point is counts as two intersection points.
• If they lie on opposite sides of the scan line, then the point is counted as
single intersection.
Scan
line
Scan
line
Scan
line
Edge Intersection Calculation with Scan-
Line
Contd.
Edge Intersection Calculation with Scan-Line for parallel
execution
Simplified Method for Edge Intersection Calculation with
Scan-Line
Counter
=3
=2
=4
=0
=5
=6
=1
Use of Sorted Edge table in Scan-Line Polygon Fill
Algorithm
Contd.
Scan
Line
Numbe
B .
r .
.
C .
.
.
C E
D .
.
A .
1
0
Character Generation
▪ We can display letters and numbers in variety of size and style.
▪ The overall design style for the set of character is called typeface.
▪ Today large numbers of typefaces are available for computer
application for example Helvetica, Arial etc.
▪ Originally, the term font referred to a set of cast metal character
forms in a particular size and format.
▪ Example: 10-point Courier Italic or 12- point Palatino Bold.
The Scan-Line Polygon Fill Algorithm
The scan-line polygon-filling algorithm involves
• the horizontal scanning of the polygon from its lowermost to its
topmost vertex,
• identifying which edges intersect the scan-line,
• and finally drawing the interior horizontal lines with the specified
fill color. process.
68
The Scan-Line Polygon Fill Algorithm
Dealing with vertices
69
The Scan-Line Polygon Fill Algorithm
Dealing with vertices
70
The Scan-Line Polygon Fill Algorithm
Determining Edge Intersections
m = (yk+1 – yk) / (xk+1 – xk)
yk+1 – yk = 1
xk+1 = xk + 1/m
71
The Scan-Line Polygon Fill Algorithm
• Each entry in the table for a particular scan line contains
the maximum y value for that edge, the x-intercept value
(at the lower vertex) for the edge, and the inverse slope of
the edge.
72
The Scan-Line Polygon Fill Algorithm
(Example) Polygon = {A, B, C, D, E, F, G}
Polygon = {(2, 7), (4, 12), (8,15), (16, 9), (11, 5), (8, 7), (5, 5)}
73
The Scan-Line Polygon Fill Algorithm
(Example)
74
The Scan-Line Polygon Fill Algorithm
(Example)
75
The Scan-Line Polygon Fill Algorithm
(Example)
• Vertex B should be split into two vertices B' (xB', 11) and
B(4, 12)
m =( 7 – 12)/( 2 – 4) = 5/2
x'A = 2 + (2/5)( 12 – 1 – 7) = 18/5 = 3.6 4
The vertex B is split to B' (4, 11) and B(4, 12)
76
The Scan-Line Polygon Fill Algorithm
(Example)
77
The Scan-Line Polygon Fill Algorithm
(Example)
78
The Scan-Line Polygon Fill Algorithm
(Example)
79
The Scan-Line Polygon Fill Algorithm
(Example)
80
The Scan-Line Polygon Fill Algorithm
(Example)
81
The Scan-Line Polygon Fill Algorithm
(Example)
82
Contd.
▪ Now, the terms font and typeface are often used interchangeably,
since printing is no longer done with cast metal forms.
▪ Methods of character generation are:
➢ Bitmap Font/ Bitmapped Font
➢ Outline Font
➢ Stroke Method
➢ Starbust Method
Bitmap Font/ Bitmapped Font
▪ A simple method for representing the character 0 0 1 1 1 1 0 0
shapes in a particular typeface is to use 0 1 1 1 1 1 1 0
1 1 0 0 0 0 1 1
rectangular grid patterns. 1 1 0 0 0 0 1 1
1 1 1 1 1 1 1 1
▪ In frame buffer, the 1 bits designate which 1 1 1 1 1 1 1 1
pixel positions are to be displayed on the 1 1 0 0 0 0 1 1
1 1 0 0 0 0 1 1
monitor.
▪ Bitmap fonts are the simplest to define and display.
▪ Bitmap fonts require more space.
▪ It is possible to generate different size and other variation from one
set but this usually does not produce good result.
Outline Font
▪ In this method character is generated using curve
section and straight line as combine assembly.
▪ To display the character we need to fill interior
region of the character.
▪ This method requires less storage since each
variation does not required a distinct font cache.
▪ We can produce boldface, italic, or different sizes
by manipulating the curve definitions for the
character outlines.
▪ But this will take more time to process the outline
fonts, because they must be scan converted into the
frame buffer.
Stroke Method
• It uses small line segments to generate
a character.
• The small series of line segments are
drawn like a stroke of a pen to form a
character.
• We can generate our own stroke method by calling line
drawing algorithm.
• Here it is necessary to decide which line segments are
needs for each character and then draw that line to display
character.
• It support scaling by changing length of line segment.
Starbust Method
▪ In this method a fix pattern of lines (24 line) segments are used to
generate characters. 03 04
13 14
02 23 05
17 18
01 06
21
12 22 07
20
24 19
11 08
16 15
10 09
2 Dashed
3 Dotted
4 Dotdas
h lines by setting
▪ We modify a line –drawing algorithm to generate such
the length and spacing of displayed solid sections along the line path.
▪ To set line type attributes in a PHIGS application program, a user
invokes the function: setLinetype(It)
▪ Where parameter lt is assigned a positive integer value of 1, 2, 3, 4… etc.
to generate lines that are, respectively solid, dashed, dotted, or dotdash
etc.
Line Width
▪ Implementation of line-width options depends on the capabilities
of the output device.
▪ A heavy line on a video monitor could be displayed as adjacent
parallel lines, while a pen plotter might require pen changes.
▪ To set line width attributes in a PHIGS application program, a user
invokes the function: setLinewidthScalFactor (lw)
▪ Line-width parameter lw is assigned a positive number to indicate
the relative width of the line to be displayed.
▪ Values greater than 1 produce lines thicker than the standard line
width and values less than the 1 produce line thinner than the
standard line width.
Contd.
▪ In raster graphics we generate thick line by plotting
• above and below pixel of line path when slope |m|<1. &
• left and right pixel of line path when slope |m|>1.
Line Width at Endpoints and Join
▪ As we change width of the line we can also change line end and
join of two lines which are shown below