COMPUTER GRAPHICS
Bressenham's Algorithm
By: Ms. Rachana Marathe
Bressenham's algorithm: line drawing
dx = x2 - x1, dy = y2-y1
P0 =2dy - dx
if Pk<0 then (xk+1,yk) and
pk+1= pk+2dy
else (pk>0) then (xk+1,yk+1)
and
pk+1= pk+2dy-2dx
Example 1: x1,y1= 1,1 x2,y2= 5,3
Algorithm:
dx = x2 - x1, dy = y2-y1
P0 =2dy - dx
if Pk<0 then (xk+1,yk) and
pk+1= pk+2dy
else (pk>0) then (xk+1,yk+1) and
pk+1= pk+2dy-2dx
Solution:
dx= 5-1 =4
dy= 3-1 =2
P0= 2dy – dx = 2 (2) – 4 = 0
Plot 1st point (1, 1) here as | dx | > | dy |
(pk>0) then (xk+1,yk+1) and (else condition)
pk+1= pk+2dy-2dx
hence plot 2,2 and
pk+1= -4
if Pk<0 then (xk+1,yk) and (if condition)
pk+1= pk+2dy
hence plot 3,2 and
pk+1= 0
(pk>0) then (xk+1,yk+1) and (else condition)
pk+1= pk+2dy-2dx
hence plot 4,3 and
pk+1=-4
Therefore the pixels are
(1,1) (2,2) (3,2) (4,3) (5,3)
Example 2: x1, y1= 2,3 x2, y2= 7, 7
Algorithm:
dx = x2 - x1, dy = y2-y1
P0 =2dy - dx
if Pk<0 then (xk+1,yk) and
pk+1= pk+2dy
else (pk>0) then (xk+1,yk+1) and
pk+1= pk+2dy-2dx
Solution:
dx= 7-2=5
dy= 7-3=4
P0= 2dy – dx = 2 (4) – 5= 3
Plot 1st point (2,3) here as | dx | > | dy |
(Pk>0) then (xk+1,yk+1) and (else condition)
pk+1= pk+2dy-2dx
hence plot 3,4 and
pk+1= 1
(Pk>0) then (xk+1,yk+1) and (else condition)
pk+1= pk+2dy-2dx
hence plot 4,5 and
pk+1= -1
Pk<0 then (xk+1,yk) and (if condition)
pk+1= pk+2dy
hence plot 5,5
pk+1= 7
Therefore the pixels are
(2,3) (3,4) (4,5) (5,5) (6,6) (7,7)
Reference
Hearn,Baker - Computer Graphics - C Version 2nd Ed
http://edu.uokufa.edu.iq/staff/dr.nidhal/compressed%20comp.book/H
earn,Baker%20-%20Computer%20Graphics%20-
%20C%20Version%202nd%20Ed.pdf

Comuter graphics bresenhams line drawing algorithm

  • 1.
  • 2.
    Bressenham's algorithm: linedrawing dx = x2 - x1, dy = y2-y1 P0 =2dy - dx if Pk<0 then (xk+1,yk) and pk+1= pk+2dy else (pk>0) then (xk+1,yk+1) and pk+1= pk+2dy-2dx
  • 3.
    Example 1: x1,y1=1,1 x2,y2= 5,3 Algorithm: dx = x2 - x1, dy = y2-y1 P0 =2dy - dx if Pk<0 then (xk+1,yk) and pk+1= pk+2dy else (pk>0) then (xk+1,yk+1) and pk+1= pk+2dy-2dx Solution: dx= 5-1 =4 dy= 3-1 =2 P0= 2dy – dx = 2 (2) – 4 = 0 Plot 1st point (1, 1) here as | dx | > | dy | (pk>0) then (xk+1,yk+1) and (else condition) pk+1= pk+2dy-2dx hence plot 2,2 and pk+1= -4 if Pk<0 then (xk+1,yk) and (if condition) pk+1= pk+2dy hence plot 3,2 and pk+1= 0 (pk>0) then (xk+1,yk+1) and (else condition) pk+1= pk+2dy-2dx hence plot 4,3 and pk+1=-4
  • 4.
    Therefore the pixelsare (1,1) (2,2) (3,2) (4,3) (5,3)
  • 5.
    Example 2: x1,y1= 2,3 x2, y2= 7, 7 Algorithm: dx = x2 - x1, dy = y2-y1 P0 =2dy - dx if Pk<0 then (xk+1,yk) and pk+1= pk+2dy else (pk>0) then (xk+1,yk+1) and pk+1= pk+2dy-2dx Solution: dx= 7-2=5 dy= 7-3=4 P0= 2dy – dx = 2 (4) – 5= 3 Plot 1st point (2,3) here as | dx | > | dy | (Pk>0) then (xk+1,yk+1) and (else condition) pk+1= pk+2dy-2dx hence plot 3,4 and pk+1= 1 (Pk>0) then (xk+1,yk+1) and (else condition) pk+1= pk+2dy-2dx hence plot 4,5 and pk+1= -1 Pk<0 then (xk+1,yk) and (if condition) pk+1= pk+2dy hence plot 5,5 pk+1= 7
  • 6.
    Therefore the pixelsare (2,3) (3,4) (4,5) (5,5) (6,6) (7,7)
  • 7.
    Reference Hearn,Baker - ComputerGraphics - C Version 2nd Ed http://edu.uokufa.edu.iq/staff/dr.nidhal/compressed%20comp.book/H earn,Baker%20-%20Computer%20Graphics%20- %20C%20Version%202nd%20Ed.pdf