Friday, July 8, 2016

BRESENHAM’S LINE DRAWING ALGORITHM


An accurate and efficient raster line generating algorithm developed by Bresenham, scan converts lines using only incremental integer calculation that can be adopted to display circles and other curves.

          To illustrate Bresenham’s approach, we consider the scan conversion process for lines with slopes greater and less than unity. In the former case, pixels positions along the line path are determined by sampling at a unit ‘y’ intervals. Starting from first end point (x0,y0) of the given line, we step to each successive y-position and plot the pixel whose scan-line x-value is closest to the line path. Assuming we have determined that the pixel at (xk, yk) is to be displayed, we next need to decide which pixel to plot in yk+1th row. Our choices are the pixels at (xk, yk+1) and (xk +1, yk +1).

Source code

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
          int x1,y1,x2,y2,xi,yi,dx,dy,k,p;
          int gdrive=DETECT, gmode;
          initgraph(&gdrive, &gmode, "c:\\tc\\bgi");

          printf("Enter two end points (x1,y1) and (x2,y2):");
          scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

          dx=abs(x2-x1);
          dy=abs(y2-y1);

          if (x2>x1)
          {
                   xi=1;
          }
          else
          {
                   xi=-1;
          }
          if (y2>y1)
          {
                   yi=1;
          }
          else
          {
                   yi=-1;
          }

          putpixel(x1,y1,RED);

          if (dx>dy)
          {
                   p=(2*dy)-dx;
                   k=0;
                   do
                   {
                       if(p<0)
                       {
                             x1=x1+xi;
                             p=p+(2*dy);
                       }
                       else
                       {
                             x1=x1+xi;
                             y1=y1+yi;
                             p=p+(2*dy)-(2*dx);
                       }
                       putpixel(x1,y1,RED);
                       k++;
                   }while(k<=dx);
          }
          else
          {
                   p=(2*dx)-dy;
                 // k=0;
                    for(k=0;k<=dy;k++)
                   {
                             if(p<0)
                             {

                                      y1=y1+yi;
                                      p=p+(2*dx);
                             }
                             else
                             {
                                      x1=x1+xi;
                                      y1=y1+yi;
                                      p=p+(2*dx)-(2*dy);
                             }
                             putpixel(x1,y1,RED);

                   }
          }
          getch();
          closegraph();
}


Note: Above source code is implanted in c++ programming language and witten using  turboC++   libraries.

No comments:

Post a Comment