POJ 2540 Hotter Colder --半平面交
题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远了,"Same"是相同。要你推测目标点的可能位置的面积。
解法:半平面交水题。从一个点到另一个点远了,说明目标点在两点之间连线的中垂线的离源点较近的一侧,即我们每次都可以得到一条直线来切割平面,要么切割左侧,要么切割右侧,要么都切,再求一个半平面交就可以得出可能面积了。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define pi acos(-1.0) #define eps 1e-8 using namespace std; struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} void input() { scanf("%lf%lf",&x,&y); } }; typedef Point Vector; struct Line{ Point p; Vector v; double ang; Line(){} Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); } Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); } bool operator < (const Line &L)const { return ang < L.ang; } }; int dcmp(double x) { if(x < -eps) return -1; if(x > eps) return 1; return 0; } template <class T> T sqr(T x) { return x * x;} Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; } bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; } bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } Vector VectorUnit(Vector x){ return x / Length(x);} Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);} double angle(Vector v) { return atan2(v.y, v.x); } Point GetLineIntersection(Line A, Line B) { Vector u = A.p - B.p; double t = Cross(B.v, u) / Cross(A.v, B.v); return A.p + A.v*t; } double DisP(Point A,Point B) { return Length(B-A); } double CalcConvexArea(Point* p,int n) { //凸包面积 double area = 0.0; for(int i=1;i<n-1;i++) area += Cross(p[i]-p[0],p[i+1]-p[0]); return fabs(area*0.5); } bool OnLeft(Line L, Point p) { return dcmp(Cross(L.v,p-L.p)) > 0; } bool CmpPolarLine(Line a,Line b) { //直线极角排序 return angle(a.v) < angle(b.v); } int HalfPlaneIntersection(Line* L, int n, Point* poly) { //半平面交点存入poly sort(L,L+n,CmpPolarLine); int first,last; Point *p = new Point[n]; Line *q = new Line[n]; q[first=last=0] = L[0]; for(int i=1;i<n;i++) { while(first < last && !OnLeft(L[i],p[last-1])) last--; while(first < last && !OnLeft(L[i],p[first])) first++; q[++last] = L[i]; if(dcmp(Cross(q[last].v, q[last-1].v)) == 0) { last--; if(OnLeft(q[last], L[i].p)) q[last] = L[i]; } if(first < last) p[last-1] = GetLineIntersection(q[last-1],q[last]); } while(first < last && !OnLeft(q[first],p[last-1])) last--; if(last-first <= 1) return 0; //点或线或无界平面,返回0 p[last] = GetLineIntersection(q[last],q[first]); int m = 0; for(int i=first;i<=last;i++) poly[m++] = p[i]; delete p; delete q; return m; } Line L[102],TL[103]; Point poly[104]; int main() { int i,j,tot = -1; Point n,p; char ss[10]; p.x = p.y = 0.0; TL[++tot] = Line(Point(0,0),Vector(10,0)); TL[++tot] = Line(Point(10,0),Vector(0,10)); TL[++tot] = Line(Point(10,10),Vector(-10,0)); TL[++tot] = Line(Point(0,10),Vector(0,-10)); while(scanf("%lf%lf%s",&n.x,&n.y,ss)!=EOF) { if(ss[0] == 'H') TL[++tot] = Line(Point((n.x+p.x)/2.0,(n.y+p.y)/2.0),Vector(Normal(p-n))); else if(ss[0] == 'C') TL[++tot] = Line(Point((n.x+p.x)/2.0,(n.y+p.y)/2.0),Vector(Normal(n-p))); else { TL[++tot] = Line(Point((n.x+p.x)/2.0,(n.y+p.y)/2.0),Vector(Normal(p-n))); TL[++tot] = Line(Point((n.x+p.x)/2.0,(n.y+p.y)/2.0),Vector(Normal(n-p))); } p = n; for(i=0;i<=tot;i++) L[i] = TL[i]; int m = HalfPlaneIntersection(L,tot+1,poly); if(!m) puts("0.00"); else printf("%.2f\n",CalcConvexArea(poly,m)); } return 0; }
作者:whatbeg
出处1:http://whatbeg.com/
出处2:http://www.cnblogs.com/whatbeg/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
更多精彩文章抢先看?详见我的独立博客: whatbeg.com