Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

A Point in a Triangle

There is a triangle formed by three points $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$ on a plain.

Write a program which prints "YES" if a point $P$ $(x_p, y_p)$ is in the triangle and "NO" if not.

Input

Input consists of several datasets. Each dataset consists of:

$x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ $x_p$ $y_p$

All the input are real numbers. Input ends with EOF. The number of datasets is less than or equal to 100.

Constraints

You can assume that:

$ -100 \leq x_1, y_1, x_2, y_2, x_3, y_3, x_p, y_p \leq 100$

1.0 $\leq$ Length of each side of a tringle

0.001 $\leq$ Distance between $P$ and each side of a triangle

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

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ
{
  class Point
  {
    public double X { get; private set; }
    public double Y { get; private set; }

    public Point(double x, double y)
    {
      X = x;
      Y = y;
    }

    public static Point operator+ (Point p1, Point p2)
    {
      return new Point(p1.X + p2.X, p1.Y + p2.Y);
    }

    public static Point operator- (Point p1, Point p2)
    {
      return new Point(p1.X - p2.X, p1.Y - p2.Y);
    }
  }

  class Program
  {
    public static void Main()
    {
      foreach (var points in ReadPoints())
      {
        if (IsInTriangle(points[0], points[1], points[2], points[3]))
          Console.WriteLine("YES");
        else
          Console.WriteLine("NO");
      }
    }

    static IEnumerable<Point[]> ReadPoints()
    {
      string line;
      while ((line = Console.ReadLine()) != null)
        yield return line.Split(' ').Select((s, i) => new { Index = i, Value = double.Parse(s) })
                                    .GroupBy(x => x.Index / 2)
                                    .Select(x => x.Select(v => v.Value))
                                    .Select(x => new Point(x.First(), x.Skip(1).First()))
                                    .ToArray();
    }

    static bool IsInTriangle(Point a, Point b, Point c, Point p)
    {
      var center = new Point((a.X + b.X + c.X) / 3.0, (a.Y + b.Y + c.Y) / 3.0);
      if (IntersectionLine(a, b, p, center)
          || IntersectionLine(a, c, p, center)
          || IntersectionLine(b, c, p, center)) return false;
      return true;
    }

    static bool IntersectionLine(Point a1, Point a2, Point b1, Point b2)
    {
      return ((a1.X - a2.X) * (b1.Y - a1.Y) + (a1.Y - a2.Y) * (a1.X - b1.X))
        * ((a1.X - a2.X) * (b2.Y - a1.Y) + (a1.Y - a2.Y) * (a1.X - b2.X)) < 0;
    }
  }
}