This is what I just realized will work. These are probably not the most beautiful interpolations, but these are just 1-2 float additions to iterate on the line with a one-time preliminary calculation. It works by calculating the number of steps on the Manhattan matrix.
Ah, and he has not yet caught the case where the line is vertical (dx = 0)
This is a naive brashenham, but iterations can theoretically only use integers. If you want to get rid of the color value of the float, things will get complicated, because the line may be longer than the color difference, so delta-color <1.
void Brepolate( uint8_t* pColorBuffer, uint8_t cs, float xs, float ys, float zs, uint8_t ce, float xe, float ye, float ze ) { float nColSteps = (xe - xs) + (ye - ys); float fColInc = ((float)cs - (float)ce) / nColSteps; float fCol = cs; float dx = xe - xs; float dy = ye - ys; float fCol = cs; if (dx > 0.5) { float de = fabs( dy / dx ); float re = de - 0.5f; uint32_t iY = ys; uint32_t iX; for ( uint32_t iX = xs; iX <= xe; iX++ ) { uint32_t off = surf.Offset( iX, iY ); pColorBuffer[off] = fCol; re += de; if (re >= 0.5f) { iY++; re -= 1.0f; fCol += fColInc; } fCol += fColInc; } } }
source share