RicardasSim icon

trying C from pseudocode

RicardasSim | PRO | 06/11/20 06:54:43 PM UTC | 0 ⭐ | 1455 👁️ | Never ⏰ | []
C |

1.09 KB

|

None

|

0 👍

/

0 👎

/*
https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
 
function line(x0, y0, x1, y1)
    real deltax := x1 - x0
    real deltay := y1 - y0
    real deltaerr := abs(deltay / deltax)    // Assume deltax != 0 (line is not vertical),
          // note that this division needs to be done in a way that preserves the fractional part
    real error := 0.0 // No error at start
    int y := y0
    for x from x0 to x1 
        plot(x, y)
        error := error + deltaerr
        if error ≥ 0.5 then
            y := y + sign(deltay) * 1
            error := error - 1.0
*/
 
 
    // int32_t x0, int32_t y0, int32_t x1, int32_t y1
 
    float deltax = x1 - x0;
    float deltay = y1 - y0;
    float deltaerr = abs( deltay / deltax );
    float error = 0.0f;
    int32_t y = y0;
 
    for ( int32_t x = x0; x < x1; ++x )
    {
 
        PlotPoint( x, y );
 
        //error = error + deltaerr;
        error += deltaerr;
        
        if ( error >= 0.5f )
        {
            y += deltay < 0 ? (-1) : 1;
 
            //error = error - 1.0f;
            error -= 1.0f;
        }
    }

Comments