Академический Документы
Профессиональный Документы
Культура Документы
DEPARTMENTOFCIVILENGINEERING
CE201:ComputerAidedGraphics
Week5,6,7
RajatRastogi
rajatfce@iitr.ernet.in
Previous Lecture
PreviousLecture
ImageFileFormats
Image File Formats
RasterFormats
VectorFormats
Vector Formats
FormatStructure
ColourSetting
C l
S tti
Lecture Outline
LectureOutline
ScanConversion
Scan Conversion
Point
Line
Circle
Point Scan
PointScan
Point
Pointplottingisfurnishedbyconvertingasingle
plotting is furnished by converting a single
coordinatepositionintoappropriateoperationsfor
theoutputdeviceinuse.
Randomscanusesstoredpointplottinginstructions
(coordinates)andconvertthemindeflectionvoltage
thatpositionstheelectronbeamatthescreen
locationstobeplottedduringeachrefreshcycle.
Point Scan1
PointScan1
Raster
Rasterscandependsonsettingthebitvalue
scan depends on setting the bit value
correspondingthespecifiedscreenpositionwithin
theframebufferto1.Theelectronbeamwhen
sweepsacrosssuchpositioninalineemitsburstof
electronswithintensitiescorrespondingtocolour
codestobedisplayedatthatlocation.
d t b di l d t th t l ti
Point Scan2
PointScan2
Let
Letasscan
as scanconvert
convertapoint(x,y)toapixellocation
a point (x, y) to a pixel location
(x,y)
x=Floor(x),andy=Floor(y)
WhereFloorisafunctionthatreturnslargestintegerthat
islessthanorequaltotheargument.
Insuchacase,theoriginisplacedatthelowestleft
cornerofthepixelgridintheimagespace
Allpointsthatsatisfyx
All i
h
i f x<x+1andy
1 d y<y+1
1
willbemappedtopixellocation(x,y)(referFiga)
Point Scan3
PointScan3
Point Scan4
PointScan4
If
Iftheoriginofcoordinatesystemfor(x,y)isto
the origin of coordinate system for (x y) is to
beshiftedtonewlocationsay(x+0.5,y+0.5),
then
x=Floor(x+0.5),andy=Floor(y+0.5)
Thiswillplacetheoriginofthecoordinatesystem
This will place the origin of the coordinate system
(x,y)atthecentreofpixel(0,0)
Now,allpointsthatsatisfyx
Now all points that satisfy x0
0.5
5 x<x
x < x+0
+0.5and
5 and
y0.5 y<y+0.5willbemappedtopixellocation
( ,y)(
(x,y)(referFigb)
g )
Line ScanConversion
LineScan
Conversion
Lineplottingisdonebycalculatingintermediate
e p o g s do e by ca cu a g e ed a e
positionsbetweenthetwospecifiedendpointpositions.
Linearlyvaryinghorizontalandverticaldeflection
voltagesaregeneratedproportiontotherequired
changesinx,ydirectionstoproducethesmoothline.
Astraightlinesegmentisdisplayedbyplottingthe
A t i ht li
t i di l d b l tti th
discretepointsbetweenendpointpositions.
These
Theseintermediatepositionsarecalculatedfromtheequation
intermediate positions are calculated from the equation
oftheline
Colourintensityisloadedintotheframebufferatthe
corresponding pixel coordinate
correspondingpixelcoordinate
Line ScanConversion1
LineScan
Conversion1
Sc
Screenlocationsarereferencedwithintegervalues.E.g.
ee oca o s a e e e e ced
ege a ues g
(10.48,20.51)willbeconvertedtopixelposition(10,20)
Thisroundingeffectcauseslinestobedisplayedwitha
stairstepappearance.Thisisnoticeableonlow
resolutionsystems.
Bettersmoothingcanbeachievedbyadjustinglight
B tt
thi
b
hi d b dj ti li ht
intensitiesalongthelinepaths.
Line ScanConversion2
LineScan
Conversion2
Scan Lin
ne No
Pixelcolumnnumberisbasicallypixelpositionacrossa
e co u
u be s bas ca y p e pos o ac oss a
scanline.Theseareincreasingfromlefttoright.
Scanlinenumberareincreasingfrombottomtotop.
Loadingaspecifiedcolour intoframe
bufferatapixelpositiondefinedby
columnxalongscanliney
l
l
li
setPixel (x,y)
Currentframebufferintensityfora
Current frame buffer intensity for a
specifiedlocationcanbeaccomplished
ggetPixel ((x,y)
, y)
2
1
0
0 12
Pixel
column No
Line ScanConversion3
LineScan
Conversion3
LineisdefinedbytwoendpointsP
y
p
q
1,,P2 andtheequationof
lineasshownintheFigure
Thesecanbeplottedorscanconvertedusingdifferentline
plottingalgorithms.
l tti
l ith
Theslopeinterceptequationis
y
P2(x2, y2)
notsuitableforverticallines.
Horizontal,verticalanddiagonal
y = m.x + b
(|m|=1)linescanbehandled
as special cases and can be
asspecialcasesandcanbe
P1 (x
( 1, y 1)
mappedtoimagespace
b
straightforward.
x
TheCartesianslope
interceptequationfora
straightline:
y=m.x+b
(1)
mrepresentsslopeof
l
f
thelineandbis
p
y
interceptonyaxis,such
that
m=
y 2 y1
x2 x1
b = y1 m . x1
y1
(2)
(3)
x1 x2
y2
y1
x1
x2
Usingvalueofmcomputeb
Placex=0to6andcomputecorrespondingvalueofy
Plot
Forslopegreaterthan1,increasingyby1
xk+1 =xk +1/m,wherek=1,2,.,endpoint (7)
(8)
(9)
Ifmovingfromrighttoleft:
If
Ifm<1thensetx=1andobtainy
m < 1 then set x = 1 and obtain y
valuesfromequation(6)
Ifm>1thensety=+1andobtainx
values from equation (9)
valuesfromequation(9)
BresenhamssLineAlgorithm
Bresenham
Line Algorithm
Thisrasterlinegeneratingalgorithmismoreaccurate
s as e
e ge e a g a go
s o e accu a e
andefficient
Itusesonlyincrementalintegercalculations
Itcanbeadaptedtodisplaycirclesandothercurves.
Themethodisbasedonidentifyingthenearestpixel
positionclosertothelinepathateachsamplestep.
Fig-1
13
53
12
52
11
51
10
50
10
11
12
13
Fig-2
50
51
52
53
BresenhamssLineAlgorithm1
Bresenham
Line Algorithm1
InFig1thelinesegment
g
e e seg e
startsfromscanline11
andpixelposition
( l
(column)10.Thenext
) 10 Th
pointalongthelinemay
be at pixel position (11,
beatpixelposition(11,
11)or(11,12)
Similarly,inFig2theline
segmentstartsfrom(50,
52)andthenextpixel
position may be (51 52)
positionmaybe(51,52)
or(51,51)
13
12
11
Fig-1
10
10
11
12
13
53
52
51
50
Fig-2
Fig
2
50
51
52
53
BresenhamssLineAlgorithm2
Bresenham
Line Algorithm2
Bresenhams
Bresenham slinealgorithmteststhesignofan
line algorithm tests the sign of an
integerparameterandtheonewhosevalueis
proportionaltothedifferencebetweenthe
separationsofthetwopixelpositionsfromtheactual
linepathisselected.
y
Letthelinestartsfromleftend
point(xo,yo)ofagivenline.Now, y
the pixel whose scan line y value y
thepixelwhosescanlineyvalue
isclosesttothelinepathisplotted. y
k+3
k+2
k+1
xk
Fig-3
BresenhamssLineAlgorithm3
Bresenham
Line Algorithm3
Let
Letthepixeldisplayedis(x
the pixel displayed is (xk,y
, yk).Thenextpixelto
). The next pixel to
plotwillbeincolumnxk+1 andmayberepresentedas
(xk+1,yk)or(xk+1,yk+1)
y
y = m.x + b
Thedifferencebetweenestimated y
valueofyandnewyvaluealongthey
d
d
lineistobecomputed.
y
k 3
k+3
k+2
kk+1
1
2
1
Forfirstpoint
yest =m.(xk+1)+b(10) x
Difference
iff
d1 =yest yk
(11)
Forsecondpoint d2 =(yk+1) yest
(12)
Difference between the two separations = d1 d2
Differencebetweenthetwoseparations=d
k
Fig-3
BresenhamssLineAlgorithm4
Bresenham
Line Algorithm4
Difference
Difference(d
(d1 d2)
) =2m.(x
2m.(xk+1)
+1) 2y
2 yk +2b
+ 2b 1 (13)
Letm=y/xdenotingverticalandhorizontal
separations.Letp
p
px isdefinedas
y
k 3
k+3
px = x(d1 d2)
px=2 y.xk 2x.yk +c
Where,c=2 y+x(2b 1)
y = m.x + b
yk+2
(14)
(15)
ykk+11
d2
d1
yk
Decisionparameterpx isvei.e.d1<d2
Pixelatyk isclosertolinethanthatatyk+1
Lowerpointwillbeplotted
ViceVersa
xk
Fig-3
BresenhamssLineAlgorithm5
Bresenham
Line Algorithm5
The
Thechangeincoordinatealongthelinetakesplace
change in coordinate along the line takes place
inunitsteps.Thedecisionparameteratnextstepwill
be:
pk+1 =2y.xk+1 2x.yk+1 +c
Andpk+1 pk =2y(xk+1 xk) 2x(yk+1 yk)
(16)
Butxk+1 =xk+1,sothat
pk+1 =pk +2y 2x(yk+1 yk)
(17)
( k+1 yk)iseither0or1dependinguponthesignofp
(y
) is either 0 or 1 depending pon the sign of pk.
Theinitialvalueofparameterwillbe
po =2y
= 2 y x
(18)
BresenhamssLineAlgorithm6
Bresenham
Line Algorithm6
Algorithmfor|m|<1:(steps)
Algorithm for |m|<1: (steps)
Inputandstore(xo,yo).Plotit(loadonbuffer).
Calculatex,y,2y 2x
Computedecisionparameter,po (=2y x)
Assumingpk<0,thenextpointwillbe(xk+1,yk)and
pk+1 =pk +2y
Otherwise,plotpoint(xk +1,yk +1)and
pk+1 =pk +2y
2 2x
2
Repeatstepxtimes
BresenhamssLineAlgorithm7
Bresenham
Line Algorithm7
E.g.
E.g.Plotalinewithendpoints(20,10)and(30,18).
Plot a line with end points (20, 10) and (30, 18).
Theslopeoflineis0.8.
Calculatex(=10),y(=8)
Computedecisionparameter,po (=2y x=6)
Compute2y(=16),2y 2x(=4)
Assumingpk<0,thenextpointwillbe(xk+1,yk)and
pk+1 =pk +2y
Oth
Otherwise,plotpoint(x
i
l t i t ( k +1,y
1 k +1)and
1) d
pk+1 =pk +2y 2x
Repeatstepxtimes
Repeat step x times
BresenhamssLineAlgorithm8
Bresenham
Line Algorithm8
Itisgeneralizedtolineswitharbitraryslopes.
s ge e a ed o es
a b a y s opes
Considerssymmetrybetweenoctantsandquadrantsof
thexyplane.
Algorithmallowsmovingalongpixelpositionfromeither
endoftheline.
Forpositiveslopeandstartfromleftendpoint,boththe
incrementsarepositive.Itisviceversaforstartfrom
right end point
rightendpoint.
Fornegativeslope,oneincrementwillbepositiveand
othernegativedependinguponthestartpoint.
g
p
g p
p
BresenhamssLineAlgorithm9
Bresenham
Line Algorithm9
Whenthetwoverticalseparationsfromthelinepathare
e
e o e ca sepa a o s o
e e pa a e
equal(d1 =d2),wechooseupper(orlower)ofthetwo
candidatepixels.
Specialcaseslikey=0(horizontalline);x=0(vertical
line)and|x|=|y|(diagonalline)canbeloaded
directly into the frame buffer without processing them
directlyintotheframebufferwithoutprocessingthem
throughthelineplottingalgorithm.
(23)
r
yc
xc
Thi
Thisbecomesthedecisionparameterformidpoint
b
h d ii
f
id i
algorithm
p k = f circle x k + 1, y k
2
= (x k + 1 )
+yk
2
yk-1
Midpoint
r2
(27)
xk
x k + 1 xk + 2
2yk+1 = 2yk 2
p 0 = f cricle 1 , r
=1+ r
5
p0 =
r
4
( )
(29)
If the radius r is specified as an integer, we can simply
or
round to p0 = 1 r ((for r an integer).
g )
Steps
Steps
Input radius r, circle center (xc, yc), and obtain the first point
on the circumference of a circle centered at the origin as (x0,
y0) = (0, r)
Calculate the initial value of the decision parameter as
p0 = 5/4 r
At each xk position, starting at k = 0, perform the following
test: if pk < 0,
0 the next point along the circle centered on (0,0)
(0 0)
is (xk+1 , yk) and pk+1 = pk + 2xk+1 + 1
Otherwise,thenextpointalongthecircleis(xk+1,yk1)and
pk+1 =pk +2x
2 k+1 +1
1 2y
2 k+1
Where2xk+1 =2xk +2and2yk+1 =2yk 2
Determinesymmetrypointsinthesevenotheroctants
Determine symmetry points in the seven other octants
Movepixelposition(calculated)toscreenpositionbyadding
Example
Plot
Plotthegeneratedpixelpositionsforacirclewith
the generated pixel positions for a circle with
radiusr=10usingmidpointcirclealgorithm.
po =1 r=9
Initialpoint(0,10)
Decisionparameters
2xo =0;2yo =20
y=x
y
10
9
8
7
6
5
4
3
2
1
0
x
0
10
BRESENHAMSSALGORITHM
BRESENHAM
ALGORITHM
Assume
ssu e that
a ((xi, yi) aaree thee coo
coordinates
d a es o
of thee last
as sca
scan
converted pixel upon entering step i.
Let the distance from the origin to pixel T squared minus
the distance to the true circle squared = D(t).
Then let the distance from the origin to pixel S squared
minus
i
th distance
the
di t
t the
to
th true
t
circle
i l squared
d = D(s).
D( )
As the coordinates of T are (xi + 1, yi) and those of S are
(xi + 1,
1 yi 1),
1) the following expression can be developed:
BRESENHAMSSALGORITHM1
BRESENHAM
ALGORITHM1
y
(xi, yi)
(xi + 1, yi)
(xi, + 1, yi - 1)
BRESENHAMSSALGORITHM2
BRESENHAM
ALGORITHM2
D(t)
( ) = ((xi + 1))2 + yi2 r2
D(s) = (xi+1)2 + (yi 1)2 r2
This function D provides a relative measurement of the
distance from the center of a pixel to the true circle.
Since D(t)
( ) will always
y be p
positive ((T is outside the true
circle) and D(s) will always be negative (S is inside the
true circle), a decision variable, di may be defined as
follows:
di = D(t) + D(s)
Therefore
di = 2(xi + 1)2 + yi2 + (yi 1)2 2r2
BRESENHAMSSALGORITHM3
BRESENHAM
ALGORITHM3
When
e di<0,
0, ||D(t)|<|D(s)|,
( )| | (s)|, then
e se
select
ec p
pixel
e T.
When di 0, |D(t) |D(s)|, then select pixel S.
The decision variable di+1 for the next step:
p
di+1 = (2xi+1+1)2 + y2i+ 1 + (yi+11)2 2 r2
Hence
di+1 di = 2(xi+1 + 1)2 + y2i+1 + (yi+1 1)2
2(xi + 1)2 y2i (yi 1)2
Since xi+1 = xi + 1
so,
di+1 = di + 4x
4 i + 2(y
2( 2i+1 y2i) 2(y
2( i+1 yi) + 6
BRESENHAMSSALGORITHM4
BRESENHAM
ALGORITHM4
If T is the chosen p
pixel ((di < 0)) then yi+1=yyi and so
di+1 = di + 4xi + 6
If S is the chosen pixel (di 0) then yi+1 = yi1 and so
di+1 = di + 4(xiyi) + 10
Set (0, r) to be the starting pixel coordinates and
compute the base case value di :
Example
Givenaradiusof10units.
e a ad us o 0 u s
Computedi =2(xi+1)2 +yi2+(yi1)22r2.
Setthevalueas(0,r)asthestartpoint
( ,)
p
d0 =2(0+1)2 +102 +101)2 2x102
=2+100+81200
=183200
=17.
Example1
Asd
As d0 <0
< 0,
, yi+1=yyi1.
i1
Sonextpoint(1,10)
d1 =d
d0 +4x
4xii +6.
6.
= 17+4+6
=7
Sonextpointis(2,10).
d2 =d1 +4x2+6
=7+8+6.
=+7.
7.
Example2
So
Sonextpoint(3,9).
e po (3, 9)
d3 =di+4(xiyi)+10
=7+4(39)+10
( )
=724+10
=7.
Sonextpoint(4,9)
d4=7+16+6=15.
Sonextpoint(5,8).
d5 =15+4(58)+10.
Example3
So
Sonextpoint(6,8).
e po (6,8)
d6 =17+4x6+6
=+13.
Sonextpointis(7,7)
Comparison
Iteration
Mid point
Bresenham
(0,10)
(0,10)
(1 10)
(1,10)
(1 10)
(1,10)
(2,10)
(2,10)
(3 10)
(3,10)
(3 9)
(3,9)
(4,9)
(4,9)
(5,9)
(5,8)
(6,8)
(6,8)
(7,7)
(7,7)
Readings
HearnandBaker,104
Hearn and Baker 104 122
XiangandPlastock,