def origin(current, start, end, cum_extents, screen, moving):
    """Determine new origin for screen view if necessary.

    The part of the DataFrame displayed on screen is conceptually a box which 
    has the same dimensions as the screen and hovers over the contents of the 
    DataFrame. The origin of the relative coordinate system of the box is 
    calculated here.

    >>> origin(0, 0, 0, [0, 4, 8, 12], 7, True)
    0
    >>> origin(4, 0, 2, [0, 4, 8, 12], 7, True)
    5
    >>> origin(5, 1, 1, [0, 4, 8, 12], 7, False)
    4

    :param current: current origin of a given axis
    :type current: int
    :param start: leftmost column index or topmost row index selected
    :type start: int
    :param end: rightmost column index or bottommost row index selected
    :type end: int
    :param cum_extents: cumulative sum of column widths or row heights
    :type cum_extents: numpy.ndarray
    :param screen: total extent of a given axis
    :type screen: int
    :param moving: flag if current action is advancing
    :type: bool
    :returns: new origin
    :rtype: int
    """
    # Convert indices to coordinates of boundaries
    start = cum_extents[start]
    end = cum_extents[end+1]
    if end > current + screen and moving:
        return end - screen
    elif start < current and not moving:
        return start
    else:
        return current