aborrower
3/16/2016 - 6:28 AM

convert angle

convert angle

def conv_angle(angle, unit_in, unit_out):
    '''
    Convert angle between degree, radian, and turn
    This function uses: sympy (for pi and simplify)
    
    angle: angle number
    unit_in, unit_out: unit name degree / rad / turn
    
    returns: angle (converted to desired unit)
    '''
    from sympy import pi, simplify
    
    # from degree
    if unit_in == "degree":
        if unit_out == "degree":
            ans = angle
        if unit_out == "rad":
            ans = simplify(angle * pi / 180)
        if unit_out == "turn":
            ans = simplify(angle / 360)
    
    # from radian
    if unit_in == "rad":
        if unit_out == "degree":
            ans = simplify(angle * 180 * pi**(-1))
        if unit_out == "rad":
            ans = angle
        if unit_out == "turn":
            ans = simplify(angle * (2 * pi)**(-1))
    
    # from turn
    if unit_in == "turn":
        if unit_out == "degree":
            ans = angle * 360
        if unit_out == "rad":
            ans = angle * 2 * pi
        if unit_out == "turn":
            ans = angle
    
    return ans