3 ms·
>It can be done entirely with integer arithmetic. It's true when first and last points are within or not too far from the screen/clip, but if wanting to draw a
by jffhn 4y ago
>It can be done entirely with integer arithmetic.
It's true when first and last points are within or not too far from the screen/clip, but if wanting to draw a long line (if coords are 32 bits ints) that's mostly out of it and don't want clipping to introduce half a pixel size errors/inconsistencies, unless running Bresenham for a long time out of the clip, floating points are a more accurate tool to do the clipping and the drawing.
See for example:
https://github.com/jeffhain/jolikit/blob/master/src/main/java/net/jolikit/bwd/impl/utils/gprim/DefaultClippedLineDrawer.java https://github.com/jeffhain/jolikit/blob/master/src/main/jav...
[edit: it might actually be possible to jump to the clipped area while staying in integers and not loose accuracy, but I don't recall why I didn't try that]
- zozbot234 4y agoThe drawing actually needs arbitrarily high precision in the worst case. Consider a nearly horizontal line segment spanning the whole viewport where the endpoints' y coordinates are arbitrarily close to the boundary line between two pixels, on either side. You've got to decide (1) where to put the endpoints, and (2) where to split the line segment if the endpoints end up being placed at differently rounded y coordinates. Both steps might require high precision, and for either of them it might be computationally infeasible to get an exact result. (The latter step depends on a ratio of arbitrarily small subpixel deltas, and bounding such a ratio exactly is what might be infeasible.)