# [POJ3130]How I Mathematician Wonder What You Are!（半平面交）

535人阅读 评论(0)

## 代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1005

const double inf=1e6;
const double eps=1e-8;
int dcmp(double x)
{
if (x<=eps&&x>=-eps) return 0;
return (x>0)?1:-1;
}
struct Vector
{
double x,y;
Vector(double X=0,double Y=0)
{
x=X,y=Y;
}
};
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 a) {return Vector(A.x*a,A.y*a);}
bool operator == (Vector A,Vector B) {return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;}

int n,cnt,ncnt;
double x,y;
Point p[N],poly[N],npoly[N];

double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double Len(Vector A)
{
return sqrt(Dot(A,A));
}
bool insLS(Point A,Point B,Point C,Point D)
{
Vector v=B-A,w=C-A,u=D-A;
return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
void init()
{
cnt=0;
poly[++cnt]=Point(-inf,-inf);
poly[++cnt]=Point(inf,-inf);
poly[++cnt]=Point(inf,inf);
poly[++cnt]=Point(-inf,inf);
}
void halfp(Point A,Point B)
{
ncnt=0;
Point C,D,ip;
for (int i=1;i<=cnt;++i)
{
C=poly[i%cnt+1];
D=poly[(i+1)%cnt+1];
if (dcmp(Cross(B-A,C-A))>=0)
npoly[++ncnt]=C;
if (insLS(A,B,C,D))
{
ip=GLI(A,B-A,C,D-C);
npoly[++ncnt]=ip;
}
}
cnt=ncnt;
for (int i=1;i<=cnt;++i)
poly[i]=npoly[i];
}
int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
for (int i=1;i<=n;++i)
{
scanf("%lf%lf",&x,&y);
p[i]=Point(x,y);
}
init();
for (int i=1;i<=n;++i)
halfp(p[i%n+1],p[(i+1)%n+1]);
if (cnt) puts("1");
else puts("0");
}
}
0
0

个人资料
• 访问：643041次
• 积分：17905
• 等级：
• 排名：第630名
• 原创：1151篇
• 转载：7篇
• 译文：0篇
• 评论：190条
联系方式
QQ：1209628538 加好友请备注省份和ID
E-mail：fiona_2000@126.com
友链
最新评论