Determining which octant has a specific point I am interested in octants in plane geometry, which basically involves dividing a plane into 8 equal parts according to a certain point.



Octants are particularly useful while implementing the Bresenham's algorithm.


Problem

Given two points A = (x1, y1) and B = (x2, y2), how to determine in which octant belongs B according to A?

Counting octants is performed as shown: starting from the upper right and iterating anticlockwise.

Naive implementation

def get_octant(pA, pB):
    AX = pA[0]
    AY = pA[1]
    BX = pB[0]
    BY = pB[1]

    if BX &gt; AX:
        if BY &gt; AY:
            if (BY - AY) &lt; (BX - AX):
                octant = 0
            else:
                octant = 1
        else:
            if (AY - BY) &lt; (BX - AX):
                octant = 7
            else:
                octant = 6
    else:
        if BY &gt; AY:
            if (BY - AY) &lt; (AX - BX):
                octant = 3
            else:
                octant = 2
        else:
            if (AY - BY) &lt; (AX - BX):
                octant = 4
            else:
                octant = 5

    return octant


Would it not possible to find a cleverer implementation?

Using the symmetry properties, I am convinced that it is possible to make a shorter and more readable code. Unfortunately, I do not find it myself, so does anyone have an idea, please?
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.