Professional Documents
Culture Documents
Computer Graphics
Lecture 5
3.3.3 Bresenham Algorithm
Bresenham developed an accurate and efficient raster
line-generating algorithm that also can be adapted to
display circle and other curves.
Idea
Algorithm Description
Example
Principle
10 11 12 13 14 15 16
xk xk+1 x
Sampling at unit x intervals.
(1) Assuming 0<m<1.
(2) Current determined pixel (xk, yk). Next pixel (xk+1, yk+1)
(3) Two choices: (xk+1, yk) and (xk+1, yk+1)
where xk+1 = xk+1
(4) At sampling position xk+1, the vertical pixel separations from the ideal line
are labeled as d1 and d2
Algorithm Description
d1 = y - yk = m*xk+1+b-yk = m*(xk+1)+b- yk
d2 = yk+1 - y = yk+1-m*(xk+1)-b
d1 - d2 = 2m(xk+1)-2yk+2b-1
substitute m= y / x
Pk is decision parameter for the kth step
Algorithm Description
• definition of Pk
Pk = x (d1-d2) = 2y*xk -2x*yk+C
where C = 2y + x*(2b-1)
The sign of Pk is the same as the sign of d1-d2
Pk+1 = 2y*xk+1-2x*yk+1+C
Pk = 2y*xk -2x*yk+C
∴ recursion equation of P k
if (xa>xb) {
x = xb;
y = yb;
xEnd = xa;
}
else {
Program Implementation
x = xa;
y = ya;
xEnd = xb;
}
setpixel (x,y);
Program Implementation
while (x<xEnd) {
x++;
if (p<0) p += twoDy;
else {
y++;
p += twoDyDx;
}
setpixel (x,y);
}
}
Example
• Assume two endpoints positions are
P1(20,10) , P2(30,18). Calculate the intermediate pixel
positions by using Bresenham algorithm
x = 10, y = 8;
2y = 16, 2y -2x = -4;
P0 = 2y - x =6;
Example
step k Pk (x k+1, yk+1)
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)
7 -2 (28, 16)
8 14 (29, 17)
9 10 (30, 18)
Example
18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30
Exercise 2
• Assume two endpoints positions are
P1(10,30) , P2(20,36). Calculate the intermediate pixel
positions by using Bresenham algorithm
it involves
We could considerable
use this equation to computation.
calculate the position
Moreover, the spacing between plotted pixel
of points on a circle by stepping along the x axis in
positions is not uniform
unit steps from xc-r to xc+r
y y c r 2 ( xc x) 2
3.4.1 Properties of Circles
• We also can calculate points along the circular
boundary using polar coordinates r and θ
It is time-consuming because of the
x = xc + r cosθ
trigonometric functions
y = yc + r sinθ
Symmetry of a circle
y
(-y,x) (y,x)
(-x,y) (x,y)
(-x,-y) (x,-y) x
(-y,-x) (y,-x)
3.4.2 Bresenham Circle Generating
• Principle xk xk+1
• Algorithm Description
• Example yk
yk-1
yk-2
Principle
yk
d1
Sampling at unit x intervals. y
(1) Current displayed pixel (xk, yk).
y -1 d2
Next pixel (xk+1, yk+1) k
(2) Two choices: (xk+1, yk) and (xk+1, yk-1)
where xk+1 = xk+1
(3) At sampling position xk+1, the
vertical pixel separations from the
ideal circle path are labeled as d1 and d2
xk xk+1 x
Algorithm Description
• d1=yk2-y2= yk2-r2+(xk+1) 2
d2= y2-(yk-1)2= r2-(xk+1)2-(yk-1) 2
• pk+1 = pk+4xk+2(yk+12-yk2)-2(yk+1-yk)+6
pk+4xk+6 pk<0(yk+1=yk)
pk+1 =
pk+4(xk-yk)+10 pk≥0(yk+1=yk-1)
• p0 = 3 - 2r (x0,y0)=(0,r)
The Steps of Algorithm
1) Input radius r and center of circle (xc, yc) ,
(x0, y0) = (0, r)
2) Calculate P0 = 3 - 2r;
3) Calculate Pk+1, and determine next point ;
if pk <0, (xk+1, yk)
if pk ≥0, (xk+1, yk-1)
4) Return to step 3 until x ≥ y
Example
• Assume two points coordinated in circle path are (0,
10) and (10, 0). Calculate the intermediate pixel
positions by using Bresenham algorithm
pk+4xk+6 pk<0(yk+1=yk)
pk+1 =
pk+4(xk-yk)+10 pk≥0(yk+1=yk-1)
pk+4xk+6 pk<0(yk+1=yk)
pk+1 =
r=10 pk+4(xk-yk)+10 pk≥0(yk+1=yk-1)
k Pk (xk,yk)
P0 = 3 - 2r
0 -17 (0,10)
P0= 3-20=-17 1 -11 (1,10)
P1= -17+4*0+6=-11 2 -1 (2,10)
P2= -11+4*1+6=-1 3 13 (3,10)
P3= -1+4*2+6=13
4 -5 (4,9)
P4= 13+4*(3-10)+10=-5
5 17 (5,9)
P5= -5+4*4+6=17
6 11 (6,8)
P = 17+4*(5-9)+10=11 (7,7)
6
Example r=10
y Y=x
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 x
3.4.3 Midpoint Circle Algorithm
• IDEA
• Formula Derivation(P.173)
• Algorithm Description
• Program Implementation(P.174)
• Example (P.84)
Xi Xi+1
IDEA Yi
• p0 = 5/4 - r ≈ 1- r
Algorithm Description
Direct storage
scheme
3.5 Color Lookup Table
Frame buffer
values are now
used as indices into
the color table.
Each pixel can
reference any one
of the 256 table
positions, and each
entry in the table
uses 24 bits to
specify an RGB
color
Direct Storage Scheme
• Advantages
• Extremely flexible - we may simultaneously use any colors
required from the range available
• Simple hardware (in concept only!)
• Disadvantages
• The large amount of memory required for the frame buffer
• Slower video operations. Changing a single pixel requires
three bytes to be altered.