半平面交模板:
const double eps=1e-10; struct Point { double x; double y; }point[110],q[110],p[110]; //point[]记录最开始的多边形,q[]临时保存新切割的多边形p[]保存新切割的多边形 int n,m;//n初始多边形的点数,m为切割后的点数 double a,b,c;//直线ax+by+c=0的参数 int dcmp(double x) { if(x>eps) return 1; if(x<-eps) return -1; return 0; } void getlinex(Point xx,Point yy)//求两点的直线参数 { a=yy.y-xx.y; b=xx.x-yy.x; c=yy.x*xx.y-xx.x*yy.y; } Point intersect(Point px,Point py)//已知的直线ax+by+c=0与点px,py形成的直线的交点 { double u=fabs(a*px.x+b*px.y+c); double v=fabs(a*py.x+b*py.y+c); return Point{(px.x*v+py.x*u)/(u+v),(px.y*v+py.y*u)/(u+v)}; } void cut()//用直线ax+by+c=0切割多边形 { int cutm=0; for(int i=1;i<=m;i++) { if(dcmp(a*p[i].x+b*p[i].y+c)>=0) q[++cutm]=p[i]; else { if(dcmp(a*p[i-1].x+b*p[i-1].y+c)>0) q[++cutm]=intersect(p[i-1],p[i]); if(dcmp(a*p[i+1].x+b*p[i+1].y+c)>0) q[++cutm]=intersect(p[i+1],p[i]); } } for(int i=1;i<=cutm;i++) p[i]=q[i]; p[cutm+1]=q[1]; p[0]=q[cutm]; m=cutm; } void HalfPlaneIntersection() //HalfPlaneIntersection(double r)多边形缩小r之后的情况 { for(int i=1;i<=n;i++) p[i]=point[i]; point[n+1]=point[1]; p[n+1]=p[1]; p[0]=p[n]; m=n; for(int i=1;i<=n;i++) { // a = point[i].x-point[i+1].x; b = point[i+1].y-point[i].y; double sinx=a/sqrt(a*a+b*b); double cosx=b/sqrt(a*a+b*b); Point pt=Point{r*cosx,r*sinx}; Point pa=Point{point[i].x+pt.x,point[i].y+pt.y}; Point pb=Point{point[i+1].x+pt.x,point[i+1].y+pt.y}; getlinex(pa,pb); // cut(); getlinex(point[i],point[i+1]); cut(); } } 主函数: cin>>n; for(int i=1;i<=n;i++) scanf("%lf%lf",&point[i].x,&point[i].y);//是顺时针读入 如果题意是逆时针输入,则 for(int i=n;i>=1;i--) scanf("%lf%lf",&point[i].x,&point[i].y); HalfPlaneIntersection(); HalfPlaneIntersection(r);//多边形缩小r之后的情况 执行完之后,如果m=0则没有内核 p[]为切割之后的多边形的点
poj 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 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 nx1y1x2y2 ... xnyn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xiyi 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
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #define LL long long #define inf 0x3f3f3f3f using namespace std; const double eps=1e-10; struct Point { double x; double y; }point[110],q[110],p[110]; //point[]记录最开始的多边形,q[]临时保存新切割的多边形p[]保存新切割的多边形 int n,m;//n初始多边形的点数,m为切割后的点数 double a,b,c;//直线ax+by+c=0的参数 int dcmp(double x) { if(x>eps) return 1; if(x<-eps) return -1; return 0; } void getlinex(Point xx,Point yy)//求两点的直线参数 { a=yy.y-xx.y; b=xx.x-yy.x; c=yy.x*xx.y-xx.x*yy.y; } Point intersect(Point px,Point py)//已知的直线ax+by+c=0与点px,py形成的直线的交点 { double u=fabs(a*px.x+b*px.y+c); double v=fabs(a*py.x+b*py.y+c); return Point{(px.x*v+py.x*u)/(u+v),(px.y*v+py.y*u)/(u+v)}; } void cut()//用直线ax+by+c=0切割多边形 { int cutm=0; for(int i=1;i<=m;i++) { if(dcmp(a*p[i].x+b*p[i].y+c)>=0) q[++cutm]=p[i]; else { if(dcmp(a*p[i-1].x+b*p[i-1].y+c)>0) q[++cutm]=intersect(p[i-1],p[i]); if(dcmp(a*p[i+1].x+b*p[i+1].y+c)>0) q[++cutm]=intersect(p[i+1],p[i]); } } for(int i=1;i<=cutm;i++) p[i]=q[i]; p[cutm+1]=q[1]; p[0]=q[cutm]; m=cutm; } void HalfPlaneIntersection() { for(int i=1;i<=n;i++) p[i]=point[i]; point[n+1]=point[1]; p[n+1]=p[1]; p[0]=p[n]; m=n; for(int i=1;i<=n;i++) { getlinex(point[i],point[i+1]); cut(); } } int main() { int T; cin>>T; while(T--) { cin>>n; for(int i=1;i<=n;i++) scanf("%lf%lf",&point[i].x,&point[i].y); HalfPlaneIntersection(); if(!m) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }
poj 3525
Description
The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.
In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.
Input
The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.
n | ||
x1 | y1 | |
⋮ | ||
xn | yn |
Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.
n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xi, yi)–(xi+1, yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.
You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.
The last dataset is followed by a line containing a single zero.
Output
For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.
Sample Input
4 0 0 10000 0 10000 10000 0 10000 3 0 0 10000 0 7000 1000 6 0 40 100 20 250 40 250 70 100 90 0 70 3 0 0 10000 10000 5000 5001 0
Sample Output
5000.000000 494.233641 34.542948 0.353553
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #define LL long long #define inf 0x3f3f3f3f using namespace std; const double eps=1e-10; struct Point { double x; double y; }point[110],q[110],p[110]; //point[]记录最开始的多边形,q[]临时保存新切割的多边形p[]保存新切割的多边形 int n,m;//n初始多边形的点数,m为切割后的点数 double a,b,c;//直线ax+by+c=0的参数 int dcmp(double x) { if(x>eps) return 1; if(x<-eps) return -1; return 0; } void getlinex(Point xx,Point yy)//求两点的直线参数 { a=yy.y-xx.y; b=xx.x-yy.x; c=yy.x*xx.y-xx.x*yy.y; } Point intersect(Point px,Point py)//已知的直线ax+by+c=0与点px,py形成的直线的交点 { double u=fabs(a*px.x+b*px.y+c); double v=fabs(a*py.x+b*py.y+c); return Point{(px.x*v+py.x*u)/(u+v),(px.y*v+py.y*u)/(u+v)}; } void cut()//用直线ax+by+c=0切割多边形 { int cutm=0; for(int i=1;i<=m;i++) { if(dcmp(a*p[i].x+b*p[i].y+c)>=0) q[++cutm]=p[i]; else { if(dcmp(a*p[i-1].x+b*p[i-1].y+c)>0) q[++cutm]=intersect(p[i-1],p[i]); if(dcmp(a*p[i+1].x+b*p[i+1].y+c)>0) q[++cutm]=intersect(p[i+1],p[i]); } } for(int i=1;i<=cutm;i++) p[i]=q[i]; p[cutm+1]=q[1]; p[0]=q[cutm]; m=cutm; } void HalfPlaneIntersection(double r) { for(int i=1;i<=n;i++) p[i]=point[i]; point[n+1]=point[1]; p[n+1]=p[1]; p[0]=p[n]; m=n; for(int i=1;i<=n;i++) { a = point[i].x-point[i+1].x; //point[]不是p[].... b = point[i+1].y-point[i].y; double sinx=a/sqrt(a*a+b*b); double cosx=b/sqrt(a*a+b*b); Point pt=Point{r*cosx,r*sinx}; Point pa=Point{point[i].x+pt.x,point[i].y+pt.y}; Point pb=Point{point[i+1].x+pt.x,point[i+1].y+pt.y}; getlinex(pa,pb); cut(); } } int main() { while(cin>>n) { if(n==0) return 0; for(int i=n;i>=1;i--) scanf("%lf%lf",&point[i].x,&point[i].y); double l=0,r=20000; while(r-l>eps) { double mid=(l+r)/2; HalfPlaneIntersection(mid); if(!m) r=mid; else l=mid; } printf("%.6f\n",l); } return 0; }