Task

Using the data storage type defined on the Bitmap page for raster graphics images,

draw a line given two points with Bresenham's line algorithm.




void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end);
 
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
{
    int x = int(start.x);
    int y = int(start.y);
 
    int dx = abs(x - int(end.x));
    int dy = abs(y - int(end.y));
 
    int sx = start.x < end.x ? 1 : -1;
    int sy = start.y < end.y ? 1 : -1;
 
    int err = (dx > dy ? dx : -dy) / 2;
 
    while (true)
    {
        targetTexture.write(float4(1.0), uint2(x, y));
 
        if (x == int(end.x) && y == int(end.y))
        {
            break;
        }
 
        int e2 = err;
 
        if (e2 > -dx)
        {
            err -= dy;
            x += sx;
        }
 
        if (e2 < dy)
        {
            err += dx;
            y += sy;
        }
    }
}