## POJ 1474 Video Surveillance 半平面交求多边形内核存在性

2017年11月21日 ⁄ 综合 ⁄ 共 1923字 ⁄ 字号 评论关闭

CODE：

```#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 3010
#define EPS 1e-8
#define DCMP(a) (fabs(a) < EPS)
using namespace std;

struct Point{
double x,y;

Point(double _ = .0,double __ = .0):x(_),y(__) {}
Point operator +(const Point &a)const {
return Point(x + a.x,y + a.y);
}
Point operator -(const Point &a)const {
return Point(x - a.x,y - a.y);
}
Point operator *(double a)const {
return Point(x * a,y * a);
}
scanf("%lf%lf",&x,&y);
}
}point[MAX],p[MAX];
struct Line{
Point p,v;
double alpha;

Line(Point _,Point __):p(_),v(__) {
alpha = atan2(v.y,v.x);
}
Line() {}
bool operator <(const Line &a)const {
return alpha < a.alpha;
}
}line[MAX],q[MAX];

int points;
int lines;

inline void Initialize()
{
lines = 0;
}

inline void MakeLine(const Point &a,const Point &b)
{
line[++lines] = Line(a,b - a);
}

inline double Cross(const Point &a,const Point &b)
{
return a.x * b.y - a.y * b.x;
}

inline bool OnLeft(const Line &l,const Point &p)
{
return Cross(l.v,p - l.p) >= 0;
}

inline Point GetIntersection(const Line &a,const Line &b)
{
Point u = a.p - b.p;
double temp = Cross(b.v,u) / Cross(a.v,b.v);
return a.p + a.v * temp;
}

inline bool HalfPlaneIntersection()
{
int front = 1,tail = 1;
q[1] = line[1];
for(int i = 2;i <= lines; ++i) {
while(front < tail && !OnLeft(line[i],p[tail - 1]))	--tail;
while(front < tail && !OnLeft(line[i],p[front]))	++front;
if(DCMP(Cross(line[i].v,q[tail].v)))
q[tail] = OnLeft(line[i],q[tail].p) ? q[tail]:line[i];
else	q[++tail] = line[i];
if(front < tail)	p[tail - 1] = GetIntersection(q[tail],q[tail - 1]);
}
while(front < tail && !OnLeft(q[front],p[tail - 1]))	--tail;
if(tail - front <= 1)	return false;
return tail > front;
}

int main()
{
while(scanf("%d",&points),points) {
Initialize();
for(int i = 1;i <= points; ++i)
for(int i = points;i > 1; --i)
MakeLine(point[i],point[i - 1]);
MakeLine(point[1],point[points]);
sort(line + 1,line + lines + 1);
bool ans = HalfPlaneIntersection();
static int T = 0;
printf("Floor #%d\n",++T);
if(ans)	puts("Surveillance is possible.\n");
else	puts("Surveillance is impossible.\n");
}
return 0;
}```