def linesegmentsintersect(p1, p2, q1, q2):
    """Checks if two line segments intersect.

    Input:
        p1 : The start vertex of the first line segment.
        p2 : The end vertex of the first line segment.
        q1 : The start vertex of the second line segment.
        q2 : The end vertex of the second line segment.

    Output:
        True if the two line segments intersect
    """
    dx = p2[0] - p1[0]
    dy = p2[1] - p1[1]
    da = q2[0] - q1[0]
    db = q2[1] - q1[1]

    # segments are parallel
    if (da*dy - db*dx) == 0:
        return False

    s = (dx * (q1[1] - p1[1]) + dy * (p1[0] - q1[0])) / (da * dy - db * dx)
    t = (da * (p1[1] - q1[1]) + db * (q1[0] - p1[0])) / (db * dx - da * dy)

    return s >= 0 and s <= 1 and t >= 0 and t <= 1