POJ 3335

/*半平面交求核心的增量法:
假设前N-1个半平面交,对于第N个半平面,只需用它来交前N-1个平面交出的多边形。
算法开始时,调整点的方向为顺时针方向,对于是否为顺时针,只需求出其面积,若为正,必为逆时针的。
对于每相邻两点求出一条直线,用该直线去交其半平面,并求出交点及判断原多边形点的方位。
*/ 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN=110;
const double eps=1e-8;

struct point {
	double x,y;
};
point pts[MAXN],p[MAXN],q[MAXN];
int n,cCnt,curCnt;

int DB(double d){
	if(d>eps) return 1;
	if(d<-eps) return -1;
	return 0;
}

double getArea(point *tmp, int n){
	double ans=0;
	for(int i=1;i<=n;i++)
	ans+=(tmp[i].x*tmp[i+1].y-tmp[i].y*tmp[i+1].x);
	return ans/2;
}

void Adjust(point *ps,int n){
	for(int i = 1; i < (n+1)/2; i ++)
 	swap(ps[i], ps[n-i]);
}

void initial(){
	double area=getArea(pts,n);
	if(DB(area)==1) Adjust(pts,n);
	for(int i=1;i<=n;i++)
	p[i]=pts[i];
	p[n+1]=p[1];
	p[0]=p[n];
	cCnt=n;
}

void getline(point x,point y,double &a,double &b,double &c){
    a = y.y - x.y;
    b = x.x - y.x;
    c = y.x * x.y - x.x * y.y;
}

point intersect(point x,point y,double a,double b,double c){
    double u = fabs(a * x.x + b * x.y + c);
    double v = fabs(a * y.x + b * y.y + c);
    point pt;
    pt.x=(x.x * v + y.x * u) / (u + v);
    pt.y=(x.y * v + y.y * u) / (u + v);
    return  pt;
}

void cut(double a,double b,double c){
	curCnt=0;
	for(int i=1;i<=cCnt;i++){
		if(DB(a*p[i].x+b*p[i].y+c)>=0) q[++curCnt] = p[i];
		else {
			if(DB(a*p[i-1].x + b*p[i-1].y + c )>0){
				q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
			}
			 if(DB(a*p[i+1].x + b*p[i+1].y + c )> 0){
                q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
            }
		}
	}
 	for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
    p[curCnt+1] = q[1];p[0] = p[curCnt];
    cCnt = curCnt;
}

void slove(){
	initial();
	for(int i=1;i<=n;i++){
		double a,b,c;
		getline(pts[i],pts[i+1],a,b,c);
		cut(a,b,c);
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		scanf("%lf%lf",&pts[i].x,&pts[i].y);
		pts[n+1]=pts[1];
		slove();
		if(cCnt>=1) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

  

POJ 3335

时间: 2024-11-13 06:52:32

POJ 3335的相关文章

poj 3335 Rotating Scoreboard(半平面交)

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 2550 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 ed

半平面交 模板 poj 3335 poj 3130 poj 1474 判断半平面交是否为空集

半平面交模板 const double pi= acos(-1.0); #define arc(x) (x / 180 * pi) const double EPS = 1e-8; const int Max_N = 105; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point p){ return Point(x- p.x , y - p.y ) ;

poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积

1 /*************** 2 poj 3335 点序顺时针 3 ***************/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 const double eps = 1e-8; 9 const double maxn = 0x7f7f7f7f; 10 int dcmp(double x){ 11 if(fabs(

POJ 3335 半平面交求多边形的核

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4899   Accepted: 1946 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 ed

poj 3335(半平面交)

链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题 ---------------------------------------------------------------- Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5158   Accepted: 2061 Description This year, ACM/ICPC

POJ 3335 Rotating Scoreboard(半平面交 模板)

题目链接:http://poj.org/problem?id=3335 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 somewher

POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)

题目链接:http://poj.org/problem? id=3335 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 somewhe

POJ 3335 Rotating Scoreboard(半平面交求多边形核)

题目链接 题意 : 给你一个多边形,问你在多边形内部是否存在这样的点,使得这个点能够看到任何在多边形边界上的点. 思路 : 半平面交求多边形内核. 半平面交资料 关于求多边形内核的算法 什么是多边形的内核? 它是平面简单多边形的核是该多边形内部的一个点集,该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部.就是一个在一个房子里面放一个摄像 头,能将所有的地方监视到的放摄像头的地点的集合即为多边形的核. 如上图,第一个图是有内核的,比如那个黑点,而第二个图就不存在内核了,无论点在哪里,总

POJ 3335 Rotating Scoreboard 半平面交求多边形内核

题目大意:多边形求内核模板题 思路:半平面交,我用的是O(nlogn)的半平面交,但是有一个问题,就是当多边形内核是一个点的时候,半平面交所得到的答案是空集合,但是输出应该是yes,实在没有什么好的解决方法,最后只能把所有直线向右移动,然后在求内核.但是这样做eps的不同取值有的时候能A有的时候不能A.有没有什么好的解决方法啊!!!求解答啊!!! CODE: #include <cmath> #include <cstdio> #include <cstring> #i