def calculate_intersection(p1, p2, q1, q2):
    """Return the 2D geometrical intersection of segments [p1, p2] and [q1, q2]
    
    inputs:
    - p1, p2: 2D points that define the first segment
    - q1, q2: 2D points that define the second segment
    """
    num_s = float((p1[0] - q1[0]) * (q1[1] - q2[1]) - (p1[1] - q1[1]) * (q1[0] - q2[0]))
    den_s = float((p1[0] - p2[0]) * (q1[1] - q2[1]) - (p1[1] - p2[1]) * (q1[0] - q2[0]))
    if (den_s == 0):
        # Intersection undefined
        return (float("nan"), float("nan"))

    s = num_s / den_s
    if (s < 0) or (s > 1.0):
        # No intersection
        return (float("nan"), float("nan"))

    # Point of intersection
    x = p1[0] + s * (p2[0] - p1[0])
    y = p1[1] + s * (p2[1] - p1[1])
    return (x,y)