BRESENHAM'S LINE DRAWING ALGORITHM ! !

Bresenham's Line Drawing algorithm
The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals and at each step choose between two different y coordinates.
For example, as shown in the following illustration, from position (2, 3) you need to choose between (3, 3) and (3, 4). You would like the point that is closer to the original line.
Bresenham’s Line Generation
At sample position  the vertical separations from the mathematical line are labelled as  and .
dupper and dlower
From the above illustration, the y coordinate on the mathematical line at  is −
Y = m(+1) + b
So,  and  are given as follows −


and

You can use these to make a simple decision about which pixel is closer to the mathematical line. This simple decision is based on the difference between the two pixel positions.

Let us substitute m with dy/dx where dx and dy are the differences between the end-points.



So, a decision parameter Pk for the kth step along a line is given by −


The sign of the decision parameter  is the same as that of .
If  is negative, then choose the lower pixel, otherwise choose the upper pixel.
Remember, the coordinate changes occur along the x axis in unit steps, so you can do everything with integer calculations. At step k+1, the decision parameter is given as −

Subtracting  from this we get −

But,  is the same as . So −

Where,  is either 0 or 1 depending on the sign of .
The first decision parameter  is evaluated at  is given as −

Now, keeping in mind all the above points and calculations, here is the Bresenham algorithm for slope m < 1 −
Step 1 − Input the two end-points of line, storing the left end-point in .
Step 2 − Plot the point .
Step 3 − Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value for the decision parameter as −

Step 4 − At each  along the line, starting at k = 0, perform the following test −
If  < 0, the next point to plot is  and
Otherwise,



Step 5 − Repeat step 4 (dx – 1) times.
For m > 1, find out whether you need to increment x while incrementing y each time.
After solving, the equation for decision parameter  will be very similar, just the x and y in the equation gets interchanged.


// C program for Bresenham's line drawing algorithm
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define sign(x) ((x>0)?1:((x<0)?-1:0))
// Driver program
int main()
{
      int driver,mode;
      int dx, dy, x,y, d, s1, s2, swap=0, temp,i;
      int x1,y1,x2,y2;
      driver = DETECT;
      detectgraph(&driver,&mode);
// Initialize graphics function
      initgraph(&driver,&mode,"");
      line((getmaxx()/2),0,(getmaxx()/2),getmaxy());
      line(0,(getmaxy()/2),getmaxx(),(getmaxy()/2));
      printf("Enter the two endpoints of the line:");
      printf("\nx1=");
      scanf("%d",&x1);
      printf("y1=");
      scanf("%d",&y1);
      printf("x2=");
      scanf("%d",&x2);
     printf("y2=");
      scanf("%d",&y2);
    // calculate dx & dy
dx = abs(x2 -x1);
      dy = abs(y2 -y1);
      s1 =sign(x2-x1);
      s2 =sign(y2-y1);
      if(dy > dx)
      {
            temp =dx;
            dx = dy;
            dy =temp;
            swap =1;
      }
      d = 2 * dy -dx;
      x = x1;
      y = y1;
      for(i = 1; i<= dx; i++)
      { // Put pixel for each step
            putpixel(x+(getmaxx()/2),(getmaxy()/2)-y,WHITE);
            while(d>= 0)
            {
                  if(swap)
                   x= x + s1;
                  else
                  {
             y= y + s2;
           d= d - 2* dx;
                  }
            }
            if(swap)
             y= y + s2;
            else
              x= x + s1;
            d = d +2 * dy;
      }
      getch();
      closegraph();
      return 0;
}

OUTPUT:-






Want a code for Dotted and Thick Line using Bresenham. Leave your email id in the comment box.

Comments