Determining which octant has a specific point
I am convinced that it is possible to make a shorter and more readable code. 


To implement the discrete analog of atan2((B.y - A.y), (B.x - A.x)):

x, y  = (B.x - A.x), (B.y - A.y)
octant = [([1, 2], [8, 7]), ([4, 3], [5, 6])][x &lt; 0][y &lt; 0][abs(x) &lt; abs(y)]


It is shorter, I don't know about readability. It is easy to write tests that check all possible variants including the behavior on the boundaries.