RicardasSim

trying Bresenham's line algorithm

Jun 11th, 2020
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. void LineTest( int32_t x0, int32_t y0, int32_t x1, int32_t y1 )
  2. {
  3.  
  4. /*
  5. https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
  6.  
  7. plotLine(int x0, int y0, int x1, int y1)
  8.     dx =  abs(x1-x0);
  9.     sx = x0<x1 ? 1 : -1;
  10.     dy = -abs(y1-y0);
  11.     sy = y0<y1 ? 1 : -1;
  12.     err = dx+dy;  //error value e_xy
  13.     while (true)   //loop
  14.         plot(x0, y0);
  15.         if (x0==x1 && y0==y1) break;
  16.         e2 = 2*err;
  17.         if (e2 >= dy)
  18.             err += dy; // e_xy+e_x > 0
  19.             x0 += sx;
  20.         end if
  21.         if (e2 <= dx) // e_xy+e_y < 0
  22.             err += dx;
  23.             y0 += sy;
  24.         end if
  25.     end while
  26.  
  27. */
  28.  
  29.     int32_t dx =  abs(x1-x0);
  30.     int32_t sx = x0<x1 ? 1 : -1;
  31.     int32_t dy = -abs(y1-y0);
  32.     int32_t sy = y0<y1 ? 1 : -1;
  33.     int32_t err = dx+dy;
  34.  
  35.     while(1)
  36.     {
  37.         PlotPoint( x0, y0 );
  38.  
  39.         if (x0==x1 && y0==y1) break;
  40.         int32_t e2 = 2 * err;
  41.         if ( e2 >= dy )
  42.         {
  43.             err += dy;
  44.             x0 += sx;
  45.         }
  46.         if (e2 <= dx)
  47.         {
  48.             err += dx;
  49.             y0 += sy;
  50.         }
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment