1016 - 计算几何之点与直线的关系 - TOYS(POJ 2318)

版权声明:虽然我只是个小蒟蒻但转载也请注明出处哦 https://blog.csdn.net/weixin_42557561/article/details/83097580

传送门

题意

给你一个这样的图

然后随机给你 m 个点,问落在每一个区域内的点有多少个

分析

入门题入门题

依旧是利用叉积,叉积太强了!

二分寻找并判断

gsj太厉害了,简直每次我算法的反例都可以被他找出来

鉴于他如此机智,他擅自修改我友链的事,我就不计较了……大佬高兴就好高兴就好Orz

一开始我是这样想的,每次二分寻找当前这个点的横坐标位于上方哪两个点之间,然后就直接判断就近的两条线段了

然而:::::

对于这种情况,按照我的思路这个红点将被判在4,5之间,显然,错了……

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define N 5009
using namespace std;
struct point{
	int x,y;
	point(){}
	point(int _x,int _y):x(_x),y(_y){}
	friend inline point operator +(const point &a,const point &b){
		return point(a.x+b.x,a.y+b.y);
	}
	friend inline point operator -(const point &a,const point &b){
		return point(a.x-b.x,a.y-b.y);
	}
	friend inline point operator *(int k,const point &a){
		return point(a.x*k,a.y*k);
	}
	friend inline int dot(const point &a,const point &b){
		return a.x*b.x+a.y*b.y;
	}
	friend inline int cross(const point &a,const point &b){
		return a.x*b.y-a.y*b.x;
	}
	friend inline double len(const point &a)
	{
		return sqrt(dot(a,a));
	}
	friend inline int dis(const point &a,const point &b){
		return len(a-b);
	}
}a[N],b[N];
int n,m,cnt[N];
int main(){
	while(1){
		scanf("%d",&n);
		if(!n) break;
		scanf("%d",&m);
		scanf("%d%d%d%d",&a[0].x,&a[0].y,&b[n+1].x,&b[n+1].y);
		b[0].x=a[0].x;b[0].y=b[n+1].y;
		a[n+1].x=b[n+1].x;a[n+1].y=a[0].y;
		int i,j,k;memset(cnt,0,sizeof(cnt));
		for(i=1;i<=n;++i){
			int xx,yy;
			scanf("%d%d",&xx,&yy);
			a[i].x=xx;a[i].y=a[0].y;
			b[i].x=yy;b[i].y=b[n+1].y;
		}
		for(i=1;i<=m;++i){
			point toy;
			scanf("%d%d",&toy.x,&toy.y);
			int l=0,r=n+1,ans=0;
			while(l<=r){
				int mid=l+r>>1;
				if(cross(toy-b[mid],a[mid]-b[mid])>=0) ans=mid,l=mid+1;
				else r=mid-1;
			}
			cnt[ans]++;
		}
		for(i=0;i<=n;++i)	printf("%d: %d\n",i,cnt[i]);
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42557561/article/details/83097580