POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
Description
After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.
The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained in F. Such a point C is called a center of F. To get accustomed to the definition let’s see some examples below.
The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.
Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.
Input
The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.
You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xi, yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.
Output
For each dataset, output “1
” if the polygon is star-shaped and “0
” otherwise. Each number must be in a separate line and the line should not contain any other characters.
Sample Input
6
66 13
96 61
76 98
13 94
4 0
45 68
8
27 21
55 14
93 12
56 95
15 48
38 46
51 65
64 31
0
Sample Output
1
0
这两个题,都是输入一个简单多边形,判断是否存在核,套半平面交模版即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 const double eps = 1e-8; 8 const int maxn = 55; 9 10 int dq[maxn], top, bot, pn, order[maxn], ln; 11 struct Point { 12 double x, y; 13 } p[maxn]; 14 15 struct Line { 16 Point a, b; 17 double angle; 18 } l[maxn]; 19 20 int dblcmp(double k) { 21 if (fabs(k) < eps) return 0; 22 return k > 0 ? 1 : -1; 23 } 24 25 double multi(Point p0, Point p1, Point p2) { 26 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); 27 } 28 29 bool cmp(int u, int v) { 30 int d = dblcmp(l[u].angle-l[v].angle); 31 if (!d) return dblcmp(multi(l[u].a, l[v].a, l[v].b)) > 0; //大于0取向量左半部分为半平面,小于0,取右半部分 32 return d < 0; 33 } 34 35 void getIntersect(Line l1, Line l2, Point& p) { 36 double dot1,dot2; 37 dot1 = multi(l2.a, l1.b, l1.a); 38 dot2 = multi(l1.b, l2.b, l1.a); 39 p.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot2 + dot1); 40 p.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot2 + dot1); 41 } 42 43 bool judge(Line l0, Line l1, Line l2) { 44 Point p; 45 getIntersect(l1, l2, p); 46 return dblcmp(multi(p, l0.a, l0.b)) < 0; //大于小于符号与上面cmp()中注释处相反 47 } 48 49 void addLine(double x1, double y1, double x2, double y2) { 50 l[ln].a.x = x1; l[ln].a.y = y1; 51 l[ln].b.x = x2; l[ln].b.y = y2; 52 l[ln].angle = atan2(y2-y1, x2-x1); 53 order[ln] = ln; 54 ln++; 55 } 56 57 void halfPlaneIntersection() { 58 int i, j; 59 sort(order, order+ln, cmp); 60 for (i = 1, j = 0; i < ln; i++) 61 if (dblcmp(l[order[i]].angle-l[order[j]].angle) > 0) 62 order[++j] = order[i]; 63 ln = j + 1; 64 dq[0] = order[0]; 65 dq[1] = order[1]; 66 bot = 0; 67 top = 1; 68 for (i = 2; i < ln; i++) { 69 while (bot < top && judge(l[order[i]], l[dq[top-1]], l[dq[top]])) top--; 70 while (bot < top && judge(l[order[i]], l[dq[bot+1]], l[dq[bot]])) bot++; 71 dq[++top] = order[i]; 72 } 73 while (bot < top && judge(l[dq[bot]], l[dq[top-1]], l[dq[top]])) top--; 74 while (bot < top && judge(l[dq[top]], l[dq[bot+1]], l[dq[bot]])) bot++; 75 } 76 77 bool isThereACore() { 78 if (top-bot > 1) return true; 79 return false; 80 } 81 82 int main() 83 { 84 //freopen("de.txt","r",stdin); 85 int i; 86 while (scanf ("%d", &pn) && pn) { 87 for (i = 0; i < pn; i++) 88 scanf ("%lf%lf", &p[i].x, &p[i].y); 89 for (ln = i = 0; i < pn-1; i++) 90 addLine(p[i].x, p[i].y, p[i+1].x, p[i+1].y); 91 addLine(p[i].x, p[i].y, p[0].x, p[0].y); 92 halfPlaneIntersection(); 93 /*输出这个核 94 Point poly[55]; 95 int k = 0; 96 for (int i=bot;i<=top;++i) 97 poly[k++] = p[i]; 98 for (int i=bot;i<=top;++i) 99 printf("%.3f %.3f\n",poly[i].x,poly[i].y); 100 */ 101 if (isThereACore()) printf ("1\n"); 102 else printf ("0\n"); 103 } 104 return 0; 105 }