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 JAVA (judge verdict: Accepted)

public class Main{
  public void run(java.io.InputStream in, java.io.PrintStream out){
    java.util.Scanner sc = new java.util.Scanner(in);
/*answer*/
    int n, q;
    double[] x, y;
    int i, j, countr;
    double x0, y0, ax, ay, bx, by, tmp;

    n = 3;
    x = new double[n + 1];
    y = new double[n + 1];

    for(;sc.hasNext();){
      for(i = 0;i < n;i++){
        x[i] = sc.nextDouble();
        y[i] = sc.nextDouble();
      }
      x[n] = x[0];
      y[n] = y[0];

      x0 = sc.nextDouble();
      y0 = sc.nextDouble();
      countr = 0;

      for(j = 0;j < n;j++){
        if((x[j] - x0) * (y[j + 1] - y0) - (y[j] - y0) * (x[j + 1] - x0) == 0){
          if((x[j] - x0) * (x[j + 1] - x0) <= 0 && (y[j] - y0) * (y[j + 1] - y0) <= 0){
            break;
          }else if(x0 < x[j + 1] && x0 < x[j] && y0 == y[j] && y0 == y[j + 1]){
            if((y[(j - 1 + n) % n] - y0) * (y[(j + 2) % n] - y0) > 0){
              countr++;
            }
          }
        }

        if((y[j] - y0) * (y[j + 1] - y0) <= 0 && y0 != y[j + 1]){
          ax = x[j];
          ay = y[j];
          bx = x[j + 1];
          by = y[j + 1];

          if(ay > by){
            tmp = ay;
            ay = by;
            by = tmp;
            tmp = ax;
            ax = bx;
            bx = tmp;
          }
          if((ax - x0) * (by - y0) - (bx - x0) * (ay - y0) > 0){
            countr++;
            if((y[(j - 1 + n) % n] - y0) * (y[j + 1] - y0) > 0 && y[j] == y0){
              countr++;
            }
          }
        }
      }

      if(j < n){
        System.out.println("NO");
      }else{
        if(countr % 2 == 1){
          System.out.println("YES");
        }else{
          System.out.println("NO");
        }
      }
    }

    sc.close();
  }
  public static void main(String[] args){
    (new Main()).run(System.in, System.out);
  }
}