1
Computer Graphics
Copyright 1996 C. Gotsman & G. Elber
Computer Science Dept. Technion
Scan Conversion
Page 1
Scan Conversion:
Drawing Lines
on
Raster Display
bresenham
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
2
Raster Display
Discrete grid of elements (frame buffers
pixels)
Shapes drawn by setting the “right”
elements
Frame buffer is scanned, one line at a
time, to refresh the image (as opposed
to vector display)
Properties
Difficult to draw smooth lines
Displays only a discrete approximation of
any shape
Refresh of entire frame buffer
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
3
Terminology
Pixel: Picture element
Smallest accessible element in picture
Usually rectangular or circular
Aspect Ratio: Ratio between physical
dimensions of pixel
(not necessarily 1 !!)
Dynamic Range: Ratio between minimal (not
zero!) and maximal light intensity emitted by
displayed pixel
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
4
Terminology (cont’d)
Resolution: number of distinguishable
rows and columns on device Measured in
Absolute values (1K x 1K)
Relative values (300 dots per inch)
Screen Space: discrete 2D Cartesian
coordinate system of screen pixels
Object Space: 3D Cartesian coordinate
system of the universe where the objects
(to be displayed) are embedded
1
2
6
6x8
2 81
y
z
x
drawing
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
5
Basic Line Drawing
Assume x1< x2 and line slope absolute value is 1
Questions:
Can this algorithm use integer arithmetic ?
Does it accumulate error ?
Is the error significant ?; end
; end
;
; ) )( , (
begin
do to from for
;
;
;
; , , , ,float
begin
) , , , (
21
1
12
12
2211
slopeyy
yx
xxx
yy
dxdy
slope
yydy
xxdx
slopeyxdydx
yxyx
RoundPlotPixel
Line
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
6
Recursive Line Drawing
Simple, recursive, integer, line drawing:
; end
; end
; ) , , , (
; ) , , , (
; ) , (
begin else
;return
) ) and (
or ) and ( ( if
;2/)(
; 2/)(
; ,int
begin
) , , , (
22
11
22
11
21
21
2211
yxyx
yxyx
yx
yyxx
yyxx
yyy
xxx
yx
yxyx
Line
Line
PlotPixel
Line
Questions:
How does the algorithm
work?
Does the algorithm
accumulate error ?
Is it significant ?
2
Computer Graphics
Copyright 1996 C. Gotsman & G. Elber
Computer Science Dept. Technion
Scan Conversion
Page 2
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
7
Recursive Line Drawing (cont’d)
More Potential Problems:
Line is not drawn sequentially
Function call for each pixel drawn
We want a faster and accurate algorithm !
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
8
Midpoint (Bresenham) Algorithm
Idea:
Define error function
Proceed along the line incrementally
Select direction that minimizes accumulated error
)+-(2 = ),(
0|,
cydxxdyyxd
cydxxdycbyaxyx
1and,12
121212
xx
yy
dx
dyyyxx
Assumptions:
Definitions
(x1,y1) (x,y)
d(x,y)
(x2,y2)
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
9
Midpoint (Bresenham) Algorithm (cont’d)
Given point P=(x,y), d(x,y) is the signed distance of P to (up to normalization factor)
d is zero for P d may serve as error function to be minimized
Starting point satisfies d(x1,y1) = 0
Each step moves right (east) or upper right
(northeast)
)+-(2 = ),( cydxxdyyxd
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
10
Midpoint Line Drawing (cont’d)
Sign of d(x1+1,y1+½) indicates whether to move east or
northeast
At (x1,y1)
dstart= d(x1+1,y1+½)=2dy-dx
Increment in d (after each step)
Move east
e= d(x+2,y+½) d(x+1,y+½) =
= 2((x+2)dy(y+½)dx+c))2((x+1)dy(y+½)dx+c)
= 2dy
Move northeast
ne= d(x+2,y+3/2) d(x+1,y+½) =
= 2((x+2)dy(y+3/2)dx+c))2((x+1)dy(y+½)dx+c)
= 2(dydx)
)+-(2 = ),( cydxxdyyxd
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
11
Midpoint Line Algorithm
; end
; end
; ) , (
; end
; 1
; 1
;
begin else
; end
; 1
;
begin
then) 0 ( if
do ) ( while
; ) , (
; )(2 ; 2
; 2
; ;
; ; xx
; , , , , , ,int
begin
) , , , (
2
1212
11
2211
yx
yy
xx
dd
xx
dd
d
xx
yx
dxdydy
dxdyd
yydyxxdx
yy
ddydxyx
yxyx
ne
e
nee
nee
PlotPixel
PlotPixel
Line
bresenham
fineline
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
12
Midpoint Examples
Question: Is there a problem with the MidPoint algorithm
(hint: horizontal vs. diagonal lines)?
Comment: Algorithm extends to higher order curves –
e.g. circles
Midpoint (squares) vs.
Recursive (squares) line drawingMidpoint line drawing
3
Computer Graphics
Copyright 1996 C. Gotsman & G. Elber
Computer Science Dept. Technion
Scan Conversion
Page 3
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
13
Error function d can be viewed as explicit surface:
d(x,y)=2(xdy-ydx+c)
Implicit equation of origin-centered circle C is
x2+y2R2 = 0
Error functional
d(x,y) = x2+y2R2
vanishes on C
Can also be seen as an explicit surface
Error Function Intuition
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
14
Circle Midpoint For first quadrant, start from (x1,y1) = (0,R)
Move east or south-east
d(x,y) will be a threshold criteria at the midpoint
Because of symmetry - suffices to deal with
octant of first quadrant for which y>x
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
15
Circle Midpoint (cont’d)
At the beginning: dstart=d(x1+1,y1½)=d(1,R½)=5/4R
Moving east:
e = d(x+2,y½)d(x+1,y½)
= (x+2)2+(y½)2R2(x+1)2(y½)2+R2
= 2x+3
Moving south-east:
se = d(x+2,y3/2)d(x+1,y½)
= (x+2)2+(y3/2)2R2(x+1)2(y½)2+R2
= 2(xy)+5
e and se not constant anymore
Since d is incremented by integer values, one can use dstart=1R, yielding an integer algorithm
This last change has no effect on threshold criteria
d(x,y) = x2+y2R2
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
16
Midpoint Circle AlgorithmCircleOctant2
PlotPixel
PlotPixel
(R)
begin
int
( );
while( ) do
if ( ) then
begin
end;
else begin
d d + 2(x - y)+ 5;
x x +1;
y y -1;
end;
( ) ( );
end;
x y d
x
y R
d R
x y
y x
d
d d x
x x
x y
, , ;
;
;
;
,
;
;
,
0
1
0
2 3
1
Generate other 7 octants by
mirroring (*)
Question: What will happen if we
change “while (y > x)” to
“while (y > 0)”?
Scan Conversion - Center for Graphics and Geometric Computing,
Technion
17
Circle Midpoint Examples