Pieces and Discs 切割多邊形 ,判斷多邊形和圓相交
阿新 • • 發佈:2019-01-27
直接切割多邊形~ 判斷多邊形和園盤是否有公共點(面積>0)
1 內含的情況--只要多邊形poly[0] 在圓內、或者圓心在多邊形內
2 相交的情況-如果不是規範相交,那麼不是內含,卻有非零公共面積只有一種情況,就是兩個點都在圓上,只有判斷中點在圓上即可。
額 先每一個案例忘記輸出空行 並不提示Presentation Error ,wa了很久
然後,每次pieces更新的時候,newpieces 需要清零啊
程式碼:
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<string> const double INF=0x3fffffff; const double eps=1e-10; const double PI=acos(-1.0); using namespace std; struct Point{ double x; double y; Point(double x=0,double y=0):x(x),y(y){} }; int dcmp(double x) {return (x>eps)-(x<-eps); } int sgn(double x) {return (x>eps)-(x<-eps); } typedef Point Vector; typedef vector<Point> Polygon; // 定義多邊形 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);} ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y; return out;} // bool operator< (const Point &A,const Point &B) { return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0); } 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 Cross(Vector A,Vector B) {return A.x*B.y-B.x*A.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 Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);} Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));} Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);} Point GetLineIntersection(Point P,Vector v,Point Q,Vector w) { Vector u=P-Q; double t=Cross(w, u)/Cross(v,w); return P+v*t; } double DistanceToLine(Point P,Point A,Point B) { Vector v1=P-A; Vector v2=B-A; return fabs(Cross(v1,v2))/Length(v2); } double DistanceToSegment(Point P,Point A,Point B) { if(A==B) return Length(P-A); Vector v1=B-A; Vector v2=P-A; Vector v3=P-B; if(dcmp(Dot(v1,v2))==-1) return Length(v2); else if(Dot(v1,v3)>0) return Length(v3); else return DistanceToLine(P, A, B); } Point GetLineProjection(Point P,Point A,Point B) { Vector v=B-A; Vector v1=P-A; double t=Dot(v,v1)/Dot(v,v); return A+v*t; } bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2) { double c1=Cross(b1-a1, a2-a1); double c2=Cross(b2-a1, a2-a1); double c3=Cross(a1-b1, b2-b1); double c4=Cross(a2-b1, b2-b1); return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ; } bool OnSegment(Point P,Point A,Point B) //本題要求不含端點 { return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<0; } bool SegmentCommon(Point a1,Point a2,Point b1,Point b2) { double c1=Cross(b1-a1, a2-a1); double c2=Cross(b2-a1, a2-a1); double c3=Cross(a1-b1, b2-b1); double c4=Cross(a2-b1, b2-b1); if(c1==0&&c2==0&&c3==0&&c4==0) { if(OnSegment(a1, b1, b2)) return 1; if(OnSegment(a2, b1, b2)) return 1; return 0; } return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0 ; } double PolygonArea(Point *p,int n) { double area=0; for(int i=1;i<n-1;i++) { area+=Cross(p[i]-p[0], p[i+1]-p[0]); } return area/2; } double PolygonArea(Polygon p) { double area=0; int n=p.size(); for(int i=1;i<n-1;i++) { area+=Cross(p[i]-p[0], p[i+1]-p[0]); } return area/2; } Point read_point() { Point P; scanf("%lf%lf",&P.x,&P.y); return P; } // ---------------與圓有關的-------- struct Circle { Point c; double r; Circle(Point c=Point(0,0),double r=0):c(c),r(r) {} Point point(double a) { return Point(c.x+r*cos(a),c.y+r*sin(a)); } }; double angle(Vector v) {return atan2(v.y,v.x);} struct Line { Point p; Vector v; double Angle; Line(Point p=Point(0,0),Vector v=Vector(0,1)):p(p),v(v) { Angle=angle(v);} bool operator<(const Line & L) const { return Angle<L.Angle; } Point point(double t) { return Point(p+v*t); } }; int getLineCircleIntersection(Line L,Circle C,double &t1,double &t2,vector<Point> &sol) { double a=L.v.x; double b=L.p.x-C.c.x; double c=L.v.y; double d=L.p.y-C.c.y; double e=a*a+c*c; double f=2*(a*b+c*d); double g=b*b+d*d-C.r*C.r; double delta=f*f-4*e*g; if(dcmp(delta)<0) return 0; if(dcmp(delta)==0) { t1=t2=-f/(2*e); sol.push_back(L.point(t1)); return 1; } else { t1=(-f-sqrt(delta))/(2*e); t2=(-f+sqrt(delta))/(2*e); sol.push_back(L.point(t1)); sol.push_back(L.point(t2)); return 2; } } // 向量極角公式 int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point> &sol) { double d=Length(C1.c-C2.c); if(dcmp(d)==0) { if(dcmp(C1.r-C2.r)==0) return -1; // 重合 else return 0; // 內含 0 個公共點 } if(dcmp(C1.r+C2.r-d)<0) return 0; // 外離 if(dcmp(fabs(C1.r-C2.r)-d)>0) return 0; // 內含 double a=angle(C2.c-C1.c); double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d)); Point p1=C1.point(a-da); Point p2=C1.point(a+da); sol.push_back(p1); if(p1==p2) return 1; // 相切 else { sol.push_back(p2); return 2; } } // 求點到圓的切線 int getTangents(Point p,Circle C,Vector *v) { Vector u=C.c-p; double dist=Length(u); if(dcmp(dist-C.r)<0) return 0; else if(dcmp(dist-C.r)==0) { v[0]=Rotate(u,PI/2); return 1; } else { double ang=asin(C.r/dist); v[0]=Rotate(u,-ang); v[1]=Rotate(u,+ang); return 2; } } // 求兩圓公切線 int getTangents(Circle A,Circle B,Point *a,Point *b) { int cnt=0; if(A.r<B.r) { swap(A,B); swap(a, b); // 有時需標記 } double d=Length(A.c-B.c); double rdiff=A.r-B.r; double rsum=A.r+B.r; if(dcmp(d-rdiff)<0) return 0; // 內含 double base=angle(B.c-A.c); if(dcmp(d)==0&&dcmp(rdiff)==0) return -1 ; // 重合 無窮多條切線 if(dcmp(d-rdiff)==0) // 內切 外公切線 { a[cnt]=A.point(base); b[cnt]=B.point(base); cnt++; return 1; } // 有外公切線的情形 double ang=acos(rdiff/d); a[cnt]=A.point(base+ang); b[cnt]=B.point(base+ang); cnt++; a[cnt]=A.point(base-ang); b[cnt]=B.point(base-ang); cnt++; if(dcmp(d-rsum)==0) // 外切 有內公切線 { a[cnt]=A.point(base); b[cnt]=B.point(base+PI); cnt++; } else if(dcmp(d-rsum)>0) // 外離 又有兩條外公切線 { double ang_in=acos(rsum/d); a[cnt]=A.point(base+ang_in); b[cnt]=B.point(base+ang_in+PI); cnt++; a[cnt]=A.point(base-ang_in); b[cnt]=B.point(base-ang_in+PI); cnt++; } return cnt; } // 幾何演算法模板 bool isPointInPolygon(Point p,Point * poly,int n) { int wn=0; for(int i=0;i<n;i++) { if(OnSegment(p, poly[i], poly[(i+1)%n])) return -1; int k=dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i])); int d1=dcmp(poly[i].y-p.y); int d2=dcmp(poly[(i+1)%n].y-p.y); if(k>0&&d1<=0&&d2>0) wn++; if(k<0&&d2<=0&&d1>0) wn--; } if(wn!=0) return 1; else return 0; } bool isPointInPolygon(Point p,Polygon poly) { int wn=0; int n=poly.size(); for(int i=0;i<n;i++) { if(OnSegment(p, poly[i], poly[(i+1)%n])) return -1; int k=dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i])); int d1=dcmp(poly[i].y-p.y); int d2=dcmp(poly[(i+1)%n].y-p.y); if(k>0&&d1<=0&&d2>0) wn++; if(k<0&&d2<=0&&d1>0) wn--; } if(wn!=0) return 1; else return 0; } bool SegmentLineIntersection(Point a1,Point a2,Point b1,Point b2) //a1,a2 是直線 規範相交 { double c1=Cross(b1-a1, a2-a1); double c2=Cross(b2-a1, a2-a1); return dcmp(c1)*dcmp(c2)<=0; } Point ori(0,0); struct Segment{ Point A; Point B; Segment(Point A=ori,Point B=ori):A(A),B(B) {} }; bool OnLeft(Line L,Point p) { return Cross(L.v,p-L.p)>0; } 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.point(t); } bool LineCmp(Line a,Line b) { return angle(a.v)<angle(b.v); } int HalfplaneIntersection(Line *L,int n,Point *poly) { sort(L,L+n); int first,last; Point *p=new Point[n]; Line *q=new Line[n]; // 雙端佇列 q[first=last=0]=L[0]; for(int i=0;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(fabs(Cross(q[last].v,q[last-1].v))<eps) // 平行取內側 { 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; // 空集 p[last]=GetLineIntersection(q[last],q[first]); int m=0; for(int i=first;i<=last;i++) // 從雙端佇列輸出 { poly[m++]=p[i]; } return m; } vector<Polygon> pieces,newpieces; Polygon CutPolygon(Polygon poly ,Point A,Point B) { Polygon newpoly; int n=poly.size(); for(int i=0;i<n;i++) { Point C=poly[i]; Point D=poly[(i+1)%n]; if(dcmp(Cross(B-A,C-A))>=0) newpoly.push_back(C); if(dcmp(Cross(B-A,D-C))!=0) { Point ip=GetLineIntersection(C, D-C, A, B-A); if(OnSegment(ip, C, D)) newpoly.push_back(ip); // 此時必須用不含端點的“線上段上” } } return newpoly; } void cut(int x1,int y1,int x2,int y2) { newpieces.clear(); // 僅僅是一個temp 記得清空 Point A(x1,y1); Point B(x2,y2); for(int i=0;i<pieces.size();i++) { Polygon poly=pieces[i]; Polygon left=CutPolygon(poly, A, B); Polygon right=CutPolygon(poly, B, A); if(left.size()>=3) newpieces.push_back(left); if(right.size()>=3) newpieces.push_back(right); } pieces=newpieces; } //bool inCircle(Circle C,Point P) // 圓周不算 //{ // if(dcmp(Length(P-C.c)-C.r)<0) return 1; // else return 0; //} double Length2(Vector A) { return Dot(A,A); } bool inCircle(Circle C,Point P) // 圓周不算 { if(dcmp(Length2(P-C.c)-C.r*C.r)<0) return 1; else return 0; } bool CircleSegIntersection(Circle C,Point A,Point B) // 線段端點不算 { double t1,t2; vector<Point> sol; if(getLineCircleIntersection(Line(A,B-A), C, t1, t2,sol)<=1) return 0; if(dcmp(t1)>0&&dcmp(t1-1)<0) return 1; if(dcmp(t2)>0&&dcmp(t2-1)<0) return 1; return 0; } bool CirclePolyIntersection(Circle C,Polygon poly) { if(isPointInPolygon(C.c, poly)) return 1; if(inCircle(C, poly[0])) return 1; Point A,B; int n=poly.size(); for(int i=0;i<n;i++) { A=poly[i]; B=poly[(i+1)%n]; if(CircleSegIntersection(C, A, B)) return 1; if(inCircle(C, (A+B)/2)) return 1; } return 0; } void Query(Circle circle) { vector<double> ans; for(int i=0;i<pieces.size();i++) if(CirclePolyIntersection(circle, pieces[i])) { ans.push_back(fabs(PolygonArea(pieces[i]))); } sort(ans.begin(),ans.end()); printf("%d",ans.size()); for(int i=0;i<ans.size();i++) { printf(" %.2lf",ans[i]); } printf("\n"); } int main() { int n,m,l,w; Circle C; while(cin>>n>>m>>l>>w) { if(!n) break; pieces.clear(); Polygon bbox; bbox.push_back(Point(0,0)); bbox.push_back(Point(l,0)); bbox.push_back(Point(l,w)); bbox.push_back(Point(0,w)); pieces.push_back(bbox); int x1,y1,x2,y2; for(int i=0;i<n;i++) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); cut(x1,y1,x2,y2); } for(int i=0;i<m;i++) { scanf("%lf%lf%lf",&C.c.x,&C.c.y,&C.r); Query(C); } printf("\n"); } }