manthanthakker40

Bresenham's line algorithm

Jul 23rd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. // bresenhams 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,e;
  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(initialise)
  43. x=x1;
  44. y=y1;
  45. putpixel((int)x,(int)y,15);
  46. //step4(initialise error variable)
  47. e=2*dy+dx;
  48. // step5(i=1)
  49. i=1;
  50. //step6
  51. do
  52. {
  53. while(e>=0)
  54. {
  55. y=y+1;
  56. e=e-2*dx;
  57. }
  58. x=x+1;
  59. e=e+2*dy;
  60. putpixel((int)x,(int)y,15);
  61. i=i+1;
  62. }while(i<=dx);
  63.  
  64.  
  65.  
  66.  
  67.  
  68. getche();
  69.  
  70. closegraph();
  71. clrscr();
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment