POJ2318 二分,判断点在哪条线的左边

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liufengwei1/article/details/86556467

判断一个点p在上面是s下面是e的线段左边,右手定则知道叉积小于0,即(s-p) ^ (e-p) <0

二分判断在哪条线左边就行了

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

using namespace std;

int n,m;
double L,U,R,D;
int ans[maxl];

struct point
{
	double x,y;
	point(double _x=0,double _y=0)
	{
		x=_x;y=_y;
	}
	point operator - (const point &b) const
	{
		return point(x-b.x,y-b.y);
	}
	double operator * (const point &b) const
	{
		return x*b.x+y*b.y;
	} 
	double operator ^ (const point &b) const 
	{
		return x*b.y-y*b.x;
	} 
}; 

struct line
{
	point s,e;
	double k;
	line() {}
	line(point _a,point _b)
	{
		s=_a;e=_b;
		k=atan2(e.y-s.y,e.x-s.x);
	}
}a[maxl];

inline void prework()
{
	scanf("%d%lf%lf%lf%lf",&m,&L,&U,&R,&D);
	double x1,x2;
	point p1,p2;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&x1,&x2);
		p1=point(x1,U);
		p2=point(x2,D);
		a[i]=line(p1,p2);
	}
	for(int i=0;i<=n;i++)
		ans[i]=0;
}

bool isleft(point p,int id)
{
	point p1=a[id].s-p;
	point p2=a[id].e-p;
	if( (p1 ^ p2) <0+eps)
		return true;
	else
		return false;
}

inline void mainwork()
{
	point p;
	int l,r,mid;
	for(int i=1;i<=m;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		l=1;r=n;
		while(l+1<r)
		{
			mid=(l+r)>>1;
			if(isleft(p,mid))
				r=mid;
			else
				l=mid;
		}
		if(isleft(p,l))
			ans[l-1]++;
		else if(isleft(p,r))
			ans[r-1]++;
		else
			ans[r]++;
	}
}

inline void print()
{
	for(int i=0;i<=n;i++)
		printf("%d: %d\n",i,ans[i]);
}

int main()
{
	int cas=0;
	while(~scanf("%d",&n) && n)
	{
		cas++;
		if(cas!=1)
			puts("");
		prework();
		mainwork();
		print();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/liufengwei1/article/details/86556467