几何运算(叉乘---判断是否在凸四边形内的判断(二分))
2015-05-29 16:02
260 查看
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <string.h> //poj2318 #include <queue> //几何运算(叉乘---判断是否在凸四边形内的判断(二分)) #define N 5010 using namespace std; struct zuobiao { int x1, y1, x2, y2; }p ; int b, c, a ; int check(int t) //叉乘的运算x1*y2-x2*y1(叉乘不满足交换律,所以可以判断其在左还是右) { int x1, y1; x1=b-p[t].x2; y1=c-p[t].y2; return p[t].x1*y1-p[t].y1*x1; } int main() { int n, m, t, x1, y1, x2, y2, k, g, h, T=0; while(1) { if(T) printf("\n"); else T=1; scanf("%d", &n); if(!n)break; scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2); p[0].x1=0; p[0].y1=y1; p[0].x2=x1; p[0].y2=y2; for(t=1; t<=n; ++t) { scanf("%d%d", &p[t].x1, &p[t].x2); p[t].y1=y1; p[t].y2=y2; p[t].x1=p[t].x1-p[t].x2; p[t].y1=p[t].y1-p[t].y2; } memset(a, 0, sizeof(a)); for(t=0; t<m; ++t) { scanf("%d%d", &b, &c); if(b<x1||b>x2||c>y1||c<y2)continue; if(check(1)>0) { a[0]++; continue; } if(check(n)<0) { a ++; continue; } int l=1, r=n, mid; h=0; //h用来储存上一次变换符号后叉乘的值 while(l<=r) //二分 { mid=(l+r)>>1; k=check(mid); g=check(mid-1); if(k>0) { if(g<0) { a[mid-1]++; break; } if(h<0) { r=mid; h=k; continue; } r=mid-1; h=k; } else if(k<0) { if(h>0) { l=mid; h=k; continue; } l=mid+1; h=k; } else if(k==0||g==0)break; } } for(t=0; t<=n; ++t) { printf("%d: %d\n", t, a[t]); } } return 0; }
相关文章推荐
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板
- poj 2485 Highways
- poj 1511 Invitation Cards
- 【网络流-最大流-Dinic-建模】POJ3281 Dining:Pascal 解法
- poj1042
- poj1080
- poj1088