Thursday, September 27, 2012

Graph – DDA and Breshenham Algorithm




Digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point. DDA is used for plotting horizontal or vertical lines. It is use to plot lines, triangles and polygons. DDA algorithm can be described as naive line-drawing algorithm, with minor variations.

Note : You should have graphics.h header file  in the specified directory otherwise no graphs  will be displayed. You also can copy the said file in  the current directory and declared it as  #include "graphics.h". Some of the software which meant for 64 bit system are without any graphics utility. ‘Turbo C ‘ is providing a lots of graphics utilities.




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

  void ddaGr(int xs,int ys,int xn,int yn)
      {
           int dx,dy,x=0,y=0,len,i,s,j;
              if((xn-xs)>(yn-ys))
                   {
                        len=(xn-xs);
                   }
              else
                   {
                        len=(yn-ys);
                   }

            dx=((xn-xs)/len);
            dy=((yn-ys)/len);
            for(i=1;i<=len;i++)
              {
                   x=x+dx;
                   y=y+dy;
                   putpixel(x,y,4);
                   if(dx>dy)
                        {
                            x++;
                        }
                   else
                        {
                            y++;
                        }
              }
      }

  int main()
      {
           int gd,gm,x1,y1,x2,y2;
           printf("\n\n\tEnter the co-ordinates of first point : ");
           scanf("%d%d",&x1,&y1);
           printf("\n\n\tEnter the co-ordinates of second point : ");
           scanf("%d %d",&x2,&y2);
           detectgraph(&gm,&gd); 
           initgraph(&gm,&gd,""); // mentioned proper path for bgi
           ddaGr(x1,y1,x2,y2);
           getch();
           closegraph();
      }




The Bresenham algorithm  used to draw lines on a computer screen, using only integer operation such as addition, subtraction and bit shifting. A modified extension of the  algorithm also implemented for dawing circles.

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

  int main()
      {
           int dx,dy,x,y,t,x1,y1,x2,y2;
           int gd,gm;
           printf("\n\n\tEnter the co-ordinates of first point : ");
           scanf("%d%d",&x1,&y1);
           printf("\n\n\tEnter the co-ordinates of second point : ");
           scanf("%d %d",&x2,&y2);
           dx = (x2 - x1);
           dy = (y2 - y1);
           t = 2 * (dy) - (dx);
           x = x1;
           y = y1;
           detectgraph(&gd,&gm);
           initgraph(&gd,&gm,"");
           putpixel(x,y,WHITE);
           while(x <= x2)
              {
                   if(t < 0)
                        {
                            x=x+1;
                            y=y;
                            t = t + 2 * (dy);
                        }
                   else
                        {
                            x=x+1;
                            y=y+1;
                            t = t + 2 * (dy - dx);
                        }
                   putpixel(x,y,WHITE);
              }
           getch();
           closegraph();
              return 0;
      }




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

  void plot(int x,int y,int xc,int yc)
      {
           putpixel(x+xc,y+yc,1);
           putpixel(y+xc,x+yc,1);
           putpixel(-y+xc,x+yc,1);
           putpixel(-x+xc,y+yc,1);
           putpixel(-x+xc,-y+yc,1);
           putpixel(-y+xc,-x+yc,1);
           putpixel(y+xc,-x+yc,1);
           putpixel(x+xc,-y+yc,1);
      }
  int main()
      {
           int gdriver, gmode;
           float Rad, xc, yc, x, y, p;
           printf("\n\n\tEnter the radius : ");
           scanf("%f",&Rad);
           printf("\n\tEnter the Center Point : ");
           scanf("%f%f",&xc,&yc);
           detectgraph(&gdriver,&gmode);
           initgraph(&gdriver, &gmode, "");
           x=0;
           y=Rad;
           plot(x,y,xc,yc);
           p=3-2*Rad;
           while(x<=y)
              {
                   if(p<0)
                        {
                            x=x+1;
                            plot(x,y,xc,yc);
                            p=p+4*x+6;
                        }
                   else
                        {
                            x=x+1;
                           y=y-1;
                            plot(x,y,xc,yc);
                            p=p+4*(x-y)+10;
                        }
              }
           getch();
           closegraph();

           return 0;
      }




No comments: