Bresenham's algorithm is used to draw a line on a square grid as are pixels for example.
The algorithm is partly based on the subdivision of the plane into 8 parts called octants.
The trick is to use symmetries to generalize the algorithm regardless of where the second point is located: firstly we "move" it to the first octant, then the calculations are made, and finally the generated points are converted back to their original octant.
Wikipedia provides a basic function to perform the trick.
function switchToOctantZeroFrom(octant, x, y)
switch(octant)
case 1: return (x, y)
case 2: return (y, x)
case 3: return (y, -x)
case 4: return (-x, y)
case 5: return (-x, -y)
case 6: return (-y, -x)
case 7: return (-y, x)
case 8: return (x, -y)
Moreover, it is written that we just have to:
flip the co-ordinate system on the input and output
This is based on the fact that these transpositions are in fact involutions: f(f(x)) = x
Without paying much attention to it, I first thought it would work.
But for cases 3 and 7, it does not work because it is not an involution.
For example:
Case 4: (-5, 1) => (5, 1) => (-5, 1) // Good
Case 3: (-1, 5) => (5, 1) => (1, -5) // Not good
We have to do the trick once again:
Case 3: (-1, 5) => (5, 1) => (1, -5) => (-5, -1) => (-1, 5) // Good
So, did I misunderstand something?
Or is it in fact a lack of precision in the drafting of the article on Wikipedia and should someone improve it?
Is there not a better way to make these transitions without that I need to use two functions switchToOctant_onInput
and switchToOctant_onOutput
(the obvious solution to this problem that I see now)?