Output primitives
 A picture can be describedinseveral ways.
o In a raster display,apicture iscompletelyspecifiedbythe setof intensitiesforthe
pixel positionsinthe display.
At the otherextreme,apicture canbe describedasa setof complex objects,suchastreesandterrain
or furniture andwalls,positionedatspecifiedcoordinatelocationswithinthe scene
Point Plotting
· Pointplottingisattainedbyconvertingasingle coordinate positionfurnishedbyan
applicationprogramintoappropriate operationsforoutputdeviceinuse.
· For example, with a CRT monitor, the electron beam is turned on to illuminate the screen
phosphor at the selected location. The electron beam is positioned based on the display
technology.
Line Drawing
· The line is the most fundamental drawingprimitive withmanyuses - charts,engineering
drawings,illustrations,2Dpencil-basedanimationandcurve approximation.
· Line drawing is accomplished by calculating intermediate positions along directed to fill in
these positionsbetween the endpoint positions. An output device is then directed to fill in
these positions between the endpoints.
The Cartesianslope-interceptequationforastraightline is
y = m. x + b – (1)
DDA Algorithm
 The digital differential analyzer(DDA) isascan-conversionline algorithmbased
on calculatingeitheryor x.
 The line issampledatunitintervalsinone coordinate andcorrespondinginteger
valuesnearestthe line pathforthe othercoordinate are determined.
Algorithm:
Step1
Inputtwo endpointpixel positions(x1,y1) and(x2,y2)
Step2
Findhorizontal andvertical difference betweenthe endpoints
dx = x2-x1
dy = y2-y1
Step3
The difference withthe greatermagnitudedeterminesthe value of parametersteps
If abs (dx)>abs(dy) then
steps= abs (dx)
Else
steps= abs (dy)
Step4
Startingwithpixel position(x1,y1) be determinedoffsetneededateachstepto generate nextpixel
alongthe line path.
xIncrement=dx/steps
yIincrement=dy/steps
Step5
Assignthe valuesof x1,y1 tox, y
x = x1
y = y1
Step6
Plotthe pixel at(x,y) positiononscreensetpixel (round(x),round(y),1).Here ‘1’is the intensity
of pixel i.e.,intensitywithwhichpicture isilluminated.
step7
Calculate the valuesof x andy for the nextpixel position.
x = x + xIncrement
y = y + yIncrement
Step8
Plotthe pixel at(x,y) position,setpixel (round(x),round(y),1)
Step9
Repeatthe steps7 and 8 until the value of I i.e.,startfrom 1 tosteps.
EXAMPLE: Draw A line betweenthe points (20,10) and(30,18)
Step1: x1 = 20, x2 =30, y1 = 10, y2 = 18.
Step2: dx = 30 -20 = 10, dy = 18 – 10 = 8.
Step3: dx > dy=> 10 >8, steps=10.
Step4: x increment= 10/10 = dx/steps=1.
y increment = 8/10 = dy/steps = 0.8.
Step5: x = 20, y = 10.
Step6: pixel (20,10) .
Step7: x = x + x increment.
y = y + y increment.
I x y (round(x),round(y))
1 20+1=21 10+0.8=10.8 (21,11)
2 21+1=22 10.8+0.8=11.6 (22,12)
3 22+1=23 11.6+0.8=12.4 (23, 12)
4 23+1=24 12.4+0.8=13.2 (24,13)
5 24+1=25 13.2+0.8=14 (25,14)
6 25+1=26 14+0.8=14.8 (26,15)
7 26+1=27 14.8+0.8=15.6 (27,16)
8 27+1=28 15.6+0.8=16.4 (28,16)
9 28+1=29 16.4+0.8=17.2 (29,17)
10 29+1=30 17.2+0.8=18 (30,18)
Advantages:
1. It is the simplest algorithm
2. It is a is a faster method for calculating pixel positions
Disadvantages of DDA Algorithm
1. Floating point arithmetic in DDA algorithm is still time-consuming
2. End point accuracy is poor
Bresenham’s Line Algorithm
 An accurate and efficient raster line-generating algorithm, developed by Bresenham, scans converts
lines using only incrementa1 integer calculations that can be adapted to display circles and other
curves.
· Algorithm:
·
· Step1
·
· Inputthe two line endpointsandstore the leftendpointsin(x0,y0)
·
· Step2
·
· Load (x0,y0) intothe frame buffer;plotthe firstpoint.
·
· Step3
·
· Calculate constants x, y,2y, and 2y - 2x, andobtainthe startingvalue
for the decisionparameteras
·
· x = xn - x0 and y= yn - y0
·
· p0 = 2y - x
· Step4
·
· At eachxk alongthe line,startingatk = 0, performthe followingtest:
·
· If pk < 0, the nextpointtoplotis (xk+1,yk) and
·
· pk+1 = pk +2y
·
· Otherwise,the nextpointtoplotis(xk+1,yk+1) and
·
· pk+1 = pk +2y -2x
·
· Step5
·
· Repeatstep4 x times.
EXAMPLE: Draw A line betweenthe points(20,10) and(30,18)
·
· Thisline hasa slope of 0.8, with
·
· x = 10 , y = 8
·
· The initial decisionparameterhasthe value
·
· p0 = 2y - x
·
· = 6
·
· and the incrementsforcalculatingsuccessive decisionparametersare
·
· 2y = 16
·
· 2y - 2x = -4
·
· We plotthe initial point(xo,yo)=(20, 10), anddetermine successivepixel positions
alongthe line pathfromthe decisionparameteras
Advantages
Algorithm is Fast
Uses only integer calculations
Disadvantages
It is meant only for basic line drawing.
Mid-Point circle Algorithm
 As inthe raster line algorithm, unitintervalsare takenandthe closestpixel positionis
determinedtothe specifiedcircle pathat each step.
 For a givenradiusr and screencenterposition(xc,yc),the algorithmisfirstsetupto
calculate pixel positionsaroundacircle pathcenteredat the coordinate origin(0,0).
Algorithm:
·
· Step1
·
· Inputradius
r and circle center(xc,yc) andobtainthe firstpointonthe circumference of a circle
centeredonthe originas
k Pk
X,
Y
0 6 (21,11)
1 2 (22,12)
2 -2 (23,12)
3 14 (24,13)
4 10 (25,14)
5 6 (26,15)
6 2 (27,16)
algorithm.
7 -2 (28,16)
8 14 (29,17)
9 10 (30,18)
(x0, y0) = (0, r)
Step2
Calculate the initial value of the decisionparameteras
p0 = 5/4 – r
Step3
At eachxk positionstartingatk=0, performthe followingtest:
If pk<0, the nextpointalongthe circle centeredon(0,0) is (xk+1,yk) and
pk+1 = pk + 2xk+1 +1
Otherwise,the nextpointalongthe circle is(xk +1, yk - 1) and
pk+1 = pk + 2xk+1 +1 – 2yk+1
where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk – 2.
Step4
Determine symmetrypointsinthe othersevenoctants.
Step5
Move eachcalculatedpixel position(x,y) ontothe circularpathcenteredon(xc,yc) andplotthe
coordinate values:
x = x + xc, y = y + yc.
Step6
Repeatsteps3 through5 until x >= y.
Example:
 Givena circle radiusr= 10, midpointcircle algorithmisdemonstratedbydeterminingpositions
alongthe circle octant inthe firstquadrantfromx=0 to x=y
 The initial value of the decisionparameteris
p0 = 1 – r = -9
 For the circle centeredonthe coordinate origin,the initial pointis(x0,y0)=(0,10) andinitial
incrementtermsforcalculatingthe decisionparametersare
2x0=0 and 2y0=20
 Successive decisionparametervaluesandpositionsalongthe circle pathare calculatedusing
the midpointmethodas
k pk (xk+1,yk+1) 2x k+1 2y k+1
0 -9 (1,10) 2 20
1 -6 (2,10) 4 20
2 -1 (3,10) 6 20
3 6 (4,9) 8 18
4 -3 (5,9) 10 18
5 8 (6,8) 12 16
Filled area primitives
 Areais filledwithacertaincolor
 Most oftenthe shape isthat of a polygon
 Boundariesare linearastheyare polygons
 Standardgraphicsobjectsare objectsmade of a setof polygonsurface patches.
 There are twobasicapproachesto area fillingonrastersystems.
o To fill anarea isto determine the overlapintervalsforscanlinesthatcrossthe area.
o Areafillingistostart froma giveninteriorpositionandpaintoutwardfromthis
pointuntil we encounter the specifiedboundaryconditions.
Boundary-fill Algorithm
 In Boundaryfill algorithm,area-fillingisstartedata pointinside aregionandpaint
the interiortowardsthe boundary
 If the boundaryisspecifiedinasingle color,the fill algorithm proceedsoutward
pixel bypixel until the boundarycolorisencountered
 Thismethodisparticularlyuseful ininteractivepaintingpackages,where interior
pointsare easilyselected
 Usinga graphicstabletor otherinteractive device,anartistor designercansketch
a figure outline ,selectafill colororpatternfroma colormenu,andpick an
interiorpoint
 The systemthenpaintsthe figure interior
 To displayasolidcolorregion( withno border),the designercanchoose the fill
colorto be the same as the boundarycolor
 A boundaryfill procedure acceptsasinputthe coordinatesof aninteriorpoint(x,
y),a fill color,anda boundarycolor
 Startingfrom(x,y),the procedure testsneighboringpositionstodetermine
whethertheyare of the boundarycolor.If not,theyare painted
withthe fill color,andtheirneighborsare tested
 Thisprocesscontinuesuntil all pixelsup
to the boundarycolorfor the area has
beentested
 
Procedure forboundaryfill algorithm:
voidboundaryFill4(intx, inty,intfill,intboundary:integer)
{
intcurrent;
current= getpixel(x,y) ;
if (current!= boundary) and(current!= fill)
{
setColor(fill);
setpixel(x,y);
boundaryFill4 (x+1, y, fill, boundary);
boundaryFill4 (x-1, y, fill, boundary);
boundaryFill4 (x, y+1, fill, boundary);
boundaryFill4 (x, y-1, fill, boundary);
}
} // { endof procedure boundaryFill4}.
Flood-fill algorithm
 Flood-Fill Algorithmisusedtofill in(orrecolor) anareathat isnot
definedwithinasingle color boundary.
 We can paintsuch areasby replacingaspecifiedinteriorcolorinstead
of searchingfora boundarycolorvalue.
 We start from a specified interior point (x, y) and reassign all pixel
valuesthatare currentlysettoa giveninteriorcolor with the desired
fill color.
 The processwill be a recursive one andstoppedwhenthere isno
neighboringpixelswhichcanbe colored.
 If the area we wantto painthas more than one interiorcolor,we
can firstreassignpixel valuessothatall interiorpointshave the same color.
 Usingeithera 4-connectedor8-connectedapproach,we thenstepthroughpixel positionsuntil
all interiorpointshave beenrepainted.
Procedure forfloodfill algorithm:
voidfloodFill4(intx,inty,intfillcolor,int oldcolor)
{
if (getpixel(x,y) ==oldcolor)
{
setpixel(x,y,fill color);
floodFill4(x+1,y,fill color,oldcolor);
floodFill4(x-1,y,fill color,oldcolor);
floodFill4(x,y+1,fill color,oldcolor);
floodFill4(x,y-1,fill color,old color);
}
} // {endof procedurefloodFill4}

Unit 2 notes

  • 1.
    Output primitives  Apicture can be describedinseveral ways. o In a raster display,apicture iscompletelyspecifiedbythe setof intensitiesforthe pixel positionsinthe display. At the otherextreme,apicture canbe describedasa setof complex objects,suchastreesandterrain or furniture andwalls,positionedatspecifiedcoordinatelocationswithinthe scene Point Plotting · Pointplottingisattainedbyconvertingasingle coordinate positionfurnishedbyan applicationprogramintoappropriate operationsforoutputdeviceinuse. · For example, with a CRT monitor, the electron beam is turned on to illuminate the screen phosphor at the selected location. The electron beam is positioned based on the display technology. Line Drawing · The line is the most fundamental drawingprimitive withmanyuses - charts,engineering drawings,illustrations,2Dpencil-basedanimationandcurve approximation. · Line drawing is accomplished by calculating intermediate positions along directed to fill in these positionsbetween the endpoint positions. An output device is then directed to fill in these positions between the endpoints. The Cartesianslope-interceptequationforastraightline is y = m. x + b – (1) DDA Algorithm  The digital differential analyzer(DDA) isascan-conversionline algorithmbased on calculatingeitheryor x.  The line issampledatunitintervalsinone coordinate andcorrespondinginteger valuesnearestthe line pathforthe othercoordinate are determined. Algorithm: Step1 Inputtwo endpointpixel positions(x1,y1) and(x2,y2) Step2 Findhorizontal andvertical difference betweenthe endpoints dx = x2-x1 dy = y2-y1 Step3 The difference withthe greatermagnitudedeterminesthe value of parametersteps If abs (dx)>abs(dy) then steps= abs (dx) Else steps= abs (dy) Step4 Startingwithpixel position(x1,y1) be determinedoffsetneededateachstepto generate nextpixel alongthe line path. xIncrement=dx/steps
  • 2.
    yIincrement=dy/steps Step5 Assignthe valuesof x1,y1tox, y x = x1 y = y1 Step6 Plotthe pixel at(x,y) positiononscreensetpixel (round(x),round(y),1).Here ‘1’is the intensity of pixel i.e.,intensitywithwhichpicture isilluminated. step7 Calculate the valuesof x andy for the nextpixel position. x = x + xIncrement y = y + yIncrement Step8 Plotthe pixel at(x,y) position,setpixel (round(x),round(y),1) Step9 Repeatthe steps7 and 8 until the value of I i.e.,startfrom 1 tosteps. EXAMPLE: Draw A line betweenthe points (20,10) and(30,18) Step1: x1 = 20, x2 =30, y1 = 10, y2 = 18. Step2: dx = 30 -20 = 10, dy = 18 – 10 = 8. Step3: dx > dy=> 10 >8, steps=10. Step4: x increment= 10/10 = dx/steps=1. y increment = 8/10 = dy/steps = 0.8. Step5: x = 20, y = 10. Step6: pixel (20,10) . Step7: x = x + x increment. y = y + y increment.
  • 3.
    I x y(round(x),round(y)) 1 20+1=21 10+0.8=10.8 (21,11) 2 21+1=22 10.8+0.8=11.6 (22,12) 3 22+1=23 11.6+0.8=12.4 (23, 12) 4 23+1=24 12.4+0.8=13.2 (24,13) 5 24+1=25 13.2+0.8=14 (25,14) 6 25+1=26 14+0.8=14.8 (26,15) 7 26+1=27 14.8+0.8=15.6 (27,16) 8 27+1=28 15.6+0.8=16.4 (28,16) 9 28+1=29 16.4+0.8=17.2 (29,17) 10 29+1=30 17.2+0.8=18 (30,18)
  • 4.
    Advantages: 1. It isthe simplest algorithm 2. It is a is a faster method for calculating pixel positions Disadvantages of DDA Algorithm 1. Floating point arithmetic in DDA algorithm is still time-consuming 2. End point accuracy is poor Bresenham’s Line Algorithm  An accurate and efficient raster line-generating algorithm, developed by Bresenham, scans converts lines using only incrementa1 integer calculations that can be adapted to display circles and other curves. · Algorithm: · · Step1 · · Inputthe two line endpointsandstore the leftendpointsin(x0,y0) · · Step2 · · Load (x0,y0) intothe frame buffer;plotthe firstpoint. · · Step3 · · Calculate constants x, y,2y, and 2y - 2x, andobtainthe startingvalue for the decisionparameteras · · x = xn - x0 and y= yn - y0 · · p0 = 2y - x · Step4 · · At eachxk alongthe line,startingatk = 0, performthe followingtest: · · If pk < 0, the nextpointtoplotis (xk+1,yk) and · · pk+1 = pk +2y · · Otherwise,the nextpointtoplotis(xk+1,yk+1) and · · pk+1 = pk +2y -2x · · Step5
  • 5.
    · · Repeatstep4 xtimes. EXAMPLE: Draw A line betweenthe points(20,10) and(30,18) · · Thisline hasa slope of 0.8, with · · x = 10 , y = 8 · · The initial decisionparameterhasthe value · · p0 = 2y - x · · = 6 · · and the incrementsforcalculatingsuccessive decisionparametersare · · 2y = 16 · · 2y - 2x = -4 · · We plotthe initial point(xo,yo)=(20, 10), anddetermine successivepixel positions alongthe line pathfromthe decisionparameteras
  • 6.
    Advantages Algorithm is Fast Usesonly integer calculations Disadvantages It is meant only for basic line drawing. Mid-Point circle Algorithm  As inthe raster line algorithm, unitintervalsare takenandthe closestpixel positionis determinedtothe specifiedcircle pathat each step.  For a givenradiusr and screencenterposition(xc,yc),the algorithmisfirstsetupto calculate pixel positionsaroundacircle pathcenteredat the coordinate origin(0,0). Algorithm: · · Step1 · · Inputradius r and circle center(xc,yc) andobtainthe firstpointonthe circumference of a circle centeredonthe originas k Pk X, Y 0 6 (21,11) 1 2 (22,12) 2 -2 (23,12) 3 14 (24,13) 4 10 (25,14) 5 6 (26,15) 6 2 (27,16) algorithm. 7 -2 (28,16) 8 14 (29,17) 9 10 (30,18)
  • 7.
    (x0, y0) =(0, r) Step2 Calculate the initial value of the decisionparameteras p0 = 5/4 – r Step3 At eachxk positionstartingatk=0, performthe followingtest: If pk<0, the nextpointalongthe circle centeredon(0,0) is (xk+1,yk) and pk+1 = pk + 2xk+1 +1 Otherwise,the nextpointalongthe circle is(xk +1, yk - 1) and pk+1 = pk + 2xk+1 +1 – 2yk+1 where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk – 2. Step4 Determine symmetrypointsinthe othersevenoctants. Step5 Move eachcalculatedpixel position(x,y) ontothe circularpathcenteredon(xc,yc) andplotthe coordinate values: x = x + xc, y = y + yc. Step6 Repeatsteps3 through5 until x >= y. Example:  Givena circle radiusr= 10, midpointcircle algorithmisdemonstratedbydeterminingpositions alongthe circle octant inthe firstquadrantfromx=0 to x=y  The initial value of the decisionparameteris p0 = 1 – r = -9  For the circle centeredonthe coordinate origin,the initial pointis(x0,y0)=(0,10) andinitial
  • 8.
    incrementtermsforcalculatingthe decisionparametersare 2x0=0 and2y0=20  Successive decisionparametervaluesandpositionsalongthe circle pathare calculatedusing the midpointmethodas k pk (xk+1,yk+1) 2x k+1 2y k+1 0 -9 (1,10) 2 20 1 -6 (2,10) 4 20 2 -1 (3,10) 6 20 3 6 (4,9) 8 18 4 -3 (5,9) 10 18 5 8 (6,8) 12 16 Filled area primitives  Areais filledwithacertaincolor  Most oftenthe shape isthat of a polygon  Boundariesare linearastheyare polygons  Standardgraphicsobjectsare objectsmade of a setof polygonsurface patches.  There are twobasicapproachesto area fillingonrastersystems. o To fill anarea isto determine the overlapintervalsforscanlinesthatcrossthe area. o Areafillingistostart froma giveninteriorpositionandpaintoutwardfromthis pointuntil we encounter the specifiedboundaryconditions. Boundary-fill Algorithm  In Boundaryfill algorithm,area-fillingisstartedata pointinside aregionandpaint the interiortowardsthe boundary  If the boundaryisspecifiedinasingle color,the fill algorithm proceedsoutward pixel bypixel until the boundarycolorisencountered  Thismethodisparticularlyuseful ininteractivepaintingpackages,where interior pointsare easilyselected  Usinga graphicstabletor otherinteractive device,anartistor designercansketch a figure outline ,selectafill colororpatternfroma colormenu,andpick an interiorpoint  The systemthenpaintsthe figure interior  To displayasolidcolorregion( withno border),the designercanchoose the fill colorto be the same as the boundarycolor  A boundaryfill procedure acceptsasinputthe coordinatesof aninteriorpoint(x, y),a fill color,anda boundarycolor  Startingfrom(x,y),the procedure testsneighboringpositionstodetermine whethertheyare of the boundarycolor.If not,theyare painted withthe fill color,andtheirneighborsare tested
  • 9.
     Thisprocesscontinuesuntil allpixelsup to the boundarycolorfor the area has beentested   Procedure forboundaryfill algorithm: voidboundaryFill4(intx, inty,intfill,intboundary:integer) { intcurrent; current= getpixel(x,y) ; if (current!= boundary) and(current!= fill) { setColor(fill); setpixel(x,y); boundaryFill4 (x+1, y, fill, boundary); boundaryFill4 (x-1, y, fill, boundary); boundaryFill4 (x, y+1, fill, boundary); boundaryFill4 (x, y-1, fill, boundary); } } // { endof procedure boundaryFill4}. Flood-fill algorithm  Flood-Fill Algorithmisusedtofill in(orrecolor) anareathat isnot definedwithinasingle color boundary.  We can paintsuch areasby replacingaspecifiedinteriorcolorinstead of searchingfora boundarycolorvalue.
  • 10.
     We startfrom a specified interior point (x, y) and reassign all pixel valuesthatare currentlysettoa giveninteriorcolor with the desired fill color.  The processwill be a recursive one andstoppedwhenthere isno neighboringpixelswhichcanbe colored.  If the area we wantto painthas more than one interiorcolor,we can firstreassignpixel valuessothatall interiorpointshave the same color.  Usingeithera 4-connectedor8-connectedapproach,we thenstepthroughpixel positionsuntil all interiorpointshave beenrepainted. Procedure forfloodfill algorithm: voidfloodFill4(intx,inty,intfillcolor,int oldcolor) { if (getpixel(x,y) ==oldcolor) { setpixel(x,y,fill color); floodFill4(x+1,y,fill color,oldcolor); floodFill4(x-1,y,fill color,oldcolor); floodFill4(x,y+1,fill color,oldcolor); floodFill4(x,y-1,fill color,old color); }
  • 11.
    } // {endofprocedurefloodFill4}