Adriaan van Os wrote:
Frank Heckenbach wrote:
Kevan Hashemi wrote:
My second use of round() is in drawing lines. But in this case, it is easy to implement an incremental rounding function that adds or subtracts one from an integer as its real-valued cousin rises or falls.
Sounds quite like the standard Bresenham algorithm which works completely with integers.
Yes, but it quite depends on the application whether the Bressenham line drawing algorithm is precise enough. If the "source" coordinates are floating point, you can round the start- and end-coordinates to integer values and then draw a line with the Bressenham algorithm. However, some intermediate pixels will invariably be one pixel off, because the rounding of the start- and end-coordinates also influences the "weight" and rounding of intermediate pixels.
AutoCAD, for example, does this wrong and thus draws ugly lines. It becomes even worse when circles are approximated by polygons.
It would be better to use the Bresenham algorithm for circles, of course. :-)
I have been thinking about an improved Bressenham line drawing algorithm that eliminates this problem, but I never had the time to write one, maybe it already exists, it would be interesting.
Bresenham effectively uses rationals internally. I think you could round to them (i.e., multiply by the denominator, then round to an integer). There are some tricky parts (such as finding a good denominator -- with integer coordinates it's obvious, with floating-point coordinates it's more difficult, but I think it should be possible). I haven't implemented that myself, though ...
Frank