Sunday, November 25, 2012

DRAW A LINE USING BRESENHAM ALGORITHM


DRAW A LINE USING BRESENHAM ALGORITHM

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xa,xb,ya,yb,x,y;
   char msg[25];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
  }
  printf("Enter the x1 and y1 value:\n");
  scanf("%d%d",&xa,&ya);
  printf("Enter the x2 and y2 value:\n");
  scanf("%d%d",&xb,&yb);
  linebres(xa,ya,xb,yb);
  getch();
  closegraph();
}
           
 void linebres(int xa,int ya,int xb,int yb)
{
            int dx=abs(xa-xb),dy=abs(ya-yb);
            int p=2*dy-dx;
            int twody=2*dy,twodxdy=2*(dy-dx);
            int x,y,xend;
            if(xa>xb)
            {
                    x=xb;y=yb;xend=xa;
            }
           

            else
            {
                        x=xa;y=ya;xend=xb;
            }
            putpixel(x,y,YELLOW);
            while(x<xend)
            {
                        x++;
                        if(p<0)
                                    p+=twody;
                        else
                        {
                                    y++;
                                    p+=twodxdy;
                        }
                        putpixel(x,y,YELLOW);
            }
}






























OUTPUT:-


Enter the x1 and y1 value:
100
100
Enter the x2 and y2value:
250
300














0 comments:

Post a Comment