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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <algorithm> using namespace std;
double fPre = 1e-8; struct Point { double x; double y; Point() {} Point(double fX, double fY) { x = fX, y = fY; } }; typedef vector<Point> Polygon; typedef pair<Point, Point> Line; Point operator+(const Point a, const Point b) { Point t; t.x = a.x + b.x; t.y = a.y + b.y; return t; }
Point operator-(const Point a, const Point b) { Point t; t.x = a.x - b.x; t.y = a.y - b.y; return t; }
Point operator*(Point a, double fD) { Point t; t.x = a.x * fD; t.y = a.y * fD; return t; }
int DblCmp(double fD) { return fabs(fD) < fPre ? 0 : (fD > 0 ? 1 : -1); }
double Det(double fX1, double fY1, double fX2, double fY2) { return fX1 * fY2 - fX2 * fY1; }
double Cross(Point a, Point b, Point c) { return Det(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y); }
double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
Point Intersection(Point a1, Point a2, Point b1, Point b2) { Point a = a2 - a1; Point b = b2 - b1; Point s = b1 - a1;
return a1 + a * (Cross(b, s) / Cross(b, a)); }
Polygon HalfPlane(Polygon pg, Point a, Point b) { Polygon pgTmp; int nN = pg.size(); for (int i = 0; i < nN; ++i) { double fC = Cross(a, b, pg[i]); double fD = Cross(a, b, pg[(i + 1) % nN]); if (DblCmp(fC) >= 0) { pgTmp.push_back(pg[i]); } if (fC * fD < 0) { pgTmp.push_back(Intersection(a, b, pg[i], pg[(i + 1) % nN])); } } return pgTmp; }
int main() { int nN; Point p; vector<Point> vp; Polygon pg;
while (scanf("%d", &nN), nN) { vp.clear(); for (int i = 0; i < nN; ++i) { scanf("%lf%lf", &p.x, &p.y); vp.push_back(p); } pg.clear(); pg.push_back(Point(-1e9, 1e9)); pg.push_back(Point(-1e9, -1e9)); pg.push_back(Point(1e9, -1e9)); pg.push_back(Point(1e9, 1e9)); for (int i = 0; i < nN; ++i) { pg = HalfPlane(pg, vp[i], vp[(i + 1) % nN]); if (pg.size() == 0) { printf("0\n"); break; } } if (pg.size()) { printf("1\n"); } }
return 0; }
|