Title: A Point in a Triangle

There is a triangle formed by three points (x1, y1), (x2, y2), (x3, y3) on a plane. Write a program that prints "YES" if a point P (xp, yp) is inside the triangle, and "NO" if not.

Input:
Input consists of several datasets, each containing x1, y1, x2, y2, x3, y3, xp, and yp. All inputs are real numbers. Input ends with EOF. The number of datasets is less than or equal to 100.

Constraints:
-100 ≤ x1, y1, x2, y2, x3, y3, xp, yp ≤ 100
Length of each side of a triangle ≥ 1.0
Distance between P and each side of a triangle ≥ 0.001

Output:
For each dataset, print "YES" or "NO" in a line.

Sample Input:
0.0 0.0 2.0 0.0 2.0 2.0 1.5 0.5
0.0 0.0 1.0 4.0 5.0 3.0 -1.0 3.0

Output for the Sample Input:
YES
NO

# Buggy version:
import sys

def crossMulti(p1, p2):
    return p1[0] * p2[1] - p1[1] * p2[0]
           
lines = sys.stdin.readlines()
for line in lines:
    Zd = []
    x1, y1, x2, y2, x3, y3, xp, yp = map(float, line.split())
    Ps = [(x2-x1, y2-y1),(x3-x2, y3-y2),(x1-x3, y1-y3)]
    Px = [(xp-x1, yp-y1),(xp-x2, yp-y2),(xp-x3, yp-y3)]
    for p in range(3):
        Zd.append(crossMulti(Ps[p], Px[p]))
    Zd = [x > 0 for x in Zd]

    if (Zd[0] and Zd[1] and Zd[2]) or not(Zd[0] or Zd[1] or Zd[2]):
        print('Yes')
    else:
        print('No')
    

# Fixed version:
import sys

def crossMulti(p1, p2):
    return p1[0] * p2[1] - p1[1] * p2[0]
           
lines = sys.stdin.readlines()
for line in lines:
    Zd = []
    x1, y1, x2, y2, x3, y3, xp, yp = map(float, line.split())
    Ps = [(x2-x1, y2-y1),(x3-x2, y3-y2),(x1-x3, y1-y3)]
    Px = [(xp-x1, yp-y1),(xp-x2, yp-y2),(xp-x3, yp-y3)]
    for p in range(3):
        Zd.append(crossMulti(Ps[p], Px[p]))
    Zd = [x > 0 for x in Zd]

    if (Zd[0] and Zd[1] and Zd[2]) or not(Zd[0] or Zd[1] or Zd[2]):
        print('YES')
    else:
        print('NO')

# Buggy version:
def cross_product(a, b):
    ax, ay = a
    bx, by = b
    return ax * by - ay * bx


def sub(a, b):
    a1, a2 = a
    b1, b2 = b
    return b1 - a1, b2 - a2


def judge(p1, p2, p3, pp):

    cnt = 0
    for a, b in ((p1, p2), (p2, p3), (p3, p1)):
        vec1 = sub(a, b)
        vec2 = sub(a, pp)
        if cross_product(vec2, vec1) >= 0:
            cnt += 1
    if cnt == 0 or cnt == 3:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    inputs = [float(s) for s in input().split()]
    p1 = (inputs.pop(0), inputs.pop(0))
    p2 = (inputs.pop(0), inputs.pop(0))
    p3 = (inputs.pop(0), inputs.pop(0))
    pp = (inputs.pop(0), inputs.pop(0))

    judge(p1, p2, p3, pp)


# Fixed version:
import sys

def cross_product(a, b):
    ax, ay = a
    bx, by = b
    return ax * by - ay * bx


def sub(a, b):
    a1, a2 = a
    b1, b2 = b
    return b1 - a1, b2 - a2


def judge(p1, p2, p3, pp):

    cnt = 0
    for a, b in ((p1, p2), (p2, p3), (p3, p1)):
        vec1 = sub(a, b)
        vec2 = sub(a, pp)
        if cross_product(vec2, vec1) > 0:
            cnt += 1
    if cnt == 0 or cnt == 3:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    for line in sys.stdin:
        inputs = [float(s) for s in line.split()]
        p1 = (inputs.pop(0), inputs.pop(0))
        p2 = (inputs.pop(0), inputs.pop(0))
        p3 = (inputs.pop(0), inputs.pop(0))
        pp = (inputs.pop(0), inputs.pop(0))
        judge(p1, p2, p3, pp)