Bresenham's Line Algorithm
(also what's used by GRBL to avoid floating point calculations in straight line interpolation) https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
|Slope| <= 1
Goal: draw a straight line pixel-by-pixel from (x0, y0)
to (x1, y1)
where slope <= 1. The ideal line is:
Of course, since we can only increment x
and y
by interger values, we can't fit the ideal line. When slope <= 1, each time we increment x
by 1, we decide whether we increment y
by 1 depending on:
Step 1: determine which axis to increment
dx = x1 - x0
dy = y1 - y0
xsign = 1 if dx > 0 else -1
ysign = 1 if dy > 0 else -1
dx = abs(dx)
dy = abs(dy)
if dx > dy:
# increment x
else:
# increment y