RicardasSim

trying C from pseudocode

Jun 11th, 2020
1,495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. /*
  2. https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
  3.  
  4. function line(x0, y0, x1, y1)
  5.     real deltax := x1 - x0
  6.     real deltay := y1 - y0
  7.     real deltaerr := abs(deltay / deltax)    // Assume deltax != 0 (line is not vertical),
  8.           // note that this division needs to be done in a way that preserves the fractional part
  9.     real error := 0.0 // No error at start
  10.     int y := y0
  11.     for x from x0 to x1
  12.         plot(x, y)
  13.         error := error + deltaerr
  14.         if error ≥ 0.5 then
  15.             y := y + sign(deltay) * 1
  16.             error := error - 1.0
  17. */
  18.  
  19.  
  20.     // int32_t x0, int32_t y0, int32_t x1, int32_t y1
  21.  
  22.     float deltax = x1 - x0;
  23.     float deltay = y1 - y0;
  24.     float deltaerr = abs( deltay / deltax );
  25.     float error = 0.0f;
  26.     int32_t y = y0;
  27.  
  28.     for ( int32_t x = x0; x < x1; ++x )
  29.     {
  30.  
  31.         PlotPoint( x, y );
  32.  
  33.         //error = error + deltaerr;
  34.         error += deltaerr;
  35.        
  36.         if ( error >= 0.5f )
  37.         {
  38.             y += deltay < 0 ? (-1) : 1;
  39.  
  40.             //error = error - 1.0f;
  41.             error -= 1.0f;
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment