zoj1081 判断点是否在多边形内,在边上也是在内

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxl 110
#define eps 1e-8

using namespace std;

inline int sgn(double x)
{
	if(x>-eps && x<eps) return 0;
	if(x<0) return -1;
	else	return 1;
}

struct point
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	inline double norm()
	{
		return sqrt(x*x+y*y);
	}
};
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}

struct line
{
	point s,e;
	double k;
	line(point a=point(),point b=point())
	{
		s=a;e=b;
		k=atan2(e.y-s.y,e.x-s.x);
	}
};

inline bool point_on_seg(point &p,line a)
{//在端点处点积为0,此时算在直线上 
	return sgn(det(p-a.s,p-a.e))==0 && sgn(dot(p-a.s,p-a.e))<=0;
}

struct polygon
{
	int n;
	point a[maxl];
	polygon(){}
	inline double perimeter()
	{
		double sum=0;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
			sum+=(a[i+1]-a[i]).norm();
		return sum;
	}
	inline double area()
	{
		double sum=0;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
			sum+=det(a[i+1],a[i]);
		return sum/2;
	}
	inline int point_in(point t)
	{//0 out 1 in 2 on  
		int num=0,d1,d2,k;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
		{
			if(point_on_seg(t,line(a[i],a[i+1])))
				return 2;
			k=sgn(det(a[i+1]-a[i],t-a[i]));
			d1=sgn(a[i].y-t.y);
			d2=sgn(a[i+1].y-t.y);
			if(k>0 && d1<=0 && d2>0) num++;
			if(k<0 && d2<=0 && d1>0) num++; 
		}
		return num%2!=0;
	}
}d;

int n,m,cas;
point a[maxl];

inline void prework()
{
	scanf("%d",&m);
	d.n=n;
	for(int i=1;i<=n;i++)
		scanf("%lf%lf",&d.a[i].x,&d.a[i].y);
}

inline void mainwork()
{
	++cas;
	if(cas>1)
		puts("");
	printf("Problem %d:\n",cas);
	point p;
	for(int i=1;i<=m;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		if(d.point_in(p)==0)
			puts("Outside");
		else
			puts("Within");
	}
}

inline void print()
{
}

int main()
{
	while(~scanf("%d",&n) && n)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

从这个点做一条水平向右的射线,统计射线与多边形相交的情况,如果相交次数为偶数,则说明点在多边形外,否在就在多边形内

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值