def keep_in_bound(pos, grid):
    """
    Function for truncating and bounding the random walk to within the defined grid
    
    @param pos: tuple of the point to be checked
    @param grid: the bounds for limiting the walk
    
    @return position tuple after bounding the point
    """
    pos[0] = max(pos[0], grid[0] - grid[2] / 2)
    pos[0] = min(pos[0], grid[0] + grid[2] / 2)
    pos[1] = max(pos[1], grid[1] - grid[3] / 2)
    pos[1] = min(pos[1], grid[1] + grid[3] / 2)
    return pos
