Return at least one point in a list where segments intersect if an
       intersection exists. Otherwise, return an empty list.
    >>> get_segments_intersections(LineSegment(Point(0,0), Point(1,0)), \
                                   LineSegment(Point(0,0), Point(1,0)))
    [Point(0,0)]

def get_segments_intersections(segment1, segment2):
    """Return at least one point in a list where segments intersect if an
       intersection exists. Otherwise, return an empty list.
    >>> get_segments_intersections(LineSegment(Point(0,0), Point(1,0)), \
                                   LineSegment(Point(0,0), Point(1,0)))
    [Point(0,0)]
    """
    dx1 = segment1.p2.x - segment1.p1.x
    dy1 = segment1.p2.y - segment1.p1.y
    dx2 = segment2.p2.x - segment2.p1.x
    dy2 = segment2.p2.y - segment2.p1.y
    delta = dx2 * dy1 - dy2 * dx1
    if delta == 0:  # parallel segments
        # Line segments could be (partially) identical.
        # In that case this functin should return True.
        if dx1 == 0 and dy1 == 0:  # segment1 is a point
            point = segment1.p1
            if segment2.p1.x == point.x and segment2.p1.y == point.y:
                return [Point(point.x, point.y)]
            elif segment2.p2.x == point.x and segment2.p2.y == point.y:
                return [Point(point.x, point.y)]
            else:
                return []
        elif dx2 == 0 and dy2 == 0:  # segment2 is a point
            point = segment2.p1
            if segment1.p1.x == point.x and segment1.p1.y == point.y:
                return [Point(point.x, point.y)]
            elif segment1.p2.x == point.x and segment1.p2.y == point.y:
                return [Point(point.x, point.y)]
            else:
                return []
        elif dx1 == 0:
            # Lines segments are vertical
            if segment1.p1.x == segment2.p1.x:
                if segment1.p1.y > segment1.p2.y:
                    segment1.p1, segment1.p2 = segment1.p2, segment1.p1
                if segment2.p1.y > segment2.p2.y:
                    segment2.p1, segment2.p2 = segment2.p2, segment2.p1
                # Lines segments are on the same line
                if segment1.p1.y <= segment2.p1.y <= segment1.p2.y:
                    return [Point(segment1.p1.x, segment2.p1.y)]
                if segment2.p1.y <= segment1.p1.y <= segment2.p2.y:
                    return [Point(segment1.p1.x, segment1.p1.y)]
        else:
            # The equation f(x) = m*x + t defines any non-vertical line
            t1 = segment1.get_offset()
            t2 = segment2.get_offset()
            if t1 == t2:  # line segments are on the same line
                if segment1.p1.x <= segment2.p1.x <= segment1.p2.x:
                    return [Point(segment2.p1.x,
                                  segment2.get_slope()*segment2.p1.x+t2)]
                if segment2.p1.x <= segment1.p1.x <= segment2.p2.x:
                    return [Point(segment1.p1.x,
                                  segment1.get_slope()*segment1.p1.x+t1)]
        return []
    if dx2 == 0:  # Line 2 is a vertical line, but line 1 isn't
        segment1, segment2 = segment2, segment1
        dx1, dx2 = dx2, dx1
    if dx1 == 0:  # Line 1 is a vertical line, but line 2 isn't
        if segment2.p1.x > segment2.p2.x:
            segment2.p1, segment2.p2 = segment2.p2, segment2.p1
        if segment2.p1.x <= segment1.p1.x <= segment2.p2.x:
            # The x-values overlap
            m2 = segment2.get_slope()
            t2 = segment2.get_offset()
            y = m2*segment1.p1.x + t2
            if segment1.p1.y > segment1.p2.y:
                segment1.p1, segment1.p2 = segment1.p2, segment1.p1
            if segment1.p1.y <= y <= segment1.p2.y:
                return [Point(segment1.p1.x, y)]
            else:
                return []
        else:
            return []

    m1, t1 = segment1.get_slope(), segment1.get_offset()
    m2, t2 = segment2.get_slope(), segment2.get_offset()
    try:
        x = (t2-t1)/(m1-m2)
    except Exception as inst:
        logging.debug(inst)
        logging.debug("m1=%s", repr(m1))
        logging.debug("m2=%s", repr(m2))
        return []
    if segment1.p1.x > segment1.p2.x:
        segment1.p1, segment1.p2 = segment1.p2, segment1.p1
    if segment2.p1.x > segment2.p2.x:
        segment2.p1, segment2.p2 = segment2.p2, segment2.p1
    if (segment1.p1.x <= x <= segment1.p2.x) and \
       (segment2.p1.x <= x <= segment2.p2.x):
        # The intersection is on both line segments - not only on the lines
        return [Point(x, m1*x+t1)]
    else:
        return []