Bresenham's Line Algorithm in Computer Graphics with Example

 Bresenham's Line Algorithm: Bresenham's line algorithm is a high-efficiency incremental approach for scanning lines. It generates mathematically correct results by utilizing only integer addition, subtraction, and multiplication by 2. Moreover, it is possible to accomplish this with a simple arithmetic shift operation. This faster algorithm is developed by Bresenham in the 19th century.


Advantage and Disadvantage of Bresenham's Line Algorithm: One of the advantages of this line algorithm is that it is faster because of involving only arithmetic operation between integers. It is faster also for avoiding duplicate points in the calculation. The disadvantage of this algorithm is that, because of a simple and basic algorithm in the Computer graphics line algorithm, the drawing line is not smooth.



Bresenham's Line Algorithm in C language:


Bresenham(a1, b1, a2, b2)

{

    a=a1

    b=b1

    da=a2-a1

    db=b2-b1

    d=2*db-da

    while (a<=a2)

    {

        putpixel(a,b)

        a++

        if (d<0)

        {

            d=d+2*db

        }

        else

        {

            d=d+2*db-2*da

            b++

        }

    }

}



Example of Bresenham's Line Algorithm:

Ex 1: Draw a line from point (1,1) to point (8,5)


Solution:

Let,

    (a1,b1)=(1,1) and (a2, b2)=(8,5)

Now, Applying the Bresenham's Line Algorithm procedure for this math, 

    da=a2-a1=8-1=7

    db=b2-b1=5-1=4


Now the Increments, 

    inc1=2*db=2*4=8

    inc2=2(db-da)=2*(4-7)=-6


Now, distance

    d=inc1-da=8-7=1


Distance table with points generations:


    d          a     b

    1          1     1

1+inc2=-5      2     2

-5+inc1=3      3     2

3+inc2=-3      4     4

-3+inc1=5      5     3

5+inc2=-1      6     4

-1+inc1=7      7     4

7+inc2=1       8     5

Post a Comment

Previous Post Next Post