1 #include<stdio.h> 2 #include<math.h> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 struct Point { 7 double x, y; 8 Point(double x = 0, double y = 0) : x(x) , y(y) {} 9 }; 10 typedef Point Vector; 11 Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y);} 12 Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y);} 13 Vector operator * (Vector A, double P) { return Vector(A.x * P, A.y * P);} 14 Vector operator / (Vector A, double P) { return Vector(A.x / P, A.y / P);} 15 bool operator < (const Point &a, const Point &b) { 16 return a.x < b.x || (a.x == b.x && a.y < b.y); 17 } 18 const double eps = 1e-10; 19 double fabs(double x) { 20 return x > 0 ? x : -x; 21 } 22 int dcmp(double x) { 23 if(fabs(x) < eps) return 0; 24 else return x < 0 ? -1 : 1; 25 } 26 bool operator == (const Point &a, const Point &b) { 27 return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; 28 } 29 double Dot(Vector A ,Vector B) { return A.x * B.x + A.y * B.y;}//点积 30 double Length(Vector A) { return sqrt(Dot(A, A));} //计算向量长度 31 double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B));}// 计算向量夹角 32 double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } 33 double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A);} 34 Vector Rotate(Vector A, double rad) { 35 return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); 36 } 37 Vector Normal(Vector A) { 38 double L = Length(A); 39 return Vector(-A.y / L, A.x / L); 40 } 41 Point GetlineIntersection(Point P, Vector v, Point Q, Vector w) { 42 Vector u = P - Q; 43 double t = Cross(w, u) / Cross(v, w); 44 return P + v * t; 45 } 46 double DistanceToLine(Point P, Point A, Point B) { 47 Vector v1 = B - A, v2 = P - A; 48 return fabs(Cross(v1, v2) / Length(v1)); 49 } 50 double DistanceToSegment(Point P, Point A, Point B) { 51 if(A == B) return Length(P - A); 52 Vector v1 = B - A, v2 = P - A, v3 = P - B; 53 if(dcmp(Dot(v1, v2) < 0)) return Length(v2); 54 else if(dcmp(Dot(v1, v2) < 0)) return Length(v3); 55 else return fabs(Cross(v1,v2)) / Length(v1); 56 } 57 Point GetLineProjection(Point P, Point A, Point B) { 58 Vector v = B - A; 59 return A + v * (Dot(v, P - A)) / Dot(v, v); 60 } 61 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) { 62 double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), 63 c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); 64 return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; 65 } 66 bool Onsegment(Point p, Point a1, Point a2) { 67 return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; 68 } 69 double ConvexPolygonArea(Point * p, int n) { 70 double area = 0; 71 for(int i = 1; i < n - 1; ++i) 72 area += Cross(p[i] - p[0], p[i + 1] - p[0]); 73 return fabs(area / 2); 74 } 75 const int maxn = 300 + 10; 76 Point P[maxn], V[maxn * maxn]; 77 78 int main() { 79 int n, kase = 0; 80 while(scanf("%d", &n) == 1 && n) { 81 for(int i = 0; i < n; ++i) { 82 scanf("%lf%lf", &P[i].x, &P[i].y); V[i] = P[i]; 83 } 84 n--; 85 int c = n, e = n; 86 for(int i = 0; i < n; ++i) { 87 for(int j = i + 1; j < n; ++j) { 88 if(SegmentProperIntersection(P[i], P[i + 1], P[j], P[j + 1])) { 89 V[c++] = GetlineIntersection(P[i], P[i + 1] - P[i], P[j], P[j + 1] - P[j]); 90 } 91 } 92 } 93 sort(V, V + c); 94 c = unique(V, V + c) - V; 95 for(int i = 0; i < c; ++i) 96 for(int j = 0; j < n; ++j) 97 if(Onsegment(V[i], P[j], P[j + 1])) ++e; 98 printf("Case %d: There are %d pieces.\n", ++kase, e + 2 - c); 99 } 100 return 0; 101 }
浙公网安备 33010602011771号