给出一个点的序列,按顺序做一笔画,求最后的图形把平面分成了几部分。
欧拉定理:E:线段数,V节点数,F平面数;有F+V-E==2.
所以存下来所有线段之后,求出不同的交点数,再根据这些交点找出分割后的线段数,最后又公式算出F即可。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <string> typedef double type; using namespace std; struct Point { type x,y; Point(){} Point(type a,type b) { x=a; y=b; } void read() { scanf("%lf%lf",&x,&y); } void print() { printf("%.6lf %.6lf",x,y); } }; typedef Point Vector; Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operator - (Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); } Vector operator * (Vector A,type p) { return Vector(A.x*p,A.y*p); } Vector operator / (Vector A,type 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); } const double eps=1e-10; int dcmp(double x) { if (fabs(x)<eps) return 0; else return x<0?-1:1; } bool operator == (const Point& a,const Point b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; } //atan2(x,y) :向量(x,y)的极角,即从x轴正半轴旋转到该向量方向所需要的角度。 type Dot(Vector A,Vector B) { return A.x*B.x+A.y*B.y; } type Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } type Length(Vector A) { return sqrt(Dot(A,A)); } type Angle(Vector A,Vector B) { return acos(Dot(A,B))/Length(A)/Length(B); } type 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)//单位法线,左转90度,长度归一 { 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=B-A,v2=P-A; return fabs(Cross(v1,v2))/Length(v1); } double DistanceToSegment(Point P,Point A,Point B) { if (A==B) return Length(P-A); Vector v1=B-A,v2=P-A,v3=P-B; if (dcmp(Dot(v1,v2))<0) return Length(v2); else if (dcmp(Dot(v1,v3))>0) return Length(v3); else return fabs(Cross(v1,v2))/Length(v1); } Point GetLineProjection(Point P,Point A,Point B)//P在AB上的投影 { Vector v=B-A; return A+v*(Dot(v,P-A)/Dot(v,v)); } bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2) { double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1), c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0; } bool OnSegment(Point p,Point a1,Point a2) { return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0; } double ConvexPolygonArea(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.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.0; } Point p[1020]; Point ans[123000]; int cnt; int n; int main() { // freopen("in.txt","r",stdin); int tt=0; while(~scanf("%d",&n) && n) { tt++; for (int i=0; i<n; i++) p[i].read(); n--; cnt=n; memcpy(ans,p,sizeof p); for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) { if (SegmentProperIntersection(p[i],p[i+1],p[j],p[j+1])) { ans[cnt++]=GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]); } } sort(ans,ans+cnt); cnt=unique(ans,ans+cnt)-ans; int e=n; for (int i=0; i<cnt; i++) for (int j=0; j<n; j++) { if (OnSegment(ans[i],p[j],p[j+1])) { e++; } } printf("Case %d: There are %d pieces.\n",tt,e+2-cnt); } return 0; }