def test_point_in_triangle(p, p0, p1, p2):
    """Test if a point lies in a triangle and return barycentric coordinates."""
    u = p0[1] * p2[0] - p0[0] * p2[1] + (p2[1] - p0[1]) * p[0] + (p0[0] - p2[0]) * p[1]
    v = p0[0] * p1[1] - p0[1] * p1[0] + (p0[1] - p1[1]) * p[0] + (p1[0] - p0[0]) * p[1]

    if (u < 0) != (v < 0):
        return False, 0.0, 0.0, 0.0

    a = -p1[1] * p2[0] + p0[1] * (p2[0] - p1[0]) + p0[0] * (p1[1] - p2[1]) + p1[0] * p2[1]

    if a < 0.0:
        u = -u
        v = -v
        a = -a

    result = u > 0 and v > 0 and (u + v) <= a
    w = 1.0 - u - v

    return result, u, v, w