def on_segment(p, q, r):
    """Returns True if the point q (x,y) lies on the line between p and r. """
    return q[0] <= max(p[0], r[0]) and q[0] >= min(p[0], r[0]) and q[1] <= max(
        p[1], r[1]) and q[1] >= min(p[1], r[1])
