POJ 3130: How I Mathematician Wonder What You Are! 半平面交

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

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 
x1y1
x2y2

xnyn

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


这是一道优美的半平面交裸题

写的过程简直是对蒟蒻计算几何功力的升华


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef double db;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=60;
const db eps=1e-8;

struct point
{
	db x,y;
	
	friend point operator +(const point &a,const point &b)
	{return (point){a.x+b.x,a.y+b.y};}
	
	friend point operator -(const point &a,const point &b)
	{return (point){a.x-b.x,a.y-b.y};}
	
	friend db cross(const point &x,const point &y,const point &bas)
	{
		point a(x-bas),b(y-bas);
		return a.x*b.y-a.y*b.x;
	}
}p[N];

int scnt;
struct segment
{
	point X,Y;db angle;
	
	friend bool operator <(const segment &a,const segment &b)
	{
		if(abs(a.angle-b.angle)<eps) return cross(a.Y,a.X,b.X)>0;
		return a.angle<b.angle;
	}
	
	friend point get_node(const segment &a,const segment &b)
	{
		point res;
		db s[2];
		s[0]=cross(a.X,a.Y,b.X);
		s[1]=cross(a.Y,a.X,b.Y);
		res.x=(b.Y.x*s[0]+b.X.x*s[1])/(s[0]+s[1]);
		res.y=(b.Y.y*s[0]+b.X.y*s[1])/(s[0]+s[1]);
		return res;
	}
	
	friend bool check(const segment &a,const segment &b,const segment &c)
	{
		point tmp=get_node(a,b);
		return cross(c.X,c.Y,tmp)<eps;
	}
}seg[N];

inline void add_seg(const point &a,const point &b)
{
	seg[++scnt].X=a,seg[scnt].Y=b;
	seg[scnt].angle=atan2(b.y-a.y,b.x-a.x);
}

int n;

int q[N];

int hpi()
{
	sort(seg+1,seg+1+n);
	register int i,tot(1),head(0),tail(2);
	for(i=2;i<=n;++i)
		if(seg[i].angle-seg[tot].angle>eps)
			seg[++tot]=seg[i];
	q[0]=1;q[1]=2;
	for(i=3;i<=tot;++i)
	{
		while(head<tail-1 && check(seg[q[tail-1]],seg[q[tail-2]],seg[i])) tail--;
		while(head<tail-1 && check(seg[q[head]],seg[q[head+1]],seg[i])) head++;
		q[tail++]=i;
	}
	while(head<tail-1 && check(seg[q[tail-1]],seg[q[tail-2]],seg[q[head]])) tail--;
	while(head<tail-1 && check(seg[q[head]],seg[q[head+1]],seg[q[tail-1]])) head++;
	return tail-head;
}

int main()
{
	register int i;
	while(1)
	{
		scnt=0;
		n=read();
		if(!n) break;
		for(i=1;i<=n;++i)
			p[i].x=read(),p[i].y=read();
		p[n+1]=p[1];
		for(i=1;i<=n;++i)
			add_seg(p[i],p[i+1]);
		hpi()>2 ? puts("1") : puts("0");
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值