Scan pixels in a snake pattern along the x-coordinate then y-coordinate

    :param xi: Initial x-coordinate
    :type xi: int
    :param yi: Initial y-coordinate
    :type yi: int
    :param xf: Final x-coordinate
    :type xf: int
    :param yf: Final y-coordinate
    :type yf: int
    :returns: Coordinate generator
    :rtype: function

def snakescan(xi, yi, xf, yf):
    """Scan pixels in a snake pattern along the x-coordinate then y-coordinate

    :param xi: Initial x-coordinate
    :type xi: int
    :param yi: Initial y-coordinate
    :type yi: int
    :param xf: Final x-coordinate
    :type xf: int
    :param yf: Final y-coordinate
    :type yf: int
    :returns: Coordinate generator
    :rtype: function
    """

    # Determine direction to move
    dx = 1 if xf >= xi else -1
    dy = 1 if yf >= yi else -1

    # Scan pixels first along x-coordinate then y-coordinate and flip
    # x-direction when the end of the line is reached
    x, xa, xb = xi, xi, xf
    for y in range(yi, yf + dy, dy):
        for x in range(xa, xb + dx, dx):
            yield x, y

        # Swap x-direction
        if x == xa or x == xb:
            dx *= -1
            xa, xb = xb, xa