def new_state(current_state, direction):
    """
    Calculates new heading from current heading and relative direction.
    """
    new_axis = (current_state['axis']+1) & 1
    multiplier = -1 if current_state['axis'] == 0 and direction == 1 or \
                       current_state['axis'] == 1 and direction == -1 else 1 
    new_sign = current_state['sign'] * multiplier
    return {'axis': new_axis, 'sign': new_sign}