Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Sciences
MDS, University Ajmer, Rajasthan, India
1
 Bresenham Line drawing algorithm is used to determine
closest points to be illuminated on the screen to form a
line.
 As we know a line is made by joining 2 points, but in a
computer screen, a line is drawn by illuminating the pixels
on the screen.
10 11 12 13
Specified
line path
10
11
12
13
Decide what is the next pixel position:
(11,11) or (11,12)
2
 For the pixel position
xk+1=xk+1, which one
we should choose:
 (xk+1,yk) or (xk+1, yk+1)
y=mx+
b
yk
yk+1
yk+2
yk+3
xk xk+1 xk+2 xk+3
3
The slope of a line plays a major role in the line equation that's why
Bresenham line drawing algorithm calculates the equation according to
the slope of the line.
The slope of the line can be greater than 1 (m>1) or less than or equal
to 1 (m<=1).
Now enough talking let's derive the equation.
 To draw a line we have to calculate the points or pixels to be illuminated on the screen.
Now while drawing a line a sometimes it passes through 2 pixels at the same time then
we have to choose 1 pixel from the 2 to illuminate it.
so, the bresenham algorithm calculates the distance from the intersection point y to
both the pixels and whichever is smaller we choose that pixel.
4
 The equation of Line is:
 Y=mx+c ....(1)
 Here m is the slope of Line and C is y-Intercept of line.
The slope can also be written as
.....(2)
 First we calculate the slope of the line if slope is less than 1, then X will
always be incremented.
so at (m<=1)
 we calculate the d1 which is distance between intersection point y to the
pixel yk
So, d1 = y-yk
d1 = mxk+1 +c -yk By using ....(1)
Similarly d2 is the distance between pixel yk+1 and intersection point y.
d2 = yk+1-y
d2 = yk+1-(mxk+1+c) By using ....(1)
Note: here x is always incrementing so we can write xk+1 as xk +1 and
here yk+1 is next pixel so we can write it as yk+1.
 subtracting d2 from d1
d1-d2 = m(xk+1) +c -yk – [yk+1-(mxk+1+c)]
= m(xk+1)+c -yk – yk-1+m(xk+1)+c
d1-d2 = 2m(xk+1)-2yk+2c-1 .....(3)
5
 Multiplying both side by (dx)
dx(d1-d2) = 2dy(xk+1)-2dx(yk)+2dx(c)-dx
 Now we need to find decision parameter PK
PK= dx(d1-d2) and,
 C = 2dy+2dx(c)-dx which is constant
 So new equation is.
PK =2dy(xk)-2dx(yk) +C .......(4)
 Now our next parameter will be
PK+1 =2dy(xk+1)-2dx(yk+1) +C .....(5)
 Subtracting Pk from PK+1
Pk+1-Pk= 2dy(xk+1-xk)-2dx(yk+1-yk) +C-C
Note: here x is always incrementing so we can write xk+1 as xk +1
Pk+1-Pk= 2dy(xk-xk+1)-2dx(yk+1-yk)
Pk+1 = Pk+2dy-2dx(yk+1-yk)........(6)
 when Pk<0 then (d1-d2)<0
 So d1<d2 then we will write yk+1 as yk because current pixel’s distance from intersection point
y is smaller.so, we will have to choose current pixel.
Then our formula will be:
 Pk+1 = Pk+2dy-2dx(yk-yk)
 Pk+1 = Pk+2dy
6
 And when Pk>0 then (d1-d2)>0
 So d1>d2 then we will write yk+1 as yk+1 because current
pixel’s distance from intersection point y is larger.so,
we will have to choose upper pixel.
 At that time our formula will be:
Pk+1 = Pk+2dy-2dx(yk+1-yk)
Pk+1 = Pk+2dy-2dx
 We can say that (yk+1-yk) value can either be 0 or 1.
For Initial decision parameter
 From 4th equation
PK = 2dy(xk)-2dx(yk) +C
PK = 2dy(xk)-2dx(yk)+2dx(c)-dx+2dy
 By using 1st equation
yk=m(xk)+c
c=yk-m(xk)
P0 = 2dy(xk)-2dx(yk)+2dx(yk-m(xk)-dx (By using 2)
= 2dy(xk)-2dx(yk)+2dxyk-2dyxk+2dy-dx
P0 =2dy-dx
7
 But if the slope of line is greater then 1 (m>1).
then our Y coordinate will always be incremented and we have to choose
between xk or xk+1.
So, our Line equation will be:
Yk+1 = m(x)+c
 Now, In this case our d1 will be the distance between intersection point x and
pixel xk
d1= x-xk By using ....(7)
d1= 1/m(yk+1-c)-xk
And similarly our d2 will be the distance between intersection point x and
8
 pixel xk+1
d2 = xk+1-x By using ....(7)
d2 = xk+1-1/m(yk+1-c)
 Note: here y is always incrementing so we can write yk+1 as yk+1 and here
xk+1 is next pixel so we can write it as xk+1.
subtracting d2 from d1
d1-d2 =1/m(yk+1-c)-xk – [xk+1-1/m(yk+1-c)]
= 1/m(yk+1-c)-xk – xk-1+1/m(yk+1-c)
d1-d2 = 2/m(yk+1-c)-2xk-1
d1-d2 = 2dx(yk+1-c)-2dyxk-dy
 Multiplying both side by (dy)
dy(d1-d2) = 2dxyk+2dx-2dxc-2dyxk-dy
 Now we need to find decision parameter PK
PK = dy(d1-d2)
C = 2dx-2dxc-dy which is constant
So new equation is
PK = 2dxyk-2dyxk +C ......(8)
9
 Now our next parameter will be
Pk+1 = 2dx(yk+1)-2dy(xk+1)+C
 Substracting Pk from Pk+1
Pk+1-Pk = 2dx(yk+1-yk)-2dy(xk+1-xk)+C-C
Note: here x is always incrementing so we can write yk+1 as yk +1
Pk+1-Pk = 2dx(yk +1-yk)-2dy(xk+1-xk)
Pk+1=Pk+2dx-2dy(xk+1-xk)
when Pk<0 then (d1-d2)<0
 So d1<d2 then we will write xk+1 as xk because current pixel’s distance from
intersection point x is smaller.so, we will have to choose current pixel.
Then our formula will be:
Pk+1 = Pk+2dx-2dy(xk-xk)
Pk+1 = Pk+2dx
And when Pk>0 then (d1-d2)>0
 So d1>d2 then we will write xk+1 as xk+1 because current pixel’s distance from
intersection point x is Larger.so, we will have to choose next pixel.
 At that time our formula will be:
Pk+1= Pk+2dx-2dy(xk+1-xk)
Pk+1= Pk+2dx-2dy
10
 For Initial decision parameter
From 8th equation
PK = 2dxyk-2dyxk +C
P0 = 2dxy0+2dx-2dxc-2dyx0-dy
By using 1st equation
yk=m(xk)+c
 c=yk-m(xk)P0 = 2dxy0-2dyx0+2dx-2dx(y0-m(x0))-dy
P0 = 2dxy0-2dyx0+2dx-2dxy0+2dyx0-dy (By using 2)
P0 = 2dx-dy
hence formulas for bresenham is derived.
11
Bresenham’s Line Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd =DETECT,gm;
initgraph(&gd,&gm,"C:TurboC3BGI");
int x,x1,y,y1,dx,dy,pk;
printf("Enter first co-ordinates");
scanf(" %d%d",&x,&y);
printf("Enter second co-ordinates");
scanf(" %d%d",&x1,&y1);
dx=x1-x;
dy=y1-y;
pk=2*dy-dx;
12
while(x<x1)
{ x++;
if(pk<0){
pk+=2*dy;
}
else{y++;
pk+=2*(dy-dx);
}
putpixel(x,y,9);
}
getch();
closegraph();
}
13
 Example on Bresenham’s algorithm: Consider the line from (0, 0) to (-8,-4),
use general
 Bresenham’s line algorithm to rasterize this line. Evaluate and tabulate all the
steps involved.
 Solution:
 Given data,
 (x 1, y1)= (0, 0) (x2, y2)= (-8,-4)
 ∆x=x2-x1=-8-0=8
 ∴ S1=-1
 ∆y=y2-y1
 =-4-0=4
 ∴S2=-1
 Decision Variable=e=2*(∆y)-(∆x)
 ∴e= 2*(4)-(8)
 =8-8 =0
14
 The result in tabulated form as,
15
 ∴ By pictorial presentation in graph is as
shown below,
16

Bresenham's line algorithm

  • 1.
    Prof. Neeraj Bhargava PoojaDixit Department of Computer Science School of Engineering & System Sciences MDS, University Ajmer, Rajasthan, India 1
  • 2.
     Bresenham Linedrawing algorithm is used to determine closest points to be illuminated on the screen to form a line.  As we know a line is made by joining 2 points, but in a computer screen, a line is drawn by illuminating the pixels on the screen. 10 11 12 13 Specified line path 10 11 12 13 Decide what is the next pixel position: (11,11) or (11,12) 2
  • 3.
     For thepixel position xk+1=xk+1, which one we should choose:  (xk+1,yk) or (xk+1, yk+1) y=mx+ b yk yk+1 yk+2 yk+3 xk xk+1 xk+2 xk+3 3 The slope of a line plays a major role in the line equation that's why Bresenham line drawing algorithm calculates the equation according to the slope of the line. The slope of the line can be greater than 1 (m>1) or less than or equal to 1 (m<=1). Now enough talking let's derive the equation.
  • 4.
     To drawa line we have to calculate the points or pixels to be illuminated on the screen. Now while drawing a line a sometimes it passes through 2 pixels at the same time then we have to choose 1 pixel from the 2 to illuminate it. so, the bresenham algorithm calculates the distance from the intersection point y to both the pixels and whichever is smaller we choose that pixel. 4
  • 5.
     The equationof Line is:  Y=mx+c ....(1)  Here m is the slope of Line and C is y-Intercept of line. The slope can also be written as .....(2)  First we calculate the slope of the line if slope is less than 1, then X will always be incremented. so at (m<=1)  we calculate the d1 which is distance between intersection point y to the pixel yk So, d1 = y-yk d1 = mxk+1 +c -yk By using ....(1) Similarly d2 is the distance between pixel yk+1 and intersection point y. d2 = yk+1-y d2 = yk+1-(mxk+1+c) By using ....(1) Note: here x is always incrementing so we can write xk+1 as xk +1 and here yk+1 is next pixel so we can write it as yk+1.  subtracting d2 from d1 d1-d2 = m(xk+1) +c -yk – [yk+1-(mxk+1+c)] = m(xk+1)+c -yk – yk-1+m(xk+1)+c d1-d2 = 2m(xk+1)-2yk+2c-1 .....(3) 5
  • 6.
     Multiplying bothside by (dx) dx(d1-d2) = 2dy(xk+1)-2dx(yk)+2dx(c)-dx  Now we need to find decision parameter PK PK= dx(d1-d2) and,  C = 2dy+2dx(c)-dx which is constant  So new equation is. PK =2dy(xk)-2dx(yk) +C .......(4)  Now our next parameter will be PK+1 =2dy(xk+1)-2dx(yk+1) +C .....(5)  Subtracting Pk from PK+1 Pk+1-Pk= 2dy(xk+1-xk)-2dx(yk+1-yk) +C-C Note: here x is always incrementing so we can write xk+1 as xk +1 Pk+1-Pk= 2dy(xk-xk+1)-2dx(yk+1-yk) Pk+1 = Pk+2dy-2dx(yk+1-yk)........(6)  when Pk<0 then (d1-d2)<0  So d1<d2 then we will write yk+1 as yk because current pixel’s distance from intersection point y is smaller.so, we will have to choose current pixel. Then our formula will be:  Pk+1 = Pk+2dy-2dx(yk-yk)  Pk+1 = Pk+2dy 6
  • 7.
     And whenPk>0 then (d1-d2)>0  So d1>d2 then we will write yk+1 as yk+1 because current pixel’s distance from intersection point y is larger.so, we will have to choose upper pixel.  At that time our formula will be: Pk+1 = Pk+2dy-2dx(yk+1-yk) Pk+1 = Pk+2dy-2dx  We can say that (yk+1-yk) value can either be 0 or 1. For Initial decision parameter  From 4th equation PK = 2dy(xk)-2dx(yk) +C PK = 2dy(xk)-2dx(yk)+2dx(c)-dx+2dy  By using 1st equation yk=m(xk)+c c=yk-m(xk) P0 = 2dy(xk)-2dx(yk)+2dx(yk-m(xk)-dx (By using 2) = 2dy(xk)-2dx(yk)+2dxyk-2dyxk+2dy-dx P0 =2dy-dx 7
  • 8.
     But ifthe slope of line is greater then 1 (m>1). then our Y coordinate will always be incremented and we have to choose between xk or xk+1. So, our Line equation will be: Yk+1 = m(x)+c  Now, In this case our d1 will be the distance between intersection point x and pixel xk d1= x-xk By using ....(7) d1= 1/m(yk+1-c)-xk And similarly our d2 will be the distance between intersection point x and 8
  • 9.
     pixel xk+1 d2= xk+1-x By using ....(7) d2 = xk+1-1/m(yk+1-c)  Note: here y is always incrementing so we can write yk+1 as yk+1 and here xk+1 is next pixel so we can write it as xk+1. subtracting d2 from d1 d1-d2 =1/m(yk+1-c)-xk – [xk+1-1/m(yk+1-c)] = 1/m(yk+1-c)-xk – xk-1+1/m(yk+1-c) d1-d2 = 2/m(yk+1-c)-2xk-1 d1-d2 = 2dx(yk+1-c)-2dyxk-dy  Multiplying both side by (dy) dy(d1-d2) = 2dxyk+2dx-2dxc-2dyxk-dy  Now we need to find decision parameter PK PK = dy(d1-d2) C = 2dx-2dxc-dy which is constant So new equation is PK = 2dxyk-2dyxk +C ......(8) 9
  • 10.
     Now ournext parameter will be Pk+1 = 2dx(yk+1)-2dy(xk+1)+C  Substracting Pk from Pk+1 Pk+1-Pk = 2dx(yk+1-yk)-2dy(xk+1-xk)+C-C Note: here x is always incrementing so we can write yk+1 as yk +1 Pk+1-Pk = 2dx(yk +1-yk)-2dy(xk+1-xk) Pk+1=Pk+2dx-2dy(xk+1-xk) when Pk<0 then (d1-d2)<0  So d1<d2 then we will write xk+1 as xk because current pixel’s distance from intersection point x is smaller.so, we will have to choose current pixel. Then our formula will be: Pk+1 = Pk+2dx-2dy(xk-xk) Pk+1 = Pk+2dx And when Pk>0 then (d1-d2)>0  So d1>d2 then we will write xk+1 as xk+1 because current pixel’s distance from intersection point x is Larger.so, we will have to choose next pixel.  At that time our formula will be: Pk+1= Pk+2dx-2dy(xk+1-xk) Pk+1= Pk+2dx-2dy 10
  • 11.
     For Initialdecision parameter From 8th equation PK = 2dxyk-2dyxk +C P0 = 2dxy0+2dx-2dxc-2dyx0-dy By using 1st equation yk=m(xk)+c  c=yk-m(xk)P0 = 2dxy0-2dyx0+2dx-2dx(y0-m(x0))-dy P0 = 2dxy0-2dyx0+2dx-2dxy0+2dyx0-dy (By using 2) P0 = 2dx-dy hence formulas for bresenham is derived. 11
  • 12.
    Bresenham’s Line DrawingAlgorithm #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd =DETECT,gm; initgraph(&gd,&gm,"C:TurboC3BGI"); int x,x1,y,y1,dx,dy,pk; printf("Enter first co-ordinates"); scanf(" %d%d",&x,&y); printf("Enter second co-ordinates"); scanf(" %d%d",&x1,&y1); dx=x1-x; dy=y1-y; pk=2*dy-dx; 12
  • 13.
  • 14.
     Example onBresenham’s algorithm: Consider the line from (0, 0) to (-8,-4), use general  Bresenham’s line algorithm to rasterize this line. Evaluate and tabulate all the steps involved.  Solution:  Given data,  (x 1, y1)= (0, 0) (x2, y2)= (-8,-4)  ∆x=x2-x1=-8-0=8  ∴ S1=-1  ∆y=y2-y1  =-4-0=4  ∴S2=-1  Decision Variable=e=2*(∆y)-(∆x)  ∴e= 2*(4)-(8)  =8-8 =0 14
  • 15.
     The resultin tabulated form as, 15
  • 16.
     ∴ Bypictorial presentation in graph is as shown below, 16