Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		input := strings.Split(scanner.Text(), " ")
		buf := make([]float64, len(input))
		for j := range buf {
			buf[j], _ = strconv.ParseFloat(input[j], 64)
		}
		p1 := complex(buf[0], buf[1])
		p2 := complex(buf[2], buf[3])
		p3 := complex(buf[4], buf[5])
		pk := complex(buf[6], buf[7])
		ps := complex(buf[8], buf[9])
		kin := isAtSameSide(p3, pk, []complex128{p1, p2}) &&
			isAtSameSide(p1, pk, []complex128{p2, p3}) &&
			isAtSameSide(p2, pk, []complex128{p3, p1})
		sin := isAtSameSide(p3, ps, []complex128{p1, p2}) &&
			isAtSameSide(p1, ps, []complex128{p2, p3}) &&
			isAtSameSide(p2, ps, []complex128{p3, p1})
		if (kin || sin) && !(kin && sin) {
			fmt.Println("OK")
		} else {
			fmt.Println("NG")
		}
	}
}

func isAtSameSide(a, b complex128, line []complex128) bool {
	sa := (real(line[1]) - real(line[0]))*(imag(a)-imag(line[0]))+
		(imag(line[1])-imag(line[0]))*(real(line[0])-real(a))
	sb := (real(line[1]) - real(line[0]))*(imag(b)-imag(line[0]))+
		(imag(line[1])-imag(line[0]))*(real(line[0])-real(b))
	return (sa > 0 && sb > 0) || (sa < 0 && sb < 0)
}

C++: #include <iostream>
#include <complex>
using namespace std;

typedef complex<double> P;

#define EPS (1e-10)

double cross(P a,P b){
	return (a.real() * b.imag() - a.imag() * b.real());
}

int is_intersected_ls(P a1,P a2,P b1,P b2){
	return ((cross(a2-a1,b1-a1) * cross(a2-a1,b2-a1) < EPS) &&
					(cross(b2-b1,a1-b1) * cross(b2-b1,a2-b1) < EPS));
}

int main(void){
	int n;
	cin>>n;

	while(n--){
		P p[3],k,s;

		for(int i=0;i<3;i++){
			cin>>p[i].real()>>p[i].imag();
		}
		cin>>k.real()>>k.imag();
		cin>>s.real()>>s.imag();

		int count = 0;
		count += is_intersected_ls(k,s,p[0],p[1]);
		count += is_intersected_ls(k,s,p[1],p[2]);
		count += is_intersected_ls(k,s,p[2],p[0]);

		if(count == 1){
			cout<<"OK\n";
		}
		else{
			cout<<"NG\n";
		}
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int xp1, yp1, xp2, yp2, xp3, yp3, xk, yk, xs, ys;

bool side(int a, int b, int c, int d, int x, int y) {
  if (a == c)
    return x-a > 0;
  double p = (d - b) / (double)(c - a), q = b - p * a;
  return y > p * x + q;
}

bool inside_triangle(int x, int y) {
  return
    side(xp1, yp1, xp2, yp2, x, y) == side(xp1, yp1, xp2, yp2, xp3, yp3)
    && side(xp1, yp1, xp3, yp3, x, y) == side(xp1, yp1, xp3, yp3, xp2, yp2)
    && side(xp3, yp3, xp2, yp2, x, y) == side(xp3, yp3, xp2, yp2, xp1, yp1);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> xp1 >> yp1 >> xp2 >> yp2 >> xp3 >> yp3 >> xk >> yk >> xs >> ys;
    if (inside_triangle(xk, yk) ^ inside_triangle(xs, ys))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

C++: #include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;

double EPS = 1e-10;

double add(double a, double b){
  if(abs(a+b) < EPS * (abs(a)+abs(b)))return 0;
  return a+b;
}

struct point{
  double x, y;
  point(){}
  point(double x,double y) : x(x) , y(y){}

  point operator + (point p){
    return point(add(x,p.x), add(y,p.y));
  }

  point operator - (point p){
    return point(add(x,-p.x), add(y,-p.y));
  }

  point operator * (double d){
    return point(x*d,y*d);
  }
};

double cross(point a, point b) {
  return (a.x * b.y - a.y * b.x);
}

int is_intersected_ls(point a1, point a2, point b1, point b2) {
  return ( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS ) &&
         ( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS );
}

int main(void){
  int n;
  point p[3],k,s;

  cin >> n;
  while(n--){
    for(int i=0;i<3;i++)
      cin >> p[i].x >> p[i].y;
    
    cin >> k.x >> k.y >> s.x >> s.y;
    int cnt=0;
    for(int i=0;i<3;i++)
      for(int j=i+1;j<3;j++)
	  cnt+=is_intersected_ls(p[i],p[j],k,s);
    
    if(cnt%2)cout << "OK" << endl;
    else cout << "NG" << endl;
    
  }

  return 0;
}

C++: #include <cstdio>

bool line(int x1, int y1, int x2, int y2, int x, int y){
	return (x2 - x1) * (y - y1) > (y2 - y1) * (x - x1);
}

bool triangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y){
	return	line(x1, y1, x2, y2, x, y) == line(x1, y1, x2, y2, x3, y3) &&
			line(x2, y2, x3, y3, x, y) == line(x2, y2, x3, y3, x1, y1) &&
			line(x3, y3, x1, y1, x, y) == line(x3, y3, x1, y1, x2, y2);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		int x1, y1, x2, y2, x3, y3, ax, ay, bx, by;
		scanf("%d%d%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &ax, &ay, &bx, &by);
		if(triangle(x1, y1, x2, y2, x3, y3, ax, ay) != triangle(x1, y1, x2, y2, x3, y3, bx, by)){
			printf("OK\n");
		}
		else{
			printf("NG\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

typedef double D; 
#define EPS (1e-5)
#define equal(a,b) (fabs((a)-(b)) < EPS)

D add(D a,D b){
  if(abs(a + b) < EPS * (abs(a) + abs(b))){
    return 0;
  }
  return a + b;
}

struct Point{
  D x,y;

  Point (D x = 0, D y = 0) : x(x),y(y) {}
  
  Point operator + (Point p) {
    return Point(add(x,p.x) , add(y,p.y));
  }
  Point operator - (Point p) {
    return Point(add(x,p.x) ,add(y,-p.y));
  }
  Point operator * (D a) {
    return Point( a*x , a*y );
  }
  
  D norm() {
    return x*x+y*y;
  }

  D dot(Point p){
    return add(x*p.x , y*p.y);
  }

  D det(Point p){
    return add(x*p.y , -y*p.x);
  }  
};

int ccw(Point p0,Point p1,Point p2){
  Point a = p1 - p0;
  Point b = p2 - p0;

  if(a.det(b) > EPS) return 1;
  if(a.det(b) < -EPS) return -1;
  if(a.dot(b) < -EPS) return 2;
  if(a.norm() < b.norm()) return -2;
  return 0;
}

D S(Point p1,Point p2,Point p3){
  D a = sqrt(pow(p1.x-p2.x,2) + pow(p1.y-p2.y,2));
  D b = sqrt(pow(p1.x-p3.x,2) + pow(p1.y-p3.y,2));
  D c = sqrt(pow(p3.x-p2.x,2) + pow(p3.y-p2.y,2));
  D s = (a + b + c) / 2;

  return sqrt(s*(s - a)*(s - b)*(s - c));
}

bool check(Point p1,Point p2,Point p3,Point p4){
  return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 &&
	  ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0);
}

bool eq(D S1,D S2){
  return ((float)S1 == (float)S2);
}

int main(){
  int n;
  Point P[3],T[2];

  cin >> n;
  while(n--){
    scanf("%lf %lf" ,&P[0].x ,&P[0].y);
    scanf("%lf %lf" ,&P[1].x ,&P[1].y);
    scanf("%lf %lf" ,&P[2].x ,&P[2].y);
    scanf("%lf %lf" ,&T[0].x ,&T[0].y);
    scanf("%lf %lf" ,&T[1].x ,&T[1].y);

    bool ok = false;
    D S1 = S(P[0],P[1],P[2]);
    D S2 = S(P[0],P[1],T[0])+S(P[0],P[2],T[0])+S(P[1],P[2],T[0]);
    D S3 = S(P[0],P[1],T[1])+S(P[0],P[2],T[1])+S(P[1],P[2],T[1]);

    if(check(P[0],P[1],T[0],T[1]) &&
       ((eq(S1,S2) && !eq(S1,S3)) || 
	(!eq(S1,S2) && eq(S1,S3)))){
      ok = true;
    }    
    if(check(P[0],P[2],T[0],T[1]) &&
       (eq(S1,S2) && !eq(S1,S3)) ||
       (!eq(S1,S2) && eq(S1,S3))){
      ok = true;
    }  
    if(check(P[1],P[2],T[0],T[1]) &&
       (eq(S1,S2) && !eq(S1,S3)) ||
       (!eq(S1,S2) && eq(S1,S3))){
      ok = true;
    }  

    if(ok){
      puts("OK");
    }else{
      puts("NG");
    }
  }

  return 0;
}