题意:判断点在多边形内外,用来试模版的
#include <iostream> #include <stdio.h> #include <math.h> typedef double DIY; const DIY EPS = 0; const DIY N = 1000005; using namespace std; struct Point { DIY x,y; }; struct Segment { Point a; Point b; }; typedef Point point; Point p[100]; DIY cross(Point A,Point B,Point C) { return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x); } bool IsOnline(Point p,Segment s) { if(fabs(cross(s.a,p,s.b))<=EPS&&min(s.a.x,s.b.x)<=p.x&& p.x<=max(s.a.x,s.b.x)&&min(s.a.y,s.b.y)<=p.y&&p.y<=max(s.a.y,s.b.y)) return true; return false; } bool Segment_crossing(Segment u,Segment v) /*判断两线段是否相交*/ { return((max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&& (max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&& (max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&& (max(v.a.y,v.b.y)>=min(u.a.y,u.b.y))&& (cross(v.a,u.b,u.a)*cross(u.b,v.b,u.a)>=0)&& (cross(u.a,v.b,v.a)*cross(v.b,u.b,v.a)>=0)); } bool Inside(Point t,Point p[],int n) /*判断点p是否在以边点顺序排列的点集s所围成的多边形内*/ { Segment seg; Segment pp; int num=0; Point MM; /*定义无穷点*/ MM.y=t.y; MM.x=N; pp.a=t; pp.b=MM; /*构造一条平行于x轴的以p为端点的右射线pp*/ for(int i=0; i<n; i++) { seg.a=p[i]; seg.b=p[(i+1)%n]; if(IsOnline(t,seg)) return 1; if(fabs(seg.a.y-seg.b.y)<EPS) continue; if(IsOnline(seg.a,pp)) { if(seg.a.y>seg.b.y) num++; } else if(IsOnline(seg.b,pp)) { if(seg.b.y>seg.a.y) num++; } else if(Segment_crossing(pp,seg)) num++; } if(num%2==1) return true; return false; } int main() { Point t; int n,m; while(cin>>n) { for(int i=0; i<n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); cin>>m; while(m--) { scanf("%lf%lf",&t.x,&t.y); if(Inside(t,p,n)) puts("Yes"); else puts("No"); } } return 0; }
时间: 2024-11-05 12:19:37