Thursday, June 21, 2018

DDA algorithm (Implementation of line drawing algorithm)


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<math.h>
  5. #include<dos.h>

  6. void main()
  7. {
  8. clrscr(); 
  9. int x1,y1,x2,y2,i=1; float x,y,dx,dy,len;

  10. detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi");

  11. textbackground(15); textcolor(15); setbkcolor(15); setcolor(8);

  12. cout<<"Enter starting pixels for point (x1,y1):"; cin>>x1>>y1;
  13. cout<<"\nEnter ending pixels for point (x2,y2):"; cin>>x2>>y2; line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());

  14. dx=abs(x2-x1); 
  15. dy=abs(y2-y1);

  16.          if(dy>dx)len=dy;
  17.           else len=dx;
  18.            dx=((x2-x1)/len);
  19. dy=((y2-y1)/len);
  20. x=x1+0.5*dx; y=y1+0.5*dy; while(i<=len)
  21. { 
  22. putpixel(getmaxx()/2+x,getmaxy()/2-y,8);
  23. }
  24. x+=dx; y+=dy; i++;

  25. delay(20);
  26. }
  27. getch();


No comments:

Post a Comment