Loading

c program dda bresenham line and circle drawing algorithm

C - Program to Implement DDA and Bresenhams Line and Circle Drawing Algorithm



#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
class shape                           // Declare a class shape
{
            float x1,y1,x2,y2;
public:
            void dda(float x1,float x2,float y1,float y2);                    // pass rthe parameters to draw a line
            void Bresenhamsline(float x1,float x2,float y1,float y2);               // pass the co-ordinates to draw the line
            using bresenhamsline drawing algorithm
            void Bresenhamscircle();                                                //pass the co-ordinates to draw the circle                                     using bresenhams circle drawing algorithm

};

void shape::dda(float x1,float x2,float y1,float y2 )
{
      int dx,dy,step,xinc,yinc;

      float slop,x3,y3;

      dx=x2-x1;                           //passing the parameters to         starting and ending points of parameter
      dy=y2-y1;

      if(abs(dx)>abs(dy))
            step=abs(dx);                 // calculate the absolute value
      else
            step=abs(dy);
      if(dx==0)
            slop=float(dy/(dx+0.1));      //find the slope of line
      else
            slop=float(dy/dx);

      putpixel(x1,y1,WHITE);
                                         
      x3=x1;
      y3=y1;

      for(int i=1;i<step;i++)
      {
            If(abs(dx)>abs(dy)&&x3<x2)
{
      x3=x3+1;
      y3=float(y3+slop);
}
else if(abs(dx)>abs(dy) && x3>x2)
{
      x3=x3-1;
      y3=y3-slop;
}
else if(abs(dx)<abs(dy) && y3<y2)
{
      x3=float(x3+(1/slop));
      y3=y3+1;
}
else if(abs(dx)<abs(dy) && y3>y2)
{
      x3=float(x3-(1/slop));
      y3=y3-1;
}
xinc=(x3+0.5);
yinc=(y3+0.5);
putpixel(xinc,yinc,WHITE);
}
}

void shape::Bresenhamsline(float x1,float x2,float y1,float y2)
{
      float Dx,Dy,x,y,e;
      Dx = x2 - x1;
      Dy = y2 - y1;
      Dx = fabs(Dx);
      Dy = fabs(Dy);
      x = x1;
      y = y1;
      e = 2 * (Dy-Dx);
      int i;
      i = 1;
      cleardevice();
      do
      {
            putpixel(x,y,15);
            while(e>=0)
            {
                  y = y + 1;
                  e = e - (2*Dx);
            }
            x = x + 1;
            e = e + (2 * Dy);
            i = i + 1;
      }while(i<=Dx);
}

void shape::Bresenhamscircle()
{
      float r,d,x,y;
      cout<<"\n\tEnter the radius of a circle->";
      cin>>r;
      clrscr();
      cleardevice();
      x = 0;
      y = r;
      d = 3 - (2 * r);
      do
      {
            putpixel(200+x,200+y,15);
            putpixel(200+y,200+x,15);
            putpixel(200+y,200-x,15);
            putpixel(200+x,200-y,15);
            putpixel(200-x,200-y,15);
            putpixel(200-y,200-x,15);
            putpixel(200-y,200+x,15);
            putpixel(200-x,200+y,15);
            if(d<=0)
            {
                  d = d + (4*x) + 6;
            }
            else
            {
                  d = d + (4*(x-y)) + 10;
                  y = y - 1;
            }
            x = x +1;
            delay(10);
      }while(x<y);
}

void main()
{
int ch;
float x1,x2,y1,y2;
shape s;
    int gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
   errorcode = graphresult();
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }
   do
   {
cout<<"\n\n\t\tMENU";
cout<<"\n\t1:DDA line drawing";
cout<<"\n\t2:Bresenhamsline";
cout<<"\n\t3:Bresenhamscircle;";
cout<<"\n\nEnter Your choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter (x1,y1) point";
      cin>>x1>>y1;
      cout<<"\nEnter (x2,y2) point";
      cin>>x2>>y2;               ;
      clrscr();
      s.dda(x1,x2,y1,y2);
      break;
case 2:
      cout<<"\nEnter (x1,y1) point";
      cin>>x1>>y1;
      cout<<"\nEnter (x2,y2) point";
      cin>>x2>>y2;               ;
      clrscr();
      s.Bresenhamsline(x1,x2,y1,y2);
      break;
case 3: clrscr();
      s.Bresenhamscircle();
      break;
}
}while(ch!=4);
getch();
}

This entry was posted in , , . Bookmark the permalink.

Leave a reply