def hat(segment, position):
    """
    This function returns 0 when ``position` is the start or end of ``segment``
    and 1 when ``position`` is in the middle of the segment.

           /\
        __/  \__
    """
    h = abs((segment.midpoint - position) / segment.length)
    return max(0, 1 - h)