Monday, 29 December 2014

Program to draw a line using Bresenham's Line Algorithm

Program to draw a line using Bresenham's Line Algorithm

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

int main()
{
   int x1,x2,dx,p,y1,y2,i=0,dy,xmax,ymax;
   float xmid,ymid;
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)     /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
   xmax= getmaxx()/2;
   ymax= getmaxy()/2;
   printf("mid point %d \t %d", xmax,ymax);
   printf("\n enter the value of x1 and y1:");
   scanf("%d %d",&x1,&y1);
   printf("\n enter the values of x2 and y2:");
   scanf("%d %d",&x2,&y2);
   dx=x2-x1;
   dy=y2-y1;
   printf("\n value of dx and dy :%d %d",dx,dy);
   p=(2*dy)-dx;
   printf("\n value of p=%d",p);
   putpixel(x1+319.5,-y1+234.5,2);
   for(i=0;i<dx;i++)
   {
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*dy;
       }
   else
   {
y1=y1+1;
x1=x1+1;
p=p+(2*dy)-(2*dx);
   }
   putpixel(x1+314.5,-y1+239.5,2);
   }
   getch();
   closegraph();
   return 0;
}

OUTPUT:





No comments:

Post a Comment