【HDOJ】1756 Cupid's Arrow
2015-11-01 11:30
471 查看
图论,点在多边形内部的判定。
/* 1756 */ #include <iostream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <algorithm> #include <cstdio> #include <cmath> #include <ctime> #include <cstring> #include <climits> #include <cctype> #include <cassert> #include <functional> #include <iterator> #include <iomanip> using namespace std; //#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int> #define stpii set<pair<int, int> > #define mpii map<int,int> #define vi vector<int> #define pii pair<int,int> #define vpii vector<pair<int,int> > #define rep(i, a, n) for (int i=a;i<n;++i) #define per(i, a, n) for (int i=n-1;i>=a;--i) #define clr clear #define pb push_back #define mp make_pair #define fir first #define sec second #define all(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define lson l, mid, rt<<1 #define rson mid+1, r, rt<<1|1 const double eps = 1e-10; const int maxn = 105; int dcmp(double x) { if (fabs(x) < eps) return 0; return x<0 ? -1:1; } typedef struct Point { double x, y; Point() {} Point(double x_, double y_): x(x_), y(y_) {} } Point; Point poly[maxn]; int n; Point operator-(Point A, Point B) { return Point(A.x-B.x, A.y-B.y); } double Dot(Point A, Point B) { return A.x*B.x + A.y*B.y; } double Cross(Point A, Point B) { return A.x*B.y - A.y*B.x; } bool OnSegment(Point P, Point A, Point B) { return dcmp(Cross(A-P, B-P))==0 && dcmp(Dot(A-P, B-P))<=0; } bool isPointInPolygon(Point p, Point *poly) { int wn = 0; rep(i, 0, n) { if (OnSegment(p, poly[i], poly[(i+1)%n])) return true; int k = dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i])); int d1 = dcmp(poly[i].y - p.y); int d2 = dcmp(poly[(i+1)%n].y - p.y); if (k>0 && d1<=0 && d2>0) ++wn; if (k<0 && d2<=0 && d1>0) --wn; } return wn!=0; } int main() { ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif int m; bool flag; Point p; while (scanf("%d",&n) != EOF) { rep(i, 0, n) scanf("%lf %lf", &poly[i].x, &poly[i].y); scanf("%d", &m); while (m--) { scanf("%lf %lf", &p.x, &p.y); flag = isPointInPolygon(p, poly); puts(flag ? "Yes":"No"); } } #ifndef ONLINE_JUDGE printf("time = %d.\n", (int)clock()); #endif return 0; }
相关文章推荐
- C语言-文件操作的函数
- 安装varnish遇到的小问题
- Android绘图机制(二) ViewGroup类
- 树基础总结(算法导论)
- Des 加密处理 iOS 和 安卓 与服务器 处理时 遇到的 补位问题
- windows系统清理与维护
- linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
- ZUFE OJ 2301 GW I (3)
- App 引导界面
- Hibernate一级缓存【Hibernate】
- 十月,再见;你好,十一月
- Springmvc异常处理【Springmvc】
- 数组求和与课后作业
- 6种绝招让你花1分钟增寿20岁
- 用HTML/JS/PHP方式实现页面延时跳转
- usb转rs232驱动无效,已经电脑自动扫描检测硬件改动.设备无法运行错误代码10
- 黑马程序员——面向对象(二)
- 关于鸿学金信原始股的一个坏消息与好消息
- Springmvc-freemarker 【Springmvc】
- ubuntu15.04 mysql5.6.27编码格式配置