应该是求多边形是否存在?
上半平面交。
返回的点数<=2就不存在了。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1005;
struct Point {
double x, y;
Point(){}
Point(double _x, double _y) : x(_x), y(_y) {}
Point operator +(const Point &b) const { return Point(x + b.x, y + b.y); }
Point operator -(const Point &b) const { return Point(x - b.x, y - b.y); }
double operator *(const Point &b) const { return x * b.y - y * b.x; }
Point operator *(double b) const { return Point(b * x, b * y); }
} poly[N];
struct Line {
Point x, v;
double ang;
Line(){}
Line(const Point &_x, const Point &_v) : x(_x), v(_v), ang(atan2(_v.y, _v.x)) { }
bool operator <(const Line &b) const {
if (ang == b.ang) return v * (b.x - x) < 0;
return ang < b.ang;
}
bool operator ==(const Line &b) const { return ang == b.ang; }
Point intersection(const Line &b) {
Point u = x - b.x;
double t = (b.v * u) / (v * b.v);
return x + v * t;
}
} lines[N];
bool right(const Point &p, const Line &l) {
return l.v * (p - l.x) < 0;
}
int half_plane_intersection(Line l[], int n, Point *poly) {
static Line q[N];
sort(l + 1, l + n + 1);
n = unique(l + 1, l + n + 1) - (l + 1);
int f = 1, r = 0, m = 0;
q[++r] = l[1]; q[++r] = l[2];
for (int i = 3; i <= n; i++) {
while (f < r && right(q[r].intersection(q[r - 1]), l[i])) r--;
while (f < r && right(q[f].intersection(q[f + 1]), l[i])) f++;
q[++r] = l[i];
}
while (f < r && right(q[r].intersection(q[r - 1]), q[f])) r--;
while (f < r && right(q[f].intersection(q[f + 1]), q[r])) f++;
q[r + 1] = q[f];
for (int i = f; i <= r; i++)
poly[++m] = q[i].intersection(q[i + 1]);
return m;
}
int main() {
int t, n, j, line_num;
scanf("%d", &t);
while (t--) {
line_num = 0;
scanf("%d", &n);
for (j = 1; j <= n; j++)
scanf("%lf %lf", &poly[j].x, &poly[j].y);
poly[n + 1] = poly[1];
for (j = 1; j <= n; j++)
lines[++line_num] = Line(poly[j], poly[j] - poly[j + 1]);
int m = half_plane_intersection(lines, line_num, poly);
puts(m <= 2 ? "NO" : "YES");
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6218 | Accepted: 2456 |
Description
This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.
Input
The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ...xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.
Output
The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.
Sample Input
2 4 0 0 0 1 1 1 1 0 8 0 0 0 2 1 2 1 1 2 1 2 2 3 2 3 0
Sample Output
YES NO