"""Lead-pursuit intercept time. scratch, check the discriminant handling."""
import math

def intercept_time(p_t, v_t, speed_o):
    """p_t: target pos rel to us (m). v_t: target vel (m/s). speed_o: our speed."""
    a = v_t[0]**2 + v_t[1]**2 - speed_o**2
    b = 2 * (p_t[0]*v_t[0] + p_t[1]*v_t[1])
    c = p_t[0]**2 + p_t[1]**2
    if abs(a) < 1e-6:
        return -c / b if b else None
    disc = b*b - 4*a*c
    if disc < 0:
        return None  # uncatchable
    t1 = (-b + math.sqrt(disc)) / (2*a)
    t2 = (-b - math.sqrt(disc)) / (2*a)
    ts = [t for t in (t1, t2) if t > 0]
    return min(ts) if ts else None

if __name__ == "__main__":
    # contact 2 km out, crossing left-to-right at ~13 m/s (25 kt), us 21.6 m/s (42 kt)
    print(intercept_time((2000, 500), (13.0, 0.0), 21.6), "s")
