-
//zoj1081 Points Within
-
//Accepted 1081 C ++ 00:00.00 396K
-
//(?)处有问题,不过还是AC了
-
#include <stdio.h>
-
#define MAXN 100
-
#define INF 1e10
-
struct point { double x,y;};
-
int n;
-
double xmult(double x1,double y1,double x2,double y2){return x1*y2-x2*y1;}
-
double xmult3(point p1,point p2,point p0)
-
{return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}
-
//判点p1、p2在线段p3p4同侧(不包括在直线线上)
-
bool same_side(point p1,point p2,point p3,point p4)
-
{
-
double dx,dy,dx1,dy1,dx2,dy2;
-
dx = p4.x-p3.x, dy = p4.y-p3.y;
-
dx1 = p1.x-p3.x, dy1 = p1.y-p3.y;
-
dx2 = p2.x-p4.x, dy2 = p2.y-p4.y;
-
return xmult(dx,dy,dx1,dy1)*xmult(dx,dy,dx2,dy2)>0.0;
-
}
-
//判p1p2与p3p4相交(包括端点)
-
bool intersect(point p1,point p2,point p3,point p4)
-
{
-
return (!same_side(p1,p2,p3,p4) && !same_side(p3,p4,p1,p2));
-
}
-
//判点q在线段p1p2上(包括端点)
-
bool dot_online(point q,point p1,point p2)
-
{
-
return xmult3(q,p1,p2)==0.0 &&
-
(q.x-p1.x)*(q.x-p2.x)<=0.0 &&
-
(q.y-p1.y)*(q.y-p2.y)<=0.0;
-
}
-
//判点q在多边形P(p[0,1,...,n-1]为P的邻接顶点)内(包括在边上),射线穿刺、奇偶校验法
-
/* Special case
-
y| __
-
| / /
-
| / .__/____//______qs
-
| / q /
-
___|____/____________/____x
-
| /__________/
-
(a) q->qs overlap the edge of P
-
-
y| __
-
| / /
-
| / /_.__//
-
| / q /
-
___|____/____________/____x
-
| /__________/
-
(b) q is on the edge of P (true)
-
-
y| __
-
| /._/_____________qs
-
| / q /____//
-
| / /
-
___|____/____________/____x
-
| /__________/
-
(c) q->qs cross the vertex of P
-
-
y| __
-
| / /
-
| / /____//
-
| / q.________/______qs
-
___|____/____________/____x
-
| /__________/
-
(d) q-qs cross the vertex of P
-
-
Sample Input
-
9 5
-
-
1 1
-
4 1
-
6 3
-
5 5
-
4 4
-
2 4
-
1 6
-
-1 6
-
-3 3
-
-
1 4(a)
-
3 4(b)
-
1 5(c)
-
1 3(d)
-
-5 3(d)
-
0
-
-
Sample Output
-
Within
-
Within
-
Within
-
Within
-
Outside
-
*/
-
bool inside_polygon(point q,point *p,int n)
-
{
-
point qs=q;
-
qs.x = INF;//construct radial q->qs
-
int cnt = 0;
-
for (int i=0; i<n; ++i){
-
int j=(i+1)%n,k=(i+2)%n;
-
if (dot_online(q,p[i],p[j])) return true;//case (b)
-
if (intersect(q,qs,p[j],p[k])) cnt++;//case (c) and common
-
//(?) if (dot_online(p[j],q,qs) && !same_side(p[i],p[k],q,qs)) cnt--;//case (d)
-
//(?) if (dot_online(p[j],q,qs) && dot_online(p[k],q,qs)) cnt--;//case (a)
-
}
-
printf ("%d",cnt);
-
return cnt%2!=0;
-
}
-
void solve()
-
{
-
int m;
-
point q,p[MAXN];
-
scanf ("%d",&m);
-
for (int i=0; i<n; ++i) scanf ("%lf %lf",&p[i].x,&p[i].y);
-
while (m--){
-
scanf ("%lf %lf",&q.x,&q.y);
-
if (inside_polygon(q,p,n))
-
printf ("Within/n");
-
else printf ("Outside/n");
-
}
-
}
-
int main()
-
{
-
#ifdef ONLINE_JUDGE
-
#else
-
freopen("1081.txt","r",stdin);
-
#endif
-
int t=0;
-
while (scanf("%d",&n)!=EOF && n){
-
if (t) printf ("/n");
-
printf ("Problem %d:/n",++t);
-
solve();
-
}
-
#ifdef ONLINE_JUDGE
-
#else
-
fclose(stdin);
-
#endif
-
return 0;
-
}
zoj1081 Points Within
最新推荐文章于 2019-02-12 20:16:37 发布