import math


def get_normal(line, length=0):
    """Gets the normal of a line (flips it 90 degrees).
       Line is a pair of tuples -- ((x, y), (x, y)) -- representing the
       two ends of the line.

       If length is set to a value > 0, the normal is also set to that
       fixed length.
    """
    if type(line) not in [list, tuple]:
        line = (
            line[0][0], line[1][0]        
        )

    if length <= 0:
        return (
            (line[1][0], line[0][1]),
            (line[0][0], line[1][1])
        )
    half_length = length / 2
    center = (
        line[0][0] + ((line[1][0] - line[0][0]) / 2),
        line[0][1] + ((line[1][1] - line[0][1]) / 2)
    )
    diff = (
        line[1][0] - line[0][0],
        line[1][1] - line[0][1]
    )
    angle = math.degrees(math.atan2(diff[0], diff[1]))
    return (
        (center[0] - half_length * math.sin(angle),
         center[1] - half_length * math.cos(angle)),
        (center[0] + half_length * math.sin(angle),
         center[1] + half_length * math.cos(angle))
    )