You are on page 1of 43

WUCS307

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

13 Sampling at unit x intervals, the


position of start endpoint is(10,10).
12 We need to determine at the next
11 sample position whether to plot the
pixel at position(11,10) or (11,11)
10

10 11 12 13 14 15 16

The core idea of Bresenham algorithm is applying


integer calculations to get pixel position that is closest
to the mathematical (ideal) line.
Algorithm Description
y
y=mx+b
yk+1
d2
y
d1
yk

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) = 2y*xk -2x*yk+C
where C = 2y + x*(2b-1)
The sign of Pk is the same as the sign of d1-d2

if Pk < 0, the next pixel position is (xk+1, yk)


if Pk ≥0, the next pixel position is (xk+1, yk+1)
Algorithm Description
• At step k+1, Pk+1 is computed as

Pk+1 = 2y*xk+1-2x*yk+1+C

Pk = 2y*xk -2x*yk+C

∴ Pk+1 - Pk = 2y - 2x(yk+1 - yk);


Algorithm Description
Pk+1 - Pk = 2y - 2x(yk+1 - yk);

∴ recursion equation of P k

Pk+2 y (Pk < 0)


Pk+1=
P0 = 2y - x Pk+2 y - 2 x (Pk ≥ 0)
The Steps of Algorithm
1) Input two endpoints (x0,y0), (xn,yn)
2) Plotting (x0,y0) ;
3) Calculating x, y, 2 y, 2y - 2x and P0 =2y -
x
4) From k=0, calculating Pk+1 and determining the
next pixel position (xk+1,yk+1)
Pk+2 y (Pk < 0) (xk+1, yk )
Pk+1=
Pk+2 y - 2 x (Pk ≥ 0) (xk+1,
yk+1 )
5) Return to step 4
Program Implementation

Void lineBres(int xa, int ya, int xb, int yb)


{
int dx = abs(xa-xb), dy=abs(ya-yb);
int p = 2*dy-dx;
int twoDy = 2* dy, twoDyDx = 2* (dy-dx);
int x,y,xEnd;
Program Implementation

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;
2y = 16, 2y -2x = -4;
P0 = 2y - 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

x = 20-10 = 10, y = 36-30 = 6;


2y = 12, 2y -2x = -8;
P0 = 2y - x =2;
Pk+2 y (Pk < 0) (xk+1, yk )
Pk+1=
Pk+2 y - 2 x (Pk ≥ 0) (xk+1,
y +1 )
Exercise 2
Step i Pi (Xi+1, Yi+1)
0 2 ( 11 , 31 )
1 -6 ( 12 , 31 )
2 6 ( 13 , 32 )
3 -2 ( 14 , 32 )
4 10 ( 15 , 33 )
5 2 ( 16 , 34 )
6 -6 ( 17 , 34 )
7 6 ( 18 , 35 )
8 -2 ( 19 , 35 )
9 10 ( 20 , 36 )
• Bresenham’s algorithm is generalized to lines with arbitrary
slope by considering the symmetry between the various octants
and quadrants of the xy plane.

• For a line with positive slope greater than 1, we interchange the


roles of the x and y directions. That is, we step along the y
direction in unit steps and calculate successive x values nearest
the line path.
3.4 Circle-generating Algorithm
• Properties of Circles
• Bresenham circle algorithm
• Midpoint circle algorithm
3.4.1 Properties of Circles
• A circle is defined as the set of points that are all at a
given distance r from a center position (xc, yc), which
can be expressed as
(x - x )2
+ (y - y )2
= r2
One problem with this approach is that
c c

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 = d1 - d2 =2 (xk+1)2+ yk2 + (yk-1)2-2r2


if pk <0, (xk+1, yk)
else (xk+1, yk-1)
Algorithm Description
• pk+1 = 2((xk+1)+1) 2+(yk+1-1) 2+yk+12-2r2

• 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

• Test the halfway position between two Yi-1


pixels to determine if this midpoint is Yi-2
inside or outside the circle boundary

• The midpoint approach is applied Midpoint


more easily to other conics

• For an integer circle radius, the result


is same as Bresenham circle algorithm
Formula Derivation

• Define a circle function f(x,y)=x2+y2-r2


<0 if (x,y) is inside the circle border
f(x,y) =0 if (x,y) is on the circle border
>0 if (x,y) is outside the circle B
pi = f(xi+1, yi-0.5) = (xi+1)2+(yi-0.5)2-r2
• if pi <0, select (xi+1, yi)
• Otherwise select (xi+1, yi-1)
Formula Derivation

• pi+1 = f(xi+1+1, yi+1-0.5) = (xi+1+1)2+(yi+1-0.5)2-r2


• pi+1 = pi+2(xi+1)+(yi+12-yi2)-(yi+1-yi)+1
pi+2xi+1+1 pi<0
• pi+1 =
pi+2(xi+1-yi+1)+1 pi>=0

• p0 = 5/4 - r ≈ 1- r
Algorithm Description

1)Input radius r and circle center(xc,yc) , then first


point (x0,y0)=(0,r);
2)Calculate initial value of decision parameter P0 = 1-r;
3) At each step, calculate Pi+1 , select next point ;
4)Determine symmetry points in the other seven
octants ;
5)Repeat step 3, until x>=y.
Example r=10
P0=1-10=-9 i Pi (x i ,y i )
P1=-9+2*1+1=-6 0 -9 (0,10)
P2=-6+2*2+1=-1 1 -6 (1,10)
P3=-1+2*3+1=6 2 -1 (2,10)
3 6 (3,10)
P4=6+2*(4-9)+1=-3
4 -3 (4, 9)
P5=-3+2*5+1=8
5 8 (5,9)
P6=8+2*(6-8)+1=5 6 5 (6,8)
(7,7)
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10
3.5 Color Lookup Table
• void glutInitDisplayMode (GLUT_RGB|
GLUT_INDEX)
• Color information can be stored in the frame buffer
in two ways:
(1) We can store color codes directly in the frame
buffer (direct storage scheme)
(2) We can also put the color codes in a separate
table and use pixel value as an index into this table
3.5 Color Lookup Table
• void glutInitDisplayMode (GLUT_RGB|
GLUT_INDEX)
• Color information can be stored in the frame buffer
in two ways:
(1) We can store color codes directly in the frame
buffer (direct storage scheme)
(2) We can also put the color codes in a separate
table and use pixel value as an index into this table
3.5 Color Lookup Table

 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.

You might also like