版权声明:2018/4/10重启blog;转载请注明出处 https://blog.csdn.net/zhaiqiming2010/article/details/82228340
struct point
{
double x,y;
point() {}
point(double x_, double y_)
{
x = x_;
y = y_;
}
friend point operator - (const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend point operator + (const point &a,const point &b)
{
return point(a.x+b.x,a.y+b.y);
}
};
int cmp(double x)
{
if(fabs(x) < eps)
return 0;
if(x > 0)
return 1;
return -1;
}
double det(const point &a,const point &b)
{
return a.x * b.y - a.y * b.x;
}
double dot(const point &a, const point &b)
{
return a.x * b.x + a.y * b.y;
}
bool PointOnSegment(point p1, point s, point t)
{
return cmp(det(p1-s,t-s)) == 0
&& cmp(dot(p1-s,p1-t)) <= 0;
}
struct polygon
{
int n ;
point a[maxn];
polygon() {}
int point_in(point t)
{
int num = 0;
int i, d1,d2,k;
a[n]=a[0];
for(i = 0 ; i < n ; i++)
{
if(PointOnSegment(t,a[i],a[i+1]))
return 2;
k = cmp(det(a[i+1]-a[i],t-a[i]));
d1 = cmp(a[i].y-t.y);
d2 = cmp(a[i+1].y-t.y);
if(k > 0 && d1 <= 0 && d2 > 0)
num++;
if(k < 0 && d2 <= 0 && d1 > 0)
num--;
}
return num!=0;
}
} poly;