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

传送门

 

题意

给你一个这样的图

然后随机给你 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;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值