Draw a line using mid point line drawing algorithm


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

void midpoint(int, int, int, int);
int midx = 319, midy = 239;

void main()
{
      int gdriver=DETECT, gmode, x1, y1, x2, y2;
      initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
      cleardevice();

      printf("Enter the point (X1,Y1) : ");
      scanf("%d%d",&x1,&y1);
      printf("Enter the point (X2,Y2) : ");
      scanf("%d%d",&x2,&y2);

      midpoint(x1,y1,x2,y2);

      getch();
      closegraph();
}

void midpoint(int x1, int y1, int x2, int y2)
{
      int x=x1, y=y1, dx=x2-x1, dy=y2-y1, ca, cb, d, d1, d2;

      line(0,midy,2*midx,midy);     line(midx,0,midx,2*midy);
      putpixel(midx+x1,midy-y1,14); putpixel(midx+x2,midy-y2,14);
      getch();

      if(dx > 0)  cb = 1;
      else        cb = -1;
      if(dy > 0)  ca = 1;
      else        ca = -1;

      if(abs(dy) < abs(dx))
      {
            d  = 2*ca*dy - cb*dx;
            d1 = 2*ca*dy;
            d2 = 2*(ca*dy - cb*dx);
      }
      else
      {
            d  = -2*cb*dx + ca*dy;
            d1 = -2*cb*dx;
            d2 = 2*(ca*dy - cb*dx);
      }

      putpixel(midx+x,midy-y,4);
      while(x!=x2 || y!=y2)
      {
            if(dx == 0)
                  y += ca;
            else if(abs(dy) <= abs(dx))
            {
                  x += cb;
                  if(d<0)
                        d += d1;
                  else
                  {
                        d += d2;
                        y += ca;
                  }
            }
            else
            {
                  y += ca;
                  if(d>0)
                        d += d1;
                  else
                  {
                        d += d2;
                        x += cb;
                  }
            }
            putpixel(midx+x,midy-y,4);    delay(10);
      }
}

Related Posts Plugin for WordPress, Blogger...

Engineering material

GTU IDP/ UDP PROJECT

GTU IDP/ UDP PROJECT

Patel free software download

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP