I'm having the same issue, Bresenham's algorithm is drawing a nice line, but I just can't pick any point without iterating through the coordinates, because of its nature, to avoid the rounding and other "glitches" it decides to place a pixel on-the-fly, which is very handy during rendering, but it makes geometric calculations quite expensive (line crossing/intersection finding for example).
I can't use floating point types either, because I'm developing to an MCU right now which doesn't have FPU, but the following code from my project works quite well, very fast, and I haven't noticed any faults yet.
inline static int16_t GetYOfLineAtX(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x) {
return (y1 - y0) * (x - x0) / (x1 - x0) + y0;
}
inline static int16_t GetXOfLineAtY(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t y) {
return (x1 - x0) * (y - y0) / (y1 - y0) + x0;
}
It is straightforward: x0, y0
is your line starting point, x1, y1
is the ending point (slope may be negative or positive, direction doesn't matter), the x
or y
parameter on the two functions are the required coordinate, it returns the other axis value.