Scan Conversion
A. Samal
Scan Conversion
• Last step in the graphics pipeline• Efficiency is a central issue• Common primitives
– Lines– Polygons– Circles
• Hardware implementations preferable
Lines
• Compute the coordinate of the pixels that lie on a line on a 2D raster
• Defined by the coordinates of the end points• Criteria
– Thin– As close to ideal as possible– 1 pixel wide
• If -1<= slope <= 1 exactly 1 pixel in one column• Else exactly 1 pixel in one row
– Constant brightness, independent of the length & orientation
– Should be drawn fast
Scan Conversion of Lines
Basic Incremental Algorithm
bmxylineaofEquation :
01
01
xxyy
xy
m
11 mAssume
y than faster increases xSo,
), 0y(x : point Starting 0
), 11 y(x : pointEnding
Problem
• for each x plot pixel at closest y–Problems for steep lines
Basic Incremental Algorithm
)y,round (x ii )(
),y (x ii
m),y (x ii 1
)my,round (x ii )(1
Desired Line
Basic Incremental Algorithm
• Brute force algorithm• Inefficient• Each iteration
– Floating point multiply– Floating point addition– Rounding
{
),,,( 11 yxyxLine 00
;1
1
0
0
xxyy
m
){;; 1 ii0i xxxxfor(x
);( bmxRoundy ii
}
}
);,(
ii yxdrawpixel
Basic Incremental Algorithm
• Eliminate the multiplication
• Incremental computation
• Digital Differential Analyzer (DDA)
bmxy ii 11
}
}
;
));(,(
){;;
;;
{
);,,,(
1
1
1
11
myy
yRoundxdrawpixel
xxxxfor(x
yyxxyy
m
yxyxLine
ii
ii
ii0i
0i0
0
00
myythenxIf ii 11
bxxm i )(xmyi
bmxy ii
Basic Incremental Algorithm
• If the magnitude of the slope is more than 1 change the role of x and y in the algorithm– y increases faster than x
• Simple but inefficient– Still there are floating point operations
• Bresenham’s algorithm– Uses only integer arithmetic– Avoids round function– Extended to drawing circles
Midpoint Line Algorithm11 mAssume
(NE) NorthEast
(E) East
choices Two :
right upper: yx
left lower: yx
),(
),(
11
00
),(: pp yx pixelPrevious
next? be should pixel Which
E
NE
M
Previous Pixel
Current Pixel Choices
Next Pixel Choices
),( pp yxP
Q
Midpoint Line Algorithm yx pixelPrevious pp ),(:
• Compute the distance between– Q and E– Q and NE
• See which side of the line M lies• M lies below the line
– Choose NE• M lies above the line
– Choose E• M lies on the line
– Choose either
1 pxx with line the of onintersecti of Point : Q
E
NE
M
Previous Pixel
Current Pixel Choices
Next Pixel Choices
),( pp yxP
Q
Midpoint Line Algorithmlies? point a line the of side which know we do How
).();();( 010101 xxBcxxdxbyydya
line the of tionRepresenta Implicit
0cbyaxy)F(x,
line the of tionRepresenta Explicit
Bxdxdy
Bmxy
0... dxBydxxdy
Midpoint Line Algorithmlies? point a line the of side which know we do How
)2
1,1() pp xxFF(M of sign the Check
line the below points forline the above points for
line the on points foryxF
000
),(
line the below or above lies Q if test To
Midpoint Line Algorithm
d variable decison a Define
NEorE Choosed 0
)2
1,1 pp yF(xd
E ChooseQ)Md
above is (0
NE ChooseQ) below is (M d
0
E
NE
M
Previous Pixel
Current Pixel Choices
Next Pixel Choices
),( pp yxP
Q
Midpoint Line Algorithm
value the in change the on based Add
directly F(M) of value the compute not Do
• What happens at the next grid point?– Depends on whether we choose N or NE
• If E is chosencybxayxFd ppppnew )
2
1()2()
2
1,2(
cybxad ppold )2
1()1(
add oldnew dyadd oldnewE
E) choose we after d in incrementE ( E
NEM
Previous
Pixel
Current Pixel
Choices
Next Pixel Choices
),( pp yxP
Q
Midpoint Line Algorithm
• If NE is chosen
cybxayxFd ppppnew )2
3()2()
2
3,2(
NE) choose we after d in incrementNE (
cybxad ppold )2
1()1(
badd oldnew dxdybadd oldnewNE
value the in change the on based Add
directly F(M) of value the compute not Do
E
NEM
Previous
Pixel
Current Pixel
Choices
Next Pixel Choices
),( pp yxP
Q
Midpoint Line Algorithm
• Summary – Choose between the two pixels (E/NE) based on
the sign of the decision variable d=F(M)– Update the decision variable by adding the change
along that direction– Repeat this process until the end point is reached
Midpoint Line Algorithm
2
dxdy
d variable decision the for value Initial
)2
1,1 00start yF(xF(M)d
cybxa 00 )2
1()1(
2
bacbyax 00
line the on lies),y(xsinceb
a 002
Midpoint Line Algorithm
• Fractional arithmetic still problematic• Should be avoided if possible• Redefine F(x,y) by multiplying by 2• Thus F(x,y) = 2(ax+by+c)• Replace all constants and the decision variable
by 2• Removes all multiplications; only additions left
Midpoint Line Algorithm
c)by(axF(x,y) 2
dxdybadstart 22
)(*2 dxdyNE
dyE .2
Midpoint Line AlgorithmMidpointLine(x0,y0,x1,y1,color){ int dx,dy,dE,dNE,d,x,y; dx=x1-x0; dy=y1-y0; d=2*dy-dx; dE=2*dy; dNE=2*(dy-dx); x=x0;y=y0; DrawPixel(x,y,color); for(x=x0;x<x1; x++) { if (d<=0) d+=dE ; else {d+=dNE; y++;} DrawPixel(x,y,color);}
Bresenham’s AlgorithmAn example
4 5 6 7 8 9
7
8
9
10
11
12(x0 , y0 ) (5,8)
(x4 , y4 ) (9,11)
x 4
y 3
incrE 2y 6
incrNE 2(y x) 2
d0 2y x 2
first choice is NE
Bresenham’s AlgorithmAn example
4 5 6 7 8 9
7
8
9
10
11
12
d1 d0 incrNE 2 2 0
second choice is E
Bresenham’s AlgorithmAn example
4 5 6 7 8 9
7
8
9
10
11
12
d2 d1 incrE 0 6 6
third choice is NE