Unit-2 PPT.ppt

504 views 24 slides Dec 09, 2022
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Computer graphics


Slide Content

Filled Area Primitives
Module 2
Display and Drawing of Graphics Primitives

SPLINE CURVE
In computer graphics, a spline isa curve that
connects two or more specific points, or
that is defined by two or more points.

Polygon
APolygonisaplanefigurethatisboundedbyfinite
chainofstraightlinesegmentstoformaclosed
chainorloop
There are two types of polygons
1.Convex
2. Concave

Convex:Aconvexpolygonisapolygonin
whichthelinesegmentjoininganytwopoints
withinthepolygonliescompletelyinside
polygon.
Concave:Aconcavepolygonisapolygonin
whichthelinesegmentjoininganytwopoints
withinthepolygonmaynotliecompletely
insidepolygon.

Filled Area Primitives:
Forfillingagivenpictureorobjectwithcolor’s,we
candoitintwowaysinCprogramming.Thetwo
waysaregivenbelow:
UsingfillingalgorithmssuchasFloodfillalgorithm,
Boundaryfillalgorithmandscanlinepolygonfill
algorithm,wecancolortheobjects.
Usinginbuiltgraphicsfunctionssuchas
floodfill(),setfillstyle()wecanfilltheobjectwith
color’sdirectlywithoutusinganyfillingalgorithm.
Herewewillseethefillingalgorithms

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:
Startatapointinsidearegionandpaintthe
interioroutwardtowardtheboundary.
Iftheboundaryisspecifiedinasinglecolor,thefill
algorithmprocessedoutwardpixelbypixeluntil
theboundarycolorisencountered.
Aboundary-fillprocedureacceptsasinputthe
coordinateoftheinteriorpoint(x,y),afillcolor,
andaboundarycolor.

Algorithm:
Thefollowingstepsillustratetheideaofthe
recursiveboundary-fillalgorithm:
Startfromaninteriorpoint.
Ifthecurrentpixelisnotalreadyfilledandifitis
notanedgepoint,thensetthepixelwiththefill
color,andstoreitsneighboringpixels(4or8-
connected).Storeonlyneighboringpixelthatisnot
alreadyfilledandisnotanedgepoint.
Selectthenextpixelfromthestack,andcontinue
withstep2.

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(intx, inty, intfcolor, intbcolor)
{
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(intx, inty, intfcolor, intbcolor)
{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
Thisalgorithmworksbyintersectingscanline
withpolygonedgesandfillsthepolygonbetween
pairsofintersections.
Algorithm:
1.Wewillprocessthepolygonedgeafteredge,and
storeintheedgeTable.
2.Storingisdonebystoringtheedgeinthesame
scanlineedgetupleas
thelowermostpoint'sy-coordinatevalueofthe
edge.

3.Afteradditionofanyedgeinanedgetuple,
thetupleissortedusinginsertionsort,
accordingtoitsxofyminvalue.
4.Afterthewholepolygonisaddedtothe
edgetable,thefigureisnowfilled.
5.Fillingisstartedfromthefirstscanlineat
thebottom,andcontinuedtillthetop.
6.Nowtheactiveedgetableistakenandthe
followingthingsarerepeatedforeach
scanline:

i.Copyalledgebucketsofthedesignatedscanline
totheactiveedgetuple
ii.Performaninsertionsortaccordingtothexofyminvalues
iii.Removealledgebucketswhoseymaxisequal
orgreaterthanthescanline
iv.Filluppairsofedgesinactivetuple,ifanyvertexisgot,follow
theseinstructions:
oIfbothlinesintersectingatthevertexareonthesamesideof
thescanline,consideritastwopoints.
oIflinesintersectingatthevertexareatoppositesidesofthe
scanline,consideritasonlyonepoint.
v.Updatethexofyminbyaddingslopeinverseforeachbucket.

Inside-Outside test
Whenfillingpolygonsweshoulddecide
whetheraparticularpointisinterioror
exteriortoapolygon
Two approaches:
1. Even-odd Method
2. Winding Number Method
1. Even-odd Method:
Count=even->exterior pixel
Count=odd->interior pixel
Tags