poj 3130 How I Mathematician Wonder What You Are! (半平面交)

原创 2017年01月03日 08:55:48

How I Mathematician Wonder What You Are!
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3668   Accepted: 1977

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.

n  
x1 y1
x2 y2

xn yn

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 (xiyi)–(xi + 1yi + 1) (i = 1, …, n − 1) and the line segment (xnyn)–(x1y1) 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.

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.

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:判断多边形是否有内核

题解:半平面交

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100
#define eps 1e-8
#define inf 1000000
using namespace std;
struct vector {
	double x,y;
	vector (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],p[N];
int n,m;
typedef vector point;
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 t){
	return vector (a.x*t,a.y*t);
}
void init()
{
	m=0;
	p[m++]=point(inf,inf);
	p[m++]=point(-inf,inf);
	p[m++]=point(-inf,-inf);
	p[m++]=point(inf,-inf);
}
double cross(vector a,vector b){
	return a.x*b.y-a.y*b.x;
}
point glt(point a,point a1,point b,point b1)
{
	vector v=a1-a; vector w=b1-b;
	vector u=a-b;
	double t=cross(w,u)/cross(v,w);
	return a+v*t;
}
void cut(point a,point b)
{
	point tmp[N];
	int cnt=0;
	for (int i=0;i<m;i++){
		double c=cross(b-a,p[i]-a);
		double d=cross(b-a,p[(i+1)%m]-a);
		if (c<=0) tmp[cnt++]=p[i];
		if (c*d<0)
		 tmp[cnt++]=glt(a,b,p[i],p[(i+1)%m]);
	}
	m=cnt;
	for (int i=0;i<cnt;i++) p[i]=tmp[i];
}
int main()
{
	freopen("a.in","r",stdin);
	while (true){
		scanf("%d",&n);
		if (!n) break;
		for (int i=1;i<=n;i++)
		 scanf("%lf%lf",&a[i].x,&a[i].y);
		init();
		a[n+1]=a[1];
		for (int i=2;i<=n+1;i++){
			cut(a[i],a[i-1]);
			if (!m) break;
		}
		if (m) printf("1\n");
		else printf("0\n");
	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj 3130 How I Mathematician Wonder What You Are!(半平面交)

How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memory Limit: 65536K T...

POJ 3130 How I Mathematician Wonder What You Are! 半平面交求多边形内核是否存在

题目大意:定义一种多边形,叫做星形多边形。这种多边形就是有内核的多边形。给出一些多边形,问是否是星形多边形。 思路:利用半平面交求解。PS:我的第一个多边形内核的代码不对。。一定要看这个,这...

poj3130How I Mathematician Wonder What You Are!+半平面交

半平面交

POJ 3130-How I Mathematician Wonder What You Are!(计算几何-星形-半平面交逆时针模板)

How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memory Limit: 65536K T...
  • MIKASA3
  • MIKASA3
  • 2017年04月16日 20:41
  • 281

POJ3130How I Mathematician Wonder What You Are!【半平面交判断内核】

Language: Default How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memor...

POJ 3130 & ZOJ 2820 How I Mathematician Wonder What You Are!(半平面相交 多边形是否有核)

POJ 3130 & ZOJ 2820 How I Mathematician Wonder What You Are!(半平面相交 多边形是否有核 模板)...

poj 3130 How I Mathematician Wonder What You Are!

How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memory Limit: 65536K ...
  • wwwzys
  • wwwzys
  • 2011年09月05日 20:28
  • 874

POJ 3130 How I Mathematician Wonder What You Are!

自己把半平面交的代码敲了一下,顺便理解了一下原理 #include #include const double eps=1e-8; struct Point { double x,y; ...
  • lj94093
  • lj94093
  • 2015年01月18日 23:50
  • 270

POJ 3130 How I Mathematician Wonder What You Are!

#include #include #include #include #include #include #include #include using namespace std; #defin...
  • utoppia
  • utoppia
  • 2013年08月22日 11:38
  • 430

POJ 3130 || How I Mathematician Wonder What You Are!

The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and ...
  • FXXKI
  • FXXKI
  • 2015年05月12日 20:41
  • 348
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 3130 How I Mathematician Wonder What You Are! (半平面交)
举报原因:
原因补充:

(最多只允许输入30个字)