交了两天的半平面交的模版题终于过了,,,原来是 OnLeft 里面少抄了一个等于号......
半平面交 poj3335
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <Vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define INF 0x3f3f3f3f 16 #define eps 1e-10 17 #define OPEN_FILE 18 using namespace std; 19 int n; 20 21 int dcmp(double x){ 22 if (fabs(x) < eps) return 0; 23 return x < 0 ? -1 : 1; 24 } 25 struct Point{ 26 double x, y; 27 Point(double p = 0, double q = 0){ 28 x = p; 29 y = q; 30 } 31 }; 32 struct Node{ 33 int p; 34 Point A, B; 35 Node(Point a1, Point a2, int t){ 36 A = a1; 37 B = a2; 38 p = t; 39 } 40 }; 41 typedef Point Vector; 42 43 Vector operator + (Vector A, Vector B){ 44 return Vector(A.x + B.x, A.y + B.y); 45 } 46 Vector operator - (Vector A, Vector B){ 47 return Vector(A.x - B.x, A.y - B.y); 48 } 49 Vector operator * (Vector A, double p){ 50 return Vector(A.x * p, A.y * p); 51 } 52 Vector operator / (Vector A, double p){ 53 return Vector(A.x / p, A.y / p); 54 } 55 bool operator == (Vector A, Vector B){ 56 return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0; 57 } 58 bool operator > (Vector A, Vector B){ 59 return A.x > B.x && A.y > B.y; 60 } 61 bool operator <(Vector A, Vector B){ 62 return A.x < B.x && A.y < B.y; 63 } 64 //点积 65 double Dot(Vector A, Vector B){ 66 return A.x * B.x + A.y * B.y; 67 } 68 //模 69 double Length(Vector A){ 70 return sqrt(Dot(A, A)); 71 } 72 //夹角 73 double Angle(Vector A, Vector B){ 74 return acos(Dot(A, B) / Length(A) / Length(B)); 75 } 76 //叉积 77 double Cross(Vector A, Vector B){ 78 return A.x * B.y - A.y*B.x; 79 } 80 //三角形面积 81 double Area2(Point A, Point B, Point C){ 82 return Cross(B - A, C - A); 83 } 84 struct Line 85 { 86 Point p; 87 Vector v; 88 double angle; 89 Line(){} 90 Line(Point p, Vector v) :p(p), v(v){ 91 angle = atan2(v.y, v.x); 92 } 93 bool operator < (const Line & L) const{ 94 return angle<L.angle; 95 } 96 }; 97 //点p在直线L的左边 98 bool OnLeft(Line L, Point p) { return Cross(L.v, p - L.p)>=0; } 99 //直线A,B的交点 100 Point GetIntersection(Line A, Line B) 101 { 102 Vector u = A.p - B.p; 103 double t = Cross(B.v, u) / Cross(A.v, B.v); 104 return A.p + A.v*t; 105 } 106 107 int HalfplaneIntersection(Line *L, int n, Point *poly) 108 { 109 sort(L, L + n); 110 111 int first, last; 112 Point *p = new Point[n]; 113 Line *q = new Line[n]; 114 q[first = last = 0] = L[0]; 115 for (int i = 1; i < n; i++) 116 { 117 while (first < last && !OnLeft(L[i], p[last - 1])) last--; 118 while (first < last && !OnLeft(L[i], p[first])) first++; 119 q[++last] = L[i]; 120 if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) 121 { 122 last--; 123 if (OnLeft(q[last], L[i].p)) q[last] = L[i]; 124 } 125 if (first < last) p[last - 1] = GetIntersection(q[last - 1], q[last]); 126 } 127 while (first < last && !OnLeft(q[first], p[last - 1])) last--; 128 if (last - first <= 1) return 0; 129 p[last] = GetIntersection(q[last], q[first]); 130 int m = 0; 131 for (int i = first; i <= last; i++) poly[m++] = p[i]; 132 return m; 133 } 134 135 Line L[105]; 136 Point P[105], poly[105]; 137 Vector v[105]; 138 int main() 139 { 140 #ifdef OPEN_FILE 141 //freopen("in.txt", "r", stdin); 142 //freopen("out.txt", "w", stdout); 143 #endif // OPEN_FILE 144 int T; 145 scanf("%d", &T); 146 double x, y; 147 for (int cas = 1; cas <= T; cas++){ 148 scanf("%d", &n); 149 for (int i = 0; i < n; i++) 150 { 151 scanf("%lf%lf", &x, &y); 152 P[i] = Point(x, y); 153 } 154 for (int i = 0; i < n; i++){ 155 v[i] = P[i] - P[(i + 1) % n]; 156 } 157 for (int i = 0; i < n; i++) 158 { 159 L[i] = Line(P[i], v[i]); 160 } 161 if (HalfplaneIntersection(L, n, poly) != 0){ 162 printf("YES\n"); 163 } 164 else{ 165 printf("NO\n"); 166 } 167 } 168 }