判断一个点是否在多边形之内,我开始是使用叉乘求面积,wa了一段时间后然后发现这个算法是错误的,于是上网搜了题解,我的写法参考了这篇文章http://blog.csdn.net/lfz95/article/details/8037685
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1756
#include <stdio.h>
#include <math.h>
const double pi = acos(-1.0);
const double eps = 1e-8;
struct Node {
double x,y;
};
Node a[105],temp;
int n,m;
double cross(Node a1,Node a2,Node a3){
return (a2.x-a1.x)*(a3.y-a2.y) - (a3.x-a2.x)*(a2.y-a1.y);
}
double dist2(Node a1,Node a2){
return (a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y);
}
double get_each_digit(Node a1,Node a2,Node a3){
double t1 = dist2(a1,a2) + dist2(a1,a3) - dist2(a2,a3);
double t2 = 2 * sqrt(dist2(a1,a2)) * sqrt(dist2(a1,a3));
return acos(t1/t2);
}
double get_Digit(){
double digit = 0,tempDigit;
for(int i = 0;i<n;i++){
if(fabs(cross(temp,a[i],a[i+1]))<eps)continue;
tempDigit = get_each_digit(temp,a[i],a[i+1]);
if(cross(temp,a[i],a[i+1])<0)tempDigit = -tempDigit;
digit += tempDigit;
}
return digit;
}
void input(){
while(~scanf("%d",&n)){
for(int i = 0;i<n;i++){
scanf("%lf %lf",&a[i].x,&a[i].y);
}
a[n] = a[0];
scanf("%d",&m);
for(int i = 0;i<m;i++){
scanf("%lf %lf",&temp.x,&temp.y);
double digit = get_Digit();
if(fabs(digit)<eps)puts("No");
else puts("Yes");
}
}
}
void File(){
freopen("a.in","r",stdin);
freopen("a.out","w",stdin);
}
int main(void){
//File();
input();
return 0;
}