DDA(Digital Differential Algorithm)

It is a scan conversion line algorithm based on either calculating dx and dy

ALGORITHM

  1. Line n points are (xa,ya) and(xb,yb)
  2. dx=xb-xa
  3. dy=yb-ya
  4. Determine length of line
  5. If absolute(dx) >=absolute(dy) then
  6. length =absolute(dx)
  7. else length=absolute(dy)
  8. ▲x=dx/length
  9. ▲y=dy/length
  10. x=xa
  11. y=ya
  12. setpixel(round(x),round(y)
  13. i=1
  14. while(i<=length)
  15. x=x+▲x
  16. y=y+▲y
  17. setpixel(round (x),round(y))
  18. i=i+1
  19. endwhile
  20. finish

DDA PROGRAM

  • #include(stdio.h)
  • #include<conio.h>
  • #include<math.h>
  • #include<graphics.h>
  • #define round(a)((int)(a+0.5))
  • void line_dda(int x1,int y1,int x2, int y2)
  • {
  • int dx,dy,steps,k;
  • float xinc,yinc,x,y;
  • x=x1;
  • y=y1;
  • dx=x2-x1;
  • dy=y2-y1;
  • if(abs(dx)>abs(dy)
  • { steps=abs(dx);
  • }
  • else
  • {steps=abs(dy);
  • }
  • xinc=dx/steps;
  • yinc=dy/steps;
  • putpixel(round (x),round(y),GREEN);
  • for(k=0;k<steps;k++)
  • {
  • x+=xinc;
  • y+=yinc;
  • putpixel(round(x),round(y),GREEN);
  • }
  • }
  • void main()
  • {
  • int x1,x2,y1,y2;
  • int gd=DETECT,gm;
  • printf(“input the coordinates \n”)
  • scanf(“%d %d %d %d “,&x1,&y1,&x2,&y2);
  • initgraph(&gd,&gm,”c:\\tc\\bgi”);
  • line_dda(x1,y1,x2,y2);
  • getch();
  • closegraph();
  • }

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *