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.
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.
def get_octant(pA, pB): AX = pA AY = pA BX = pB BY = pB if BX > AX: if BY > AY: if (BY - AY) < (BX - AX): octant = 0 else: octant = 1 else: if (AY - BY) < (BX - AX): octant = 7 else: octant = 6 else: if BY > AY: if (BY - AY) < (AX - BX): octant = 3 else: octant = 2 else: if (AY - BY) < (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?