Posted By

ZeusEm on 03/10/15


Tagged


Versions (?)

Graphic Design | Bresenham's Line Algorithm


 / Published in: C
 

This is an implementation of the Bresenham's Line Algorithm in Computer Graphics Design in the C programming language.

  1. //Shubham Mehta, Write a program to implement Bresenham's Line Algorithm.
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<graphics.h>
  5. void main()
  6. {
  7. int gd, gm;
  8. float x1, x2, y1, y2, dx, dy, p, xk, yk, slope;
  9. clrscr();
  10. printf("%s", "Please enter x1: ");
  11. scanf("%f", &x1);
  12. printf("%s", "Please enter y1: ");
  13. scanf("%f", &y1);
  14. printf("%s", "Please enter x2: ");
  15. scanf("%f", &x2);
  16. printf("%s", "Please enter y2: ");
  17. scanf("%f", &y2);
  18. gd=DETECT, gm;
  19. initgraph(&gd, &gm, "C:\\TC\\BGI");
  20. putpixel(x1, y1, RED);
  21. slope=(y2-y1)/(x2-x1);
  22. dx=x2-x1;
  23. dy=y2-y1;
  24. xk=x1;
  25. yk=y1;
  26. if(slope<1)
  27. {
  28. p=(2*dy)-dx;
  29. for( ; x1<=x2; x1++)
  30. {
  31. if(p<0)
  32. {
  33. putpixel(++xk, yk, RED);
  34. p+=(2*dy);
  35. }
  36. else
  37. {
  38. putpixel(++xk, ++yk, RED);
  39. p=p+(2*dy)-(2*dx);
  40. }
  41. }
  42. }
  43. else
  44. {
  45. p=(2*dx)-dy;
  46. for( ; y1<=y2; y1++)
  47. {
  48. if(p<0)
  49. {
  50. putpixel(xk, ++yk, RED);
  51. p+=(2*dx);
  52. }
  53. else
  54. {
  55. putpixel(++xk, ++yk, RED);
  56. p=p+(2*dx)-(2*dy);
  57. }
  58. }
  59. }
  60. getch();
  61. closegraph();
  62. }

Report this snippet  

You need to login to post a comment.