Bresenham's line drawing algorithm

1,846 views 22 slides May 15, 2020
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Easy to understand


Slide Content

Bresenham's Line Drawing Algorithm NEHRUREVATHY DEPARTMENT OF BCA

Bresenham's Line Algorithm This algorithm is used for scan converting a line. It was developed by Bresenham . It is an efficient method because it involves only integer addition, subtractions, and multiplication operations. These operations can be performed very rapidly so lines can be generated quickly.

The method work as follows: Starting point is (x0,y0) Pixel point is (x k ,y k ) Next pixel position is {(x k +1,y k ) and (x k +1,y k +1)} The position x k +1 is calculated as Y=m(x k +1)+b- ----------Equation-1.

Bresenham's Line Algorithm

Bresenham's Line Algorithm(contd..) Calculate d1 and d2: d1= y-y k d2= (y k +1)-y Subtract d1-d2: d1-d2 = y - y k – y k -1+y [sub y=m(x k +1)+b] = m(x k +1)-y k - y k -1 + m(x k +1)+b+b = 2m(x k +1)+2b-2y k -1

Bresenham's Line Algorithm(contd..) For Kth term: P k = Δ x(d2-d1) P k = Δ x(2m(x k +1)+2b-2y k -1) P k = Δ x .(2m(x k +1)+2b. Δ x-2y k . Δ x – Δ x P k = Δ x .2mx k + Δ x.2m+2b. Δ x-2y k . Δ x – Δ x [Assume m= Δ y/ Δ x ] P k = Δ x .2 . Δ y/ Δ x.x k + Δ x.2. Δ y/ Δ x +2b. Δ x-2y k . Δ x – Δ x

Bresenham's Line Algorithm(contd..) P k = 2 . Δ yx k + 2. Δ y +2b. Δ x-2y k . Δ x – Δ x P k = 2 . Δ yx k -2y k . Δ x + 2. Δ y +2b. Δ x – Δ x P k = 2 . Δ yx k -2y k . Δ x + 2. Δ y – Δ x(2b –1) [Assume C= 2. Δ y – Δ x(2b –1) ] P k = 2 . Δ yx k -2y k . Δ x +C P k+1 = 2 . Δ yx k+1 -2y k+1 . Δ x +C P k+1 - P k = 2 . Δ yx k+1 -2y k+1 . Δ x +C - 2 . Δ yx k +2y k . Δ x -C

Bresenham's Line Algorithm(contd..) P k+1 - P k = 2 . Δ y[x k+1 – x k ]-2 Δ x [y k+1 - y k ] [ Assume x k+1 = x k + 1] P k+1 = P k +2 . Δ y[ x k +1 – x k ]-2 Δ x [y k+1 - y k ] P k+1 = P k +2 . Δ y -2 Δ x [y k+1 - y k ] [where y k+1 - y k is either 0 or 1] The first parameter p0 is calculated as p0= 2 Δ y - Δ x

Bresenham's Line Algorithm Bresenham's Line Algorithm: Step1:  Start Algorithm Step2:  Declare variable x 1 ,x 2 ,y 1 ,y 2 ,d,i 1 ,i 2 ,dx,dy Step3:  Enter value of x 1 ,y 1 ,x 2 ,y 2           Where x 1 ,y 1 are coordinates of starting point and x 2 ,y 2  are coordinates of Ending point.

Bresenham's Line Algorithm Step4:   Calculate dx = x 2 -x 1                 Calculate dy = y 2 -y 1                 Calculate i 1 =2* dy                 Calculate i 2 =2*( dy-dx )                 Calculate d=i 1 -dx Step5:  Consider (x, y) as starting point and x end as maximum possible value of x.                 If dx < 0                         Then x = x 2                         y = y 2                           x end =x 1                 If dx > 0                     Then x = x 1                 y = y 1                       x end =x 2

Bresenham's Line Algorithm Step6 :  Generate point at ( x,y )coordinates. Step7:  Check if whole line is generated.                 If x > = x end                 Stop. Step8:  Calculate co-ordinates of the next pixel                 If d < 0                     Then d = d + i 1                 If d ≥ 0           Then d = d + i 2                 Increment y = y + 1

Bresenham's Line Algorithm Step9:  Increment x = x + 1 Step10:  Draw a point of latest (x, y) coordinates Step11:  Go to step 7 Step12:  End of Algorithm Example:  Starting and Ending position of the line are (1, 1) and (8, 5). Find intermediate points. Solution:   x 1 =1                 y 1 =1                 x 2 =8                 y 2 =5

Bresenham's Line Algorithm-Example: dx = x 2 -x 1 =8-1=7                 dy =y 2 -y 1 =5-1=4                 I 1 =2* ∆y=2*4=8                 I 2 =2*(∆y-∆x)=2*(4-7)=-6                 d = I 1 -∆x=8-7=1

Bresenham's Line Algorithm-Example: X y d=d+I 1  or I 2 1 d+I 2 =1 +(-6)=- 5 2 d+I 1 =- 5+8=3 2 d+I 2 =3 +(-6)=- 3 3 d+I 1 =- 3+8=5 3 d+I 2 =5 +(-6)=- 1 4 d+I 1 =- 1+8=7 4 d+I 2 =7 +(-6)= 1 5

Bresenham's Line Algorithm

Program to implement Bresenham's Line Drawing Algorithm: # include< stdio.h >   #include< graphics.h >   void   drawline ( int  x0,  int  y0,  int  x1,  int  y1)   {        int   dx ,  dy , p, x, y;        dx =x1-x0;        dy =y1-y0;       x=x0;       y=y0;       p=2* dy-dx ;  

Program to implement Bresenham's Line Drawing Algorithm:     while (x<x1)       {            if (p>=0)           {                putpixel (x,y,7);               y=y+1;               p=p+2*dy-2* dx ;           }            else            {                putpixel (x,y,7);               p=p+2* dy ;}               x=x+1;           }   }  

Program to implement Bresenham's Line Drawing Algorithm:   int  main()   {        int   gdriver =DETECT,  gmode , error, x0, y0, x1, y1;        initgraph (& gdriver , & gmode , "c:\\turboc3\\ bgi ");        printf ("Enter co-ordinates of first point: ");        scanf ("% d%d ", &x0, &y0);        printf ("Enter co-ordinates of second point: ");        scanf ("% d%d ", &x1, &y1);        drawline (x0, y0, x1, y1);        return  0;   }  

Advantage and Disadvantage : Advantage: 1. It involves only integer arithmetic, so it is simple. 2. It avoids the generation of duplicate points. 3. It can be implemented using hardware because it does not use multiplication and division. 4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not involve floating point calculations like DDA Algorithm. Disadvantage : 1. This algorithm is meant for basic line drawing only Initializing is not a part of Bresenham's line algorithm. So to draw smooth lines, you should want to look into a different algorithm.