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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

typedef struct POINT_tag{
  int x, y;
}POINT_t;

typedef struct LINE_tag{
  POINT_t a, b;
}LINE_t;

typedef struct TRIANGLE_tag{
  POINT_t a, b, c;
}TRIANGLE_t;

int side(POINT_t *p, LINE_t *e){
  POINT_t p1 = *p;
  POINT_t p2 = e->a;
  POINT_t p3 = e->b;

  const int n = p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y);
  if(n > 0) return 1;
  else if(n > 0) return -1;
  else return 0;
}

int solve(POINT_t *p, TRIANGLE_t *t){
  LINE_t ab, bc, ca;
  ab.a = t->a; ab.b = t->b;
  bc.a = t->b; bc.b = t->c;
  ca.a = t->c; ca.b = t->a;
  const int pab = side(p, &ab);
  const int pbc = side(p, &bc);
  const int pca = side(p, &ca);

  if((0 < pab) && (0 < pbc) && (0 < pca)) return 1;
  if((0 > pab) && (0 > pbc) && (0 > pca)) return 1;
  if((0 <= pab) && (0 <= pbc) && (0 <= pca)) return -1;
  if((0 <= pab) && (0 >= pbc) && (0 >= pca)) return -1;
  return 0;
}

int main(){
  int n;
  scanf("%d", &n);
  while(n--){
    TRIANGLE_t t;
    POINT_t k, s;
    scanf("%d%d%d%d%d%d%d%d%d%d", &t.a.x, &t.a.y, &t.b.x, &t.b.y, &t.c.x, &t.c.y, &k.x, &k.y, &s.x, &s.y);
    if(solve(&k, &t) != solve(&s, &t)) puts("OK");
    else puts("NG");
  }
  return 0;
}

C++: #include <stdio.h>
#include <math.h>

int main(void){
    int n;
    cin >> n;
    rep(i,n) {
        int count = 0;
        double x[8],y[8],a,b,c,d,p,q,det;
        //入力を多少簡略化
        for(int i=0;i<8;i++) {
            printf("x%d =? ",i+1); scanf("%lf" ,&x[i]);
            printf("y%d =? ",i+1); scanf("%lf" ,&y[i]);
        }
        rep(j,3) {
            //(x1,y1)(x2,y2)を結ぶax+by=p の係数
            a = y[j]-y[j+1];  b = x[j+1]-x[j];
            p = x[j+1]*y[j]-x[j]*y[j+1];
            //(x3,y3)(x4,y4)を結ぶcx+dy=q の係数
            c = y[6]-y[7];  d = x[7]-x[6];
            q = x[7]*y[6]-x[6]*y[7];
            //係数についての行列式
            det = a*d-c*b;
            //det=0で解は存在しない(この問題については２線分は同一直線上にないから)
            if(fabs(det) < 0.00001){
            } else {
                ansx = (d*p - b*q)/det;
                ansy = (-c*p + a*q)/det;
                //ansxがどちらの線分のx座標範囲にも含まれるかを判定
                if((x[0] - ansx)*(x[1] - ansx) <= 0.00001 && (x[6] - ansx)*(x[7] - ansx) <= 0.00001 ) {
                    count++;
                }
            }
        }
        if(count == 1) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
}

C++: #include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<climits>
 
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);
  }
};
 
struct segment{
  point a,b;
};
 
double dot(point a, point b) {
  return (a.x * b.x + a.y * b.y);
}
 
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 );
}

 double vec_abs(point a){
  return sqrt(a.x*a.x+a.y*a.y);
}

int ccw(point p0, point p1, point p2){
  point a = p1 - p0;
  point b = p2 - p0;
  if(cross(a,b)>EPS)return +1;
  if(cross(a,b)<EPS)return -1;
  if(dot(a,b)<-EPS)return +2;
  if(a.x*a.x+a.y*a.y<b.x*b.x+b.y*b.y)return -2;
  return 0;
}

 int inside(point t,point* p,int N){
  int i,count=0,j=0;
  segment lt,lp;

  p[N]=p[0];
  p[N+1]=p[1];

  lt.a=lt.b=t;
  lt.b.x=INT_MAX;

  for(i=1;i<=N;i++){
    lp.a=lp.b=p[i];

    if(!is_intersected_ls(lp.a,lp.b,lt.a,lt.b)){
      if(i==j+1){
	lp.b=p[j];
	if(is_intersected_ls(lp.a,lp.b,lt.a,lt.b))count++;
      }
      else if(ccw(lt.a,lt.b,p[i])*ccw(lt.a,lt.b,p[j])<0)count++;
      j=i;
    }
  }
  return count & 1;
}

int main(void){
  int n;
  point p[5],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;
 
    if(inside(k,p,3)!=inside(s,p,3))cout << "OK" << endl;
    else cout << "NG" << endl;
     
  }
 
  return 0;
}