Sei sulla pagina 1di 77

CS 415

Computer graphics
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
Inside-Outside Tests
when filling polygons we should decide whether a
particular point is interior or exterior to a polygon.
A rule called the odd-parity (or the odd-even rule)
is applied to test whether a point is interior or not.
To apply this rule, we conceptually draw a line
starting from the particular point and extending to a
distance point outside the coordinate extends of the
object in any direction such that no polygon vertex
intersects with the line.

5
Inside-Outside Tests
The point is considered to be interior if the number
of intersections between the line and the polygon
edges is odd. Otherwise, The point is exterior point.

Outside 8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11
Inside

6
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.

7
The Scan-Line Polygon Fill Algorithm
Dealing with vertices

8
The Scan-Line Polygon Fill Algorithm
Dealing with vertices
• When the endpoint y coordinates of the two edges are
increasing, the y value of the upper endpoint for the
current edge is decreased by one (a)
• When the endpoint y values are decreasing, the y value
of the next edge is decreased by one (b)

9
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

10
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.

11
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)}

12
The Scan-Line Polygon Fill Algorithm
(Example)

13
The Scan-Line Polygon Fill Algorithm
(Example)

• Vertex A should be split into two vertices A' (xA', 6) and


A(2, 7)
m =( 5 – 7)/( 5 – 2) = – 2/3
x'A = 5 + (–3/2)( 7 – 1 – 5) = 7/2 = 3.5  4
The vertex A is split to A' (4, 6) and A(2, 7)
14
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)
15
The Scan-Line Polygon Fill Algorithm
(Example)

• Vertex D should be split into two vertices D(16, 9) and D'


(xD', 8)
m =( 5 – 9)/( 11 – 16) = 4/5
x'D = 11 + (5/4)( 9 – 1 – 5) = 59/4 = 14.75  15
The vertex D is split to D(16, 9) and D' (15, 8)
16
The Scan-Line Polygon Fill Algorithm
(Example)

17
The Scan-Line Polygon Fill Algorithm
(Example)

18
The Scan-Line Polygon Fill Algorithm
(Example)

19
The Scan-Line Polygon Fill Algorithm
(Example)

20
The Scan-Line Polygon Fill Algorithm
(Example)

21
Boundary Fill Algorithm
• Another approach to area filling is to start at a point
inside a region and paint the interior outward toward
the boundary.
• If the boundary is specified in a single color, the fill
algorithm processed outward pixel by pixel until the
boundary color is encountered.
• A boundary-fill procedure accepts as input the
coordinate of the interior point (x, y), a fill color,
and a boundary color.

22
void BoundaryFill4(int x, int y,
color newcolor, color edgecolor)
{
int current;
current = ReadPixel(x, y);
if(current != edgecolor && current != newcolor)
{
BoundaryFill4(x+1, y, newcolor, edgecolor);
BoundaryFill4(x-1, y, newcolor, edgecolor);
BoundaryFill4(x, y+1, newcolor, edgecolor);
BoundaryFill4(x, y-1, newcolor, edgecolor);
}
}

23
Boundary Fill Algorithm
The following steps illustrate the idea of the
recursive boundary-fill algorithm:

1. Start from an interior point.


2. If the current pixel is not already filled and if it is
not an edge point, then set the pixel with the fill
color, and store its neighboring pixels (4 or 8-
connected) in the stack for processing. Store only
neighboring pixel that is not already filled and is not
an edge point.
3. Select the next pixel from the stack, and continue
with step 2.

24
Boundary Fill Algorithm

The order of pixels that should be added to stack


using 4-connected is above, below, left, and right.
For 8-connected is above, below, left, right, above-
left, above-right, below-left, and below-right.

25
Boundary Fill Algorithm
4-connected (Example)

Start Position

26
Boundary Fill Algorithm
4-connected (Example)

3
2
1
1
2 3

27
Boundary Fill Algorithm
4-connected (Example)

4
2
1 4
1
2

28
Boundary Fill Algorithm
4-connected (Example)

2
1
1
2

29
Boundary Fill Algorithm
4-connected (Example)

5
5 1
1

30
Boundary Fill Algorithm
4-connected (Example)

1
1

31
Boundary Fill Algorithm
4-connected (Example)

32
Boundary Fill Algorithm
8-connected (Example)

Start Position

33
Boundary Fill Algorithm
8-connected (Example)

4 1 5
5
2 3
4
3
2
1

34
Boundary Fill Algorithm
8-connected (Example)

6
4 1
6
2 3
4
3
2
1

35
Boundary Fill Algorithm
8-connected (Example)

7 8

8
4 1
7
2 3
4
3
2
1

36
Boundary Fill Algorithm
8-connected (Example)

12
11 9 12
11
7 10
10
9
4 1
7
2 3
4
3
2
1

37
Boundary Fill Algorithm
8-connected (Example)

11 9
11
7 10
10
9
4 1
7
2 3
4
3
2
1

38
Boundary Fill Algorithm
8-connected (Example)

9
7 10
10
9
4 1
7
2 3
4
3
2
1

39
Boundary Fill Algorithm
8-connected (Example)

9
7

9
4 1
7
2 3
4
3
2
1

40
Boundary Fill Algorithm
8-connected (Example)

4 1
7
2 3
4
3
2
1

41
Boundary Fill Algorithm
8-connected (Example)

4 1
2 3
4
3
2
1

42
Boundary Fill Algorithm
8-connected (Example)

1
2 3

3
2
1

43
Boundary Fill Algorithm
8-connected (Example)

1
2

2
1

44
Boundary Fill Algorithm
8-connected (Example)

45
Boundary Fill Algorithm
8-connected (Example)

46
Boundary Fill Algorithm

Since the previous procedure requires considerable


stacking of neighboring pixels, more efficient
methods are generally employed.
These methods (Span Flood-Fill) fill horizontal
pixel spans across scan lines, instead of
proceeding to 4-connected or 8-connected
neighboring pixels.
Then we need only stack a beginning position for
each horizontal pixel spans, instead of stacking all
unprocessed neighboring positions around the
current position.
47
Span Flood-Fill Algorithm

The algorithm is summarized as follows:


• Starting from the initial interior pixel, then fill in the
contiguous span of pixels on this starting scan line.
• Then locate and stack starting positions for spans
on the adjacent scan lines, where spans are defined
as the contiguous horizontal string of positions
bounded by pixels displayed in the area border
color.
• At each subsequent step, unstack the next start
position and repeat the process.

48
Span Flood-Fill Algorithm (example)

11
10
9
8
7
6
5 S
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

49
Span Flood-Fill Algorithm (example)

11
10
9
8
7
6 2
5 S
4 1
2
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

50
Span Flood-Fill Algorithm (example)

11
10
9
8
7 3
6 2
5 S
4 1
3
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

51
Span Flood-Fill Algorithm (example)

11
10
9
8 5 6
7 3
6 4 2
6
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

52
Span Flood-Fill Algorithm (example)

11
10
9 7
8 5 6
7 3
6 4 2
7
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

53
Span Flood-Fill Algorithm (example)

11
10
9 7
8 5 6
7 3
6 4 2
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

54
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 S
8
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

55
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 S
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

56
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 1
9
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

57
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
10
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

58
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
11
3 11
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

59
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
12
3 11
1
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

60
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
3 11
1
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

61
Span Flood-Fill Algorithm (example)

11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
3 11
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

62
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.

This approach is called a flood-fill algorithm.

63
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.

64
void FloodFill4(int x, int y, color newcolor, color oldColor)
{
if(ReadPixel(x, y) == oldColor)
{
FloodFill4(x+1, y, newcolor, oldColor);
FloodFill4(x-1, y, newcolor, oldColor);
FloodFill4(x, y+1, newcolor, oldColor);
FloodFill4(x, y-1, newcolor, oldColor);
}
}

65
Setting frame-buffer values

After generating a line or circle using scan


conversion, we need to know where is the specified
pixels are stored in the frame buffer.

Frame buffer is stored in memory as an addressable


array and stored row by row.

Pixels are labeled from (0, 0) the lower left corner to


(xmax,ymax) the top right corner.

66
Row by Row mapping

67
Setting frame-buffer values (cont.)
For a bilevel system (1 bit per pixel),the Frame-Buffer bit address for
pixel position (x, y) is calculated as:

1. addr(x, y) = addr (0,0) + y (xmax +1) + x

2. addr(x + 1, y) = addr(x, y) + 1

3. addr(x + 1, y + 1) = addr(x, y) + xmax + 2

Example:
Find the address of the pixel (6,5), where the address
of (0,0) =500, xmax=12, and ymax = 10.

addr(x, y) = addr (0,0) + y(xmax +1) + x


addr(6, 5) = 500 + 5(12 + 1) + 6 = 571

addr(7, 5) = addr(6, 5) + 1 = 572


addr(7, 6) = addr(6, 5) + xmax + 2 = 585 68
Pixel addressing and object geometry
•When an object is scan converted into the frame buffer, the input
description is transformed to pixel coordinates.

•So, the displayed image may not correspond exactly with the
relative dimensions of the input object.

•To preserve the specified geometry of world objects, we need to


compensate for the mapping of mathematical input points to finite
pixel area, we use one of the two ways:

1) Adjust the dimensions of displayed objects to account for the


amount of overlap of pixel areas with the object boundaries.
(i.e. a rectangle with 40 cm width, will be displayed in 40 pixel)

2) Map world coordinates onto screen positions between pixels, so


that we align objects boundaries with pixel boundaries instead of
pixel centers.
69
Pixel addressing and object geometry (cont.)

Screen Grid Coordinates:

An alternative to addressing
display positions in terms of pixel
centers is to reference screen
coordinates with respect to the grid
of horizontal and vertical pixel
boundary lines spaced one unit a
part.

70
Pixel addressing and object geometry (cont.)
•Screen coordinate position is then
the pair of integer values
identifying a grid intersection
position between two pixels.

•For example, the mathematical


line path for a polyline with screen
endpoints (0, 0), (5, 2), and (1,4) is
shown beside.

71
Pixel addressing and object geometry (cont.)

•With the coordinate origin at the


lower left of the screen, each pixel
area can be referenced by the
integer grid coordinates of its
lower left corner.

•The following figure illustrates


this convention for an 8 by 8
section of a raster, with a single
illuminated pixel at screen
coordinate position (4, 5).

72
Pixel addressing and object geometry (cont.)

In general, we identify the area occupied by a pixel with


screen coordinates ( x, y) as the unit square with
diagonally opposite corners at (x, y) and ( x + 1, y + 1 ).
This pixel addressing scheme has several advantages:
1. It avoids half-integer pixel boundary
2. It facilitates precise object representations.
3. Simplifies the processing involved in many scan
conversion algorithms and in other raster
procedures

73
Pixel addressing and object geometry (cont.)

Notes:

1) The previous algorithms for drawing line,


circle, …etc are still valid when applied to input
positions expressed as screen grid coordinates.

2) The decision parameter Pk is a measure of


screen grid separation differences rather than
separation differences from pixel centers.

74
Pixel addressing and object geometry (cont.)

A circle of radius 5 and center position (10, 10), for instance,


would be displayed by the midpoint circle algorithm using
screen grid coordinate positions.

But the plotted circle has a diameter of 11, To plot the circle
with the defined diameter of 10, we can modify the circle
algorithm to shorten each pixel scan line and each pixel
column.

75
Pixel addressing and object geometry (cont.)

76
Pixel addressing and object geometry (cont.)

77

Potrebbero piacerti anche