Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6.  
  7. ////////////////////////////////////////////////////////////////////////////////
  8.  
  9. void plot(int x, int y)
  10. {
  11. std::cout << x << y << std::endl;
  12. }
  13. void Bresenham(int x1,
  14. int y1,
  15. int const x2,
  16. int const y2)
  17. {
  18. int delta_x(x2 - x1);
  19. // if x1 == x2, then it does not matter what we set here
  20. signed char const ix((delta_x > 0) - (delta_x < 0));
  21. delta_x = std::abs(delta_x) << 1;
  22.  
  23. int delta_y(y2 - y1);
  24. // if y1 == y2, then it does not matter what we set here
  25. signed char const iy((delta_y > 0) - (delta_y < 0));
  26. delta_y = std::abs(delta_y) << 1;
  27.  
  28. plot(x1, y1);
  29.  
  30. if (delta_x >= delta_y)
  31. {
  32. // error may go below zero
  33. int error(delta_y - (delta_x >> 1));
  34.  
  35. while (x1 != x2)
  36. {
  37. if ((error >= 0) && (error || (ix > 0)))
  38. {
  39. error -= delta_x;
  40. y1 += iy;
  41. }
  42. // else do nothing
  43.  
  44. error += delta_y;
  45. x1 += ix;
  46.  
  47. plot(x1, y1);
  48. }
  49. }
  50. else
  51. {
  52. // error may go below zero
  53. int error(delta_x - (delta_y >> 1));
  54.  
  55. while (y1 != y2)
  56. {
  57. if ((error >= 0) && (error || (iy > 0)))
  58. {
  59. error -= delta_y;
  60. x1 += ix;
  61. }
  62. // else do nothing
  63.  
  64. error += delta_x;
  65. y1 += iy;
  66.  
  67. plot(x1, y1);
  68. }
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. Bresenham(1,2, 5, 7);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement