Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- ////////////////////////////////////////////////////////////////////////////////
- void plot(int x, int y)
- {
- std::cout << x << y << std::endl;
- }
- void Bresenham(int x1,
- int y1,
- int const x2,
- int const y2)
- {
- int delta_x(x2 - x1);
- // if x1 == x2, then it does not matter what we set here
- signed char const ix((delta_x > 0) - (delta_x < 0));
- delta_x = std::abs(delta_x) << 1;
- int delta_y(y2 - y1);
- // if y1 == y2, then it does not matter what we set here
- signed char const iy((delta_y > 0) - (delta_y < 0));
- delta_y = std::abs(delta_y) << 1;
- plot(x1, y1);
- if (delta_x >= delta_y)
- {
- // error may go below zero
- int error(delta_y - (delta_x >> 1));
- while (x1 != x2)
- {
- if ((error >= 0) && (error || (ix > 0)))
- {
- error -= delta_x;
- y1 += iy;
- }
- // else do nothing
- error += delta_y;
- x1 += ix;
- plot(x1, y1);
- }
- }
- else
- {
- // error may go below zero
- int error(delta_x - (delta_y >> 1));
- while (y1 != y2)
- {
- if ((error >= 0) && (error || (iy > 0)))
- {
- error -= delta_y;
- x1 += ix;
- }
- // else do nothing
- error += delta_x;
- y1 += iy;
- plot(x1, y1);
- }
- }
- }
- int main()
- {
- Bresenham(1,2, 5, 7);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement