You are on page 1of 45

Subject - Computer Graphics (22318)

Unit 2 (Topic 3)

- By Nitin Pawar Sir


U – 2 Raster Scan Graphics
2.1 Basic concepts of Line drawing
2.2 Circle Generating algorithm
2.3 Polygons
2.4 Scan conversion
2.5 Character generation methods
2.3 Polygon
 Means highlighting all the pixels which lie inside the
polygon.
 Colour other than background colour.
 Polygons are easier to fill.
 Two Types of Polygon
◦ - Convex Polygon
◦ - Concave Polygon
 Polygon Filling Algorithm
1. Seed Fill
i. Boundary Fill Algorithm
ii. Flood Fill Algorithm
2. Scanline Algorithm
CS 380
2.3 Polygon
Polygon is an ordered list of vertices
Polygon is a representation of the surface.
It is primitive which is closed in nature.
It is chain of connected line segments, its
start and end point is same.
2.3.1 Types of Polygon
2.3.1....
2.3.2 Representation of polygon
There are three main approaches to
represent polygon in computer graphics
system,
- Line and point approach
- Polygon drawing primitive
- Trapezoid primitive
2.3.2....
2.3.3 Entering polygon
2.3.4 Inside outside Test
In this test, we can find out if a point is
inside or outside of polygon (i.e. to
identify interior region of polygon)
There are two tests,
- Odd-evenTest
- Winding number Test
2.3.5 Polygon Filling
Filling any polygon means highlighting
(colouring) all the pixels which lie inside
the polygon with any colour other than
background colour.
Following algorithms are used to fill a
polygon,
- Scan Line algorithm
- Seed Fill (Boundary Fill, Flood Fill )
Scan Line Polygon Filling Algo.
Start with max y and move to min y or vice versa
For each scan line:
 Find the intersections of the scan line with all edges of the polygon.
 Sort the intersections by increasing x coordinate.
 Fill in all pixels between pairs of intersections

For scan line number 8 the sorted list of x-coordinates is (2, 4, 9, 13)
Therefore draw line b/w (2,4) & (9,13)
10 13 16 19

 Inthe given example, four pixel intersections are at x=10,


x=13, x=16 and x=19

 These intersection points are then sorted from left to right ,


and the corresponding frame buffer positions between each
intersection pair are set to specified color
◦ 10 to 13, and 16 to 19
1
1 2 Scan Line y’

1 2 1 1
Scan Line y
Intersection points along the scan lines
that intersect polygon vertices.
line y’ properties:
•Scan line y’ generates an even number of
intersections (crosses even number of edges).
•The two intersecting edges are both above the scan line
•We can pair the intersections to identify correctly the interior pixel spans by counting the vertex
intersection two points.
line y properties:
•Scan line y crosses odd number of edges.
•The two intersecting edges sharing a vertex are on opposite sides of the scan line
•To identify the interior pixels for scan line y, we must count the vertex intersection as only one
point.
Scan Line Polygon Filling Algo.

Assume that we are taking scan line from max y and


moving towards min y

 We know that for every scan line we have to calculate x


intersection with every polygon side. We can determine the
next x intersection value as

 xi+1=xi - 1/m where m is the slope of edge


 Itis very easy to identify which polygon sides should be tested
for x-intersection, if we sort edges in order of maximum y
and find active edge list (Active edge list for scan line
contains all the edges crossed by that scan line ).

 Two pointers are used to indicate the beginning and ending of


active edge list. As the scan line move down the picture the
beginning and end pointers also moved down to eliminate the
edges which end above the current scan line and include the
new edges which now start on or above the current scan line .
Scan line algo. Steps,
 1. Read n, the number or vertices or polygon.
 2. Read x and y coordinates of all vertices in array x[n] and
y[n].
 3. Find ymin and ymax.
 4. Sort Polygon edges from ymax to ymin.
 5. For each y value (from max to min) determines which
sides can be intersected and find x values of these
intersection points.
 6.Sort these x values, pairs x values.
 7.Pass these x value pairs(with scan line y value) to line
drawing command ie. To set those pixels to same colour.
 8.Stop
Boundary Fill Algorithm

This algorithm picks a point inside the polygon and starts


to fill until it hits the boundary of the object.

Assumption:

In this algorithm, we assume that color of the boundary is


same for the entire object.
Boundary Fill Algorithm (Code)

void boundaryfill(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);
boundaryfill(x+1,y,fill_color,boundary_color);
boundaryfill(x-1,y, fill_color,boundary_color);
boundaryfill(x,y+1, fill_color,boundary_color);
boundaryfill(x,y-1, fill_color,boundary_color);
}
}
Flood Fill Algorithm

Sometimes we want to fill in 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.
Flood Fill Algorithm (Code)

void floodfill(int x,int y,int fill_color,int old_color)


{
if(getpixel(x,y)==old_color)
{
putpixel(x,y,fill_color);
floodfill(x+1,y,fill_color,old_color);
floodfill(x-1,y, fill_color,old_color);
floodfill(x,y+1, fill_color,old_color);
floodfill(x,y-1, fill_color,old_color);
}
}
Boundary Fill Algorithm /Flood Fill algorithm

The boundary fill algorithm/ flood fill algorithm can be implemented by 4-


connected pixels or 8-connected pixels.
4-connected (Example)

Start Position
3
2
1
1
2 3
4
2
1
1 4
2
2
1

1
2
5
1

5 1
1

1
Some region remains unfilled
8-connected (Example)

Start Position
5
4
4 1 5 3
2 3 2
1
6
6 4
4 1 3
2 3 2
1
8
7 8
7
4
4 1
3
2 3
2
1
12
11 9 12 11

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

2 3 2
1
3
1
2
2 3
1
1 2
2 1
1
1
Thank
you....

You might also like