Sherlock and Planes

  • + 0 comments

    python3

    def vector_sub(a,b):
        return list(map(lambda x,y: x-y, a, b)) 
    
    def cross(a,b):
        return [a[1]*b[2] - a[2]*b[1],
                a[2]*b[0] - a[0]*b[2],
                a[0]*b[1] - a[1]*b[0]]
    
    def dot(a,b):
        return sum([x*y for x,y in zip(a,b)])
    
    def solve(points):
        # use the 4 points p,q,r,s to make vectors pq,pr,ps 
        pq = vector_sub(points[1], points[0])
        pr = vector_sub(points[2], points[0])
        ps = vector_sub(points[3], points[0]) 
        # cross product of pq and pr gives the normal vector
        n  = cross(pq, pr)
        # if the vector ps is orthoginal to n 
        # then the point lies on the plane
        if dot(n, ps) != 0: 
            return 'NO' 
        return 'YES'