poj 2318 TOYS 点 直线 位置关系

题目描述:http://poj.org/problem?id=2318
判断点与直线位置关系的题

#include <cstdio>
#include <algorithm>
using namespace std;

struct point {
	int x,y;
};

struct line {
	point a,b;
};

int xmult(line seg, point p) {
	return (seg.b.x - p.x) * (seg.a.y - p.y) - (seg.a.x - p.x) * (seg.b.y - p.y);
}

bool comp(point p1, point p2) {
	if(p1.x != p2.x)
		return p1.x < p2.x;
	return p1.y < p2.y;
}

int main() {
	//freopen("in.txt", "r", stdin);
	int n, m;
	point lu, rl;
	point toys[5005];
	line segs[5005];
	int cnt[5005];
	while(scanf("%d", &n) != EOF) {
		if(n == 0)
			break;
		for(int i = 0; i < n  + 1; i++)
			cnt[i] = 0;
		scanf("%d %d %d %d %d", &m, &lu.x, &lu.y, &rl.x, &rl.y);
		segs[0].a = lu;
		segs[0].b.x = lu.x;
		segs[0].b.y = rl.y;
		segs[n + 1].a.x = rl.x;
		segs[n + 1].a.y = lu.y;
		segs[n + 1].b = rl;
		
		int u, l;
		for(int i = 1; i <= n; i++) {
			scanf("%d %d", &u, &l);
			segs[i].a.x = u;
			segs[i].a.y = lu.y;
			segs[i].b.x = l;
			segs[i].b.y = rl.y;
		}
		for(int i = 0; i < m; i++)
			scanf("%d %d", &toys[i].x, &toys[i].y);
			
		int k = 0;
        sort(toys, toys + m, comp);
		for(int i = 0; i < m; i++) {
			for(int j = 0; j <= n; j++) {   //避免重新从j = 0 开始搜索
				if(xmult(segs[j], toys[i]) <= 0
					&& xmult(segs[j + 1], toys[i]) >= 0) {
					cnt[j]++;
                    k = j;  //避免重新从j = 0 开始搜索
					break;
				}
			}
		}
		for(int i = 0; i <= n; i++)
			printf("%d: %d\n", i, cnt[i]);
		
		printf("\n");
		
	}
	return 0;
}





你可能感兴趣的:(C++,c,C#,J#)