您的位置:首页 > 其它

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: