Advertisement
manthanthakker40

DDA line Algo

Jul 22nd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. // implement DDA algorithm
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<stdlib.h>
  5. #include<graphics.h>
  6. int sign(float x)
  7. {
  8. int signa;
  9. if(x>=0.0)
  10. signa=1;
  11. else
  12. signa=-1;
  13. return signa;
  14. }
  15. void main()
  16. {
  17. //step 1
  18. int gd=DETECT,gm;
  19. float x1,y1,x2,y2,dx,dy,length;
  20. int i=1;
  21. float dx1,dy1,x,y;
  22. int sign(float x);
  23.  
  24. initgraph(&gd,&gm,"c:/tc1/tc/tc/bgi");
  25. printf("enter point 1 \n");
  26. scanf("%f",&x1);
  27. scanf("%f",&y1);
  28. printf("coordinates are %f %f \n",x1,y1);
  29. printf("enter point 2 \n");
  30. scanf("%f",&x2);
  31. scanf("%f",&y2);
  32. printf("coordinates are %f %f \n",x2,y2);
  33. //step 2
  34. if(x2>x1)
  35. dx=x2-x1;
  36. else
  37. dx=x1-x2;
  38. if(y2>y1)
  39. dy=y2-y1;
  40. else
  41. dy=y1-y2;
  42. //step3
  43. if(dx>=dy)
  44. length=dx;
  45. else
  46. length=dy;
  47. //step 4
  48. dx1=(x2-x1)/length;
  49. dy1=(y2-y1)/length;
  50. //step5
  51. x=x1+0.5*sign(dx1);
  52. y=y1+0.5*sign(dy1);
  53.  
  54.  
  55.  
  56. //step 6
  57. while(i<=length)
  58. {
  59.  
  60. putpixel((int)x,(int)y,RED);
  61. x=x+dx1;
  62. y=y+dy1;
  63. i=i+1;
  64. }
  65.  
  66. getche();
  67.  
  68. closegraph();
  69. clrscr();
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement