RicardasSim icon

trying Bresenham's line algorithm

RicardasSim | PRO | 06/11/20 06:34:39 PM UTC | 0 ⭐ | 1317 👁️ | Never ⏰ | []
C |

1.1 KB

|

None

|

0 👍

/

0 👎

void LineTest( int32_t x0, int32_t y0, int32_t x1, int32_t y1 )
{
 
/*
https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
 
plotLine(int x0, int y0, int x1, int y1)
    dx =  abs(x1-x0);
    sx = x0<x1 ? 1 : -1;
    dy = -abs(y1-y0);
    sy = y0<y1 ? 1 : -1;
    err = dx+dy;  //error value e_xy
    while (true)   //loop
        plot(x0, y0);
        if (x0==x1 && y0==y1) break;
        e2 = 2*err;
        if (e2 >= dy) 
            err += dy; // e_xy+e_x > 0 
            x0 += sx;
        end if
        if (e2 <= dx) // e_xy+e_y < 0
            err += dx;
            y0 += sy;
        end if
    end while
 
*/
 
    int32_t dx =  abs(x1-x0);
    int32_t sx = x0<x1 ? 1 : -1;
    int32_t dy = -abs(y1-y0);
    int32_t sy = y0<y1 ? 1 : -1;
    int32_t err = dx+dy;
 
    while(1)
    {
        PlotPoint( x0, y0 );
 
        if (x0==x1 && y0==y1) break;
        int32_t e2 = 2 * err;
        if ( e2 >= dy )
        {
            err += dy;
            x0 += sx;
        }
        if (e2 <= dx)
        {
            err += dx;
            y0 += sy;
        }
    }
 
}

Comments