def setbox(x, y, mbox, xmax, ymax):
    """
    Create a box of length mbox around a position x,y. If the box will
    be out of [0,len] then reset the edges of the box to be within the
    boundaries.

    Parameters
    ----------
    x : int
        Central x-position of box.

    y : int
        Central y-position of box.

    mbox : int
        Width of box.

    xmax : int
        Maximum x value.

    ymax : int
        Maximum y value.

    Returns
    -------
    x1 : int
        Lower x corner of box.

    x2 : int
        Upper x corner of box.

    y1 : int
        Lower y corner of box.

    y2 : int
        Upper y corner of box.
    """
    mbox = max(int(0.5 * mbox), 1)
    y1 = max(0, y - mbox)
    y2 = min(y + mbox + 1, ymax - 1)
    x1 = max(0, x - mbox)
    x2 = min(x + mbox + 1, xmax - 1)
    return x1, x2, y1, y2
