POJ3335Rotating Scoreboard【半平面交判断多边形是否存在内核】
2015-09-08 17:19
399 查看
Language: Default Rotating Scoreboard
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 |
第一道半平面交参考博客:http://blog.csdn.net/accry/article/details/6070621
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct point{
double x,y;
}A[105],p[105],q[105];
int N;
int endcnt,tempcnt;
double r;
void getline(point p1,point p2,double &a,double &b,double &c){
a=p2.y-p1.y;
b=p1.x-p2.x;
c=p2.x*p1.y-p2.y*p1.x;
}
void init(){
for(int i=1;i<=N;++i){
p[i]=A[i];
}
p[N+1]=p[1];p[0]=p
;
endcnt=N;
}
point intersect(point p1,point p2,double a,double b,double c){
double u=fabs(a*p1.x+b*p1.y+c);
double v=fabs(a*p2.x+b*p2.y+c);
point temp;
temp.x=(p1.x*v+p2.x*u)/(u+v);
temp.y=(p1.y*v+p2.y*u)/(u+v);
return temp;
}
void cut(double a,double b,double c){
tempcnt=0;
for(int i=1;i<=endcnt;++i){
if(a*p[i].x+b*p[i].y+c>=0)q[++tempcnt]=p[i];
else {
if(a*p[i-1].x+b*p[i-1].y+c>0)
q[++tempcnt]=intersect(p[i],p[i-1],a,b,c);
if(a*p[i+1].x+b*p[i+1].y+c>0)
q[++tempcnt]=intersect(p[i],p[i+1],a,b,c);
}
}
for(int i=1;i<=tempcnt;++i){
p[i]=q[i];
}
p[tempcnt+1]=q[1];p[0]=p[tempcnt];
endcnt=tempcnt;
}
void guizheng(){
int i;
for(i=1;i<=N;++i){
q[i]=A[N-i+1];
}
for(i=1;i<=N;++i){
A[i]=q[i];
}
}
void solve(){
//guizheng();
init();
for(int i=1;i<=N;++i){
double a,b,c;
getline(A[i],A[i+1],a,b,c);
cut(a,b,c);
}
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d",&N);
for(i=1;i<=N;++i){
scanf("%lf%lf",&A[i].x,&A[i].y);
}A[N+1]=A[1];
solve();
if(endcnt>0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
相关文章推荐
- 简单分页
- CentOS7下FTP的安装配置
- HDU 3683 Gomoku(五子棋判断结果,模拟题)
- 大数相乘的实现
- 从傅里叶变换到Gabor变换再到小波变换
- Geometric Progression----(BestCoder Round #54 (div.2))
- 任性写记事本
- 2_兔子产仔问题
- 如何分析一个产品?
- Symmetric Tree
- vs2010下编译osip2和eXosip2的4.0.0版的静态库及搭建开发环境
- ibus拼音不能正常输入
- Android笔记:20150823
- Android笔记:20150824(this关键字的使用)
- vmotion的网络要求
- 2_兔子产仔问题
- 两种思维存在,我只是不再紧张
- 01矩阵最大正方形
- 开发记录_自学Python写爬虫程序爬取csdn个人博客信息
- Android笔记:20150820