Filled Area Primitives
Module 2
Display and Drawing of Graphics Primitives
SPLINE CURVE
 In computer graphics, a spline is a curve that
connects two or more specific points, or
that is defined by two or more points.
Polygon
A Polygon is a plane figure that is bounded by finite
chain of straight line segments to form a closed
chain or loop
There are two types of polygons
1.Convex
2. Concave
 Convex: A convex polygon is a polygon in
which the line segment joining any two points
within the polygon lies completely inside
polygon.
 Concave: A concave polygon is a polygon in
which the line segment joining any two points
within the polygon may not lie completely
inside polygon.
Filled Area Primitives:
 For filling a given picture or object with color’s, we
can do it in two ways in C programming. The two
ways are given below:
 Using filling algorithms such as Floodfill algorithm,
Boundary fill algorithm and scanline polygon fill
algorithm, we can color the objects.
 Using inbuilt graphics functions such as
floodfill(),setfillstyle() we can fill the object with
color’s directly without using any filling algorithm.
 Here we will see the filling algorithms
The Filling Algorithms
Three Algorithms for filling areas:
 1) Boundary Fill Algorithm
 2)Flood fill Algorithm
 3) Scan Line Polygon Fill Algorithm
Boundary Fill Algorithm:
 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.
Algorithm:
 The following steps illustrate the idea of the
recursive boundary-fill algorithm:
 Start from an interior point.
 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). Store only neighboring pixel that is not
already filled and is not an edge point.
 Select the next pixel from the stack, and continue
with step 2.
 In 4 connected approach, we can fill an object in
only 4 directions. We have 4 possibilities for
proceeding to next pixel from current pixel.
 In 8 connected approach, we can fill an object in 8
directions. We have 8 possibilities for proceeding
to next pixel from current pixel.
 Function for 4 connected approach:
void boundary_fill(int x, int y, int fcolor, int bcolor)
{
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x - 1, y, fcolor, bcolor);
boundary_fill(x, y + 1, fcolor, bcolor);
boundary_fill(x, y - 1, fcolor, bcolor);
}
}
Function for 8 connected approach:
void boundary_fill(int x, int y, int fcolor, int bcolor)
{ if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x , y+1, fcolor, bcolor);
boundary_fill(x+1, y + 1, fcolor, bcolor);
boundary_fill(x-1, y - 1, fcolor, bcolor);
boundary_fill(x-1, y, fcolor, bcolor);
boundary_fill(x , y-1, fcolor, bcolor);
boundary_fill(x-1, y + 1, fcolor, bcolor);
boundary_fill(x+1, y - 1, fcolor, bcolor);
}
}
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.
 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
4 connected Flood Fill approach:
 We can implement flood fill algorithm by using
recursion.
 First all the pixels should be reassigned to common
color. here common color is black.
 Start with a point inside given object, check the
following condition: if(getpixel(x,y)==old_col)---
old_col is common color
 If above condition is satisfied, then following 4 steps
are followed in filling the object.
floodfill(x,y,fill_col,old_col)
{
if(getpixel(x,y)==old_col)
{
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
}
8 connected Flood fill approach:
 We can implement flood fill algorithm by using
recursion.
 First all the pixels should be reassigned to common
color. here common color is black.
 Start with a point inside given object, check the
following condition: if(getpixel(x,y)==old_col)---
old_col is common color
 If above condition is satisfied, then following 8 steps
are followed in filling the object.
floodfill(x,y,fill_col,old_col)
{if(getpixel(x,y)==old_col)
{putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
flood(x + 1, y - 1, fill_col, old_col);
flood(x + 1, y + 1, fill_col, old_col);
flood(x - 1, y - 1, fill_col, old_col);
flood(x - 1, y + 1, fill_col, old_col);
}
}
Scan Line Polygon Fill Algorithm
This algorithm works by intersecting scanline
with polygon edges and fills the polygon between
pairs of intersections.
Algorithm:
1. We will process the polygon edge after edge, and
store in the edge Table.
2. Storing is done by storing the edge in the same
scanline edge tuple as
the lowermost point's y-coordinate value of the
edge.
 3. After addition of any edge in an edge tuple,
the tuple is sorted using insertion sort,
according to its xofymin value.
 4. After the whole polygon is added to the
edge table, the figure is now filled.
 5. Filling is started from the first scanline at
the bottom, and continued till the top.
 6. Now the active edge table is taken and the
following things are repeated for each
scanline:
 i. Copy all edge buckets of the designated scanline
to the active edge tuple
 ii. Perform an insertion sort according to the xofymin values
 iii. Remove all edge buckets whose ymax is equal
or greater than the scanline
 iv. Fillup pairs of edges in active tuple, if any vertex is got,follow
these instructions:
 o If both lines intersecting at the vertex are on the same side of
the scanline, consider it as two points.
 o If lines intersecting at the vertex are at opposite sides of the
scanline, consider it as only one point.
 v.Update the xofymin by adding slopeinverse for each bucket.
Inside-Outside test
 When filling polygons we should decide
whether a particular point is interior or
exterior to a polygon
 Two approaches:
1. Even-odd Method
2. Winding Number Method
1. Even-odd Method:
Count=even->exterior pixel
Count=odd->interior pixel
Unit-2 PPT.ppt

Unit-2 PPT.ppt

  • 1.
    Filled Area Primitives Module2 Display and Drawing of Graphics Primitives
  • 2.
    SPLINE CURVE  Incomputer graphics, a spline is a curve that connects two or more specific points, or that is defined by two or more points.
  • 4.
    Polygon A Polygon isa plane figure that is bounded by finite chain of straight line segments to form a closed chain or loop There are two types of polygons 1.Convex 2. Concave
  • 5.
     Convex: Aconvex polygon is a polygon in which the line segment joining any two points within the polygon lies completely inside polygon.  Concave: A concave polygon is a polygon in which the line segment joining any two points within the polygon may not lie completely inside polygon.
  • 6.
    Filled Area Primitives: For filling a given picture or object with color’s, we can do it in two ways in C programming. The two ways are given below:  Using filling algorithms such as Floodfill algorithm, Boundary fill algorithm and scanline polygon fill algorithm, we can color the objects.  Using inbuilt graphics functions such as floodfill(),setfillstyle() we can fill the object with color’s directly without using any filling algorithm.  Here we will see the filling algorithms
  • 7.
    The Filling Algorithms ThreeAlgorithms for filling areas:  1) Boundary Fill Algorithm  2)Flood fill Algorithm  3) Scan Line Polygon Fill Algorithm
  • 8.
    Boundary Fill Algorithm: 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.
  • 9.
    Algorithm:  The followingsteps illustrate the idea of the recursive boundary-fill algorithm:  Start from an interior point.  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). Store only neighboring pixel that is not already filled and is not an edge point.  Select the next pixel from the stack, and continue with step 2.
  • 10.
     In 4connected approach, we can fill an object in only 4 directions. We have 4 possibilities for proceeding to next pixel from current pixel.  In 8 connected approach, we can fill an object in 8 directions. We have 8 possibilities for proceeding to next pixel from current pixel.
  • 11.
     Function for4 connected approach: void boundary_fill(int x, int y, int fcolor, int bcolor) { if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor)) { putpixel(x, y, fcolor); boundary_fill(x + 1, y, fcolor, bcolor); boundary_fill(x - 1, y, fcolor, bcolor); boundary_fill(x, y + 1, fcolor, bcolor); boundary_fill(x, y - 1, fcolor, bcolor); } }
  • 12.
    Function for 8connected approach: void boundary_fill(int x, int y, int fcolor, int bcolor) { if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor)) {putpixel(x, y, fcolor); boundary_fill(x + 1, y, fcolor, bcolor); boundary_fill(x , y+1, fcolor, bcolor); boundary_fill(x+1, y + 1, fcolor, bcolor); boundary_fill(x-1, y - 1, fcolor, bcolor); boundary_fill(x-1, y, fcolor, bcolor); boundary_fill(x , y-1, fcolor, bcolor); boundary_fill(x-1, y + 1, fcolor, bcolor); boundary_fill(x+1, y - 1, fcolor, bcolor); } }
  • 13.
    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.  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.
  • 14.
     If thearea 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
  • 16.
    4 connected FloodFill approach:  We can implement flood fill algorithm by using recursion.  First all the pixels should be reassigned to common color. here common color is black.  Start with a point inside given object, check the following condition: if(getpixel(x,y)==old_col)--- old_col is common color  If above condition is satisfied, then following 4 steps are followed in filling the object.
  • 17.
  • 18.
    8 connected Floodfill approach:  We can implement flood fill algorithm by using recursion.  First all the pixels should be reassigned to common color. here common color is black.  Start with a point inside given object, check the following condition: if(getpixel(x,y)==old_col)--- old_col is common color  If above condition is satisfied, then following 8 steps are followed in filling the object.
  • 19.
    floodfill(x,y,fill_col,old_col) {if(getpixel(x,y)==old_col) {putpixel(x,y,fill_col); flood(x+1,y,fill_col,old_col); flood(x-1,y,fill_col,old_col); flood(x,y+1,fill_col,old_col); flood(x,y-1,fill_col,old_col); flood(x + 1,y - 1, fill_col, old_col); flood(x + 1, y + 1, fill_col, old_col); flood(x - 1, y - 1, fill_col, old_col); flood(x - 1, y + 1, fill_col, old_col); } }
  • 20.
    Scan Line PolygonFill Algorithm This algorithm works by intersecting scanline with polygon edges and fills the polygon between pairs of intersections. Algorithm: 1. We will process the polygon edge after edge, and store in the edge Table. 2. Storing is done by storing the edge in the same scanline edge tuple as the lowermost point's y-coordinate value of the edge.
  • 21.
     3. Afteraddition of any edge in an edge tuple, the tuple is sorted using insertion sort, according to its xofymin value.  4. After the whole polygon is added to the edge table, the figure is now filled.  5. Filling is started from the first scanline at the bottom, and continued till the top.  6. Now the active edge table is taken and the following things are repeated for each scanline:
  • 22.
     i. Copyall edge buckets of the designated scanline to the active edge tuple  ii. Perform an insertion sort according to the xofymin values  iii. Remove all edge buckets whose ymax is equal or greater than the scanline  iv. Fillup pairs of edges in active tuple, if any vertex is got,follow these instructions:  o If both lines intersecting at the vertex are on the same side of the scanline, consider it as two points.  o If lines intersecting at the vertex are at opposite sides of the scanline, consider it as only one point.  v.Update the xofymin by adding slopeinverse for each bucket.
  • 23.
    Inside-Outside test  Whenfilling polygons we should decide whether a particular point is interior or exterior to a polygon  Two approaches: 1. Even-odd Method 2. Winding Number Method 1. Even-odd Method: Count=even->exterior pixel Count=odd->interior pixel