求一笔画图形构成的的面有多少,用欧拉公式 V+F-E=2;(V顶点数,F面数,E边数);
关键求V,E;
#include <cstdio> #include <iostream> #include <algorithm> #include <complex> using namespace std; //#define e 2.718281828459 const int maxn = 91000; typedef complex<double> point; typedef point Vector; double Dot(Vector a,Vector b){return real(conj(a)*b);} double Cross(Vector a,Vector b){return imag(conj(a)*b); } //double exp(point te){return pow(e,Dot(te,te));} Vector Rotate(Vector a,double rad){ return a*exp(point(0,rad));} point read_point(){ double x,y; scanf("%lf %lf",&x,&y); return point(x,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)); } point GetLineIntersection(point a,Vector v,point b,Vector w){ Vector u=a-b; double t=Cross(w,u)/Cross(v,w); return a+v*t; } const double eps=1e-10; int dcmp(double x){ if(fabs(x)<eps) return 0; else return x<0? -1:1; } 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; } point p[maxn],V[maxn]; int cmp(const point a,const point b){ double ax=real(a),ay=imag(a); double bx=real(b),by=imag(b); return dcmp(ax-bx)<0||(dcmp(ax-bx)==0&&dcmp(ay-by)<0); } int main() { cout<<SegmentProperIntersection(point(0,0),point(1,1),point(0,2),point(2,0)); int n,kase=0; while(scanf("%d",&n)==1&&n){ for(int i=0;i<n;i++){ p[i]=read_point(); } n--; int v=0; for(int i=0;i<n;i++){ V[v++]=p[i]; } 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])) V[v++]=GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]); } sort(V,V+v,cmp); int m=unique(V,V+v)-V; v=m; int e=n; for(int i=0;i<v;i++) for(int j=0;j<n;j++){ if(OnSegment(V[i],p[j],p[j+1])) e++; } printf("Case %d: There are %d pieces.\n",++kase,e+2-v); } return 0; }