Rotates and flips a quadrant appropriately for the Hilbert scan
    generator. See https://en.wikipedia.org/wiki/Hilbert_curve.

def hilbertrot(n, x, y, rx, ry):
    """Rotates and flips a quadrant appropriately for the Hilbert scan
    generator. See https://en.wikipedia.org/wiki/Hilbert_curve.
    """
    if ry == 0:
        if rx == 1:
            x = n - 1 - x
            y = n - 1 - y
        return y, x
    return x, y