-
Notifications
You must be signed in to change notification settings - Fork 12
/
is_contain_in_the_triangle.cpp
99 lines (85 loc) · 3.81 KB
/
is_contain_in_the_triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// 点と三角形の包含関係
//
// verified:
// AOJ 0143
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0143
//
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
//------------------------------//
// 基本要素 (点)
//------------------------------//
using DD = double;
const DD INF = 1LL<<60; // to be set appropriately
const DD EPS = 1e-10; // to be set appropriately
const DD PI = acosl(-1.0);
DD torad(int deg) {return (DD)(deg) * PI / 180;}
DD todeg(DD ang) {return ang * 180 / PI;}
/* Point */
struct Point {
DD x, y;
Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';}
};
inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}
inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}
inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}
inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}
inline Point conj(const Point &p) {return Point(p.x, -p.y);}
inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) {return Point(-p.y, p.x);}
inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}
inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}
inline DD norm(const Point &p) {return dot(p, p);}
inline DD abs(const Point &p) {return sqrt(dot(p, p));}
inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}
inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}
inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}
inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}
inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}
// 粗
// 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)、0:一直線上
int simple_ccw(const Point &a, const Point &b, const Point &c) {
if (cross(b-a, c-a) > EPS) return 1;
if (cross(b-a, c-a) < -EPS) return -1;
return 0;
}
// 精
// 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)
// 2:c-a-bの順に一直線上、-2:a-b-cの順に一直線上、0:a-c-bの順に一直線上
int ccw(const Point &a, const Point &b, const Point &c) {
if (cross(b-a, c-a) > EPS) return 1;
if (cross(b-a, c-a) < -EPS) return -1;
if (dot(b-a, c-a) < -EPS) return 2;
if (norm(b-a) < norm(c-a) - EPS) return -2;
return 0;
}
// 点と三角形の包含関係(辺上については判定していない)
bool is_contain(const Point &p, const Point &a, const Point &b, const Point &c) {
int r1 = simple_ccw(p, b, c), r2 = simple_ccw(p, c, a), r3 = simple_ccw(p, a, b);
if (r1 == 1 && r2 == 1 && r3 == 1) return true;
if (r1 == -1 && r2 == -1 && r3 == -1) return true;
return false;
}
//------------------------------//
// Examples
//------------------------------//
int main() {
Point p[3], a, b;
int n;
cin >> n;
for (int id = 0; id < n; ++id) {
for (int i = 0; i < 3; ++i) cin >> p[i].x >> p[i].y;
cin >> a.x >> a.y >> b.x >> b.y;
int j1 = is_contain(a, p[0], p[1], p[2]);
int j2 = is_contain(b, p[0], p[1], p[2]);
if (j1 == j2) puts("NG");
else puts("OK");
}
}