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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
using namespace std;
typedef pair<int,int> P;
typedef long double ld;
typedef vector<int> vi;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define LINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
bool isin(int a,int b,int c,int d,int p,int q)
{
	//printf("%d %d %d %d %d %d\n",a,b,c,d,p,q);
	ld s,t;
	ld A=a,B=b,C=c,D=d,P=p,Q=q;
	if(b==0)
	{
		t=Q/D;
		s=P/A-(C*Q)/(A*D);
		//cout << s << ' ' << t << endl;
		if(s>0&&t>0&&s+t<1)return true;
		else return false;
	}
	if(a==0)
	{
		t=P/C;
		s=Q/B-(D*Q)/(B*C);
		//cout << s << ' ' << t << endl;
		if(s>0&&t>0&&s+t<1)return true;
		else return false;
	}
	t=(P*B-A*Q)/(B*C-A*D);
	s=(P-C*t)/A;
	//cout << s << ' ' << t << endl;
	if(s>0&&t>0&&s+t<1)return true;
	else return false;
}
int main()
{
	int Q;
	scanf("%d",&Q);
	int x1,x2,x3,y1,y2,y3,xk,yk,xs,ys;
	for(int i=0;i<Q;i++)
	{
		scanf("%d %d %d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&xk,&yk,&xs,&ys);
		int a,b,c,d,p,q,r,s;
		a=x2-x1;
		b=y2-y1;
		c=x3-x1;
		d=y3-y1;
		p=xk-x1;
		q=yk-y1;
		r=xs-x1;
		s=ys-y1;
		bool orihime = isin(a,b,c,d,p,q);
		bool kengyu = isin(a,b,c,d,r,s);
		if(kengyu!=orihime)cout << "OK" << endl;
		else cout << "NG" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <complex>

using namespace std;
typedef complex<double> P;

#define EPS (1e-5)

struct Seg {
  P s, t;
  Seg(P s, P t) : s(s), t(t) {}
};

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

bool isIntersectedSS(Seg a, Seg b) {
  return ( cross(a.t-a.s, b.s-a.s) * cross(a.t-a.s, b.t-a.s) < EPS )
      && ( cross(b.t-b.s, a.s-b.s) * cross(b.t-b.s, a.t-b.s) < EPS );
}

int main() {
  int n; cin >> n;
  for(int I=0; I<n; I++) {
    int x, y;
    cin >> x >> y; P pt1(x, y);
    cin >> x >> y; P pt2(x, y);
    cin >> x >> y; P pt3(x, y);
    cin >> x >> y; P pk(x, y);
    cin >> x >> y; P ps(x, y);
    
    int ok = 0;
    ok += isIntersectedSS(Seg(pt1, pt2), Seg(pk, ps));
    ok += isIntersectedSS(Seg(pt2, pt3), Seg(pk, ps));
    ok += isIntersectedSS(Seg(pt3, pt1), Seg(pk, ps));
    
    if(ok%2) {
      cout << "OK" << endl;
    }
    else {
      cout << "NG" << endl;
    }
  }
  return 0;
}

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

using namespace std;

typedef double D; 
#define EPS (1e-10)
#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;
}

C++: #include<vector>
#include<complex>
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
using namespace std;

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};

typedef vector<P> G;

struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
};

int ccw(P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > 0)   return +1;       // counter clockwise
  if (cross(b, c) < 0)   return -1;       // clockwise
  if (dot(b, c) < 0)     return +2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;       // a--b--c on line
  return 0;
}

enum { OUT, ON, IN };
int contains(const G& g, const P& p) {
  bool in = false;
  for (int i = 0; i < g.size(); ++i) {
  	P a,b;
    a = curr(g,i) - p, b = next(g,i) - p;
    if (imag(a) > imag(b)) swap(a, b);
    if (imag(a) <= 0 && 0 < imag(b))
      if (cross(a, b) < 0) in = !in;
    if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;
  }
  return in ? IN : OUT;
}

int main(){
	int n; cin>>n;
	double x1,y1,x2,y2,x3,y3,xk,yk,xs,ys;
	while(n--){
		cin>>x1>>y1>>x2>>y2>>x3>>y3>>xk>>yk>>xs>>ys;
		P pk(xk,yk),ps(xs,ys),p1(x1,y1),p2(x2,y2),p3(x3,y3);
		G t;t.push_back(p1),t.push_back(p2),t.push_back(p3);
		bool f1=contains(t,pk),f2=contains(t,ps);
		cout<<(f1&&!f2||!f1&&f2?"OK":"NG")<<endl;
	}
	return 0;
}