ZOJ1081

给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外

在多边形外的点引一条射线必然穿过多边形的两条边

而在多边形内的点则不一定。

当然凹多边形有特殊情况 但是总能找到对应位置关系的边来抵消

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const double eps=1e-9; int cmp(double x)
{
if(fabs(x)<eps)return 0;
if(x>0)return 1;
else return -1;
} const double pi=acos(-1.0); inline double sqr(double x)
{
return x*x;
} struct point
{
double x,y;
point (){}
point (double a,double b):x(a),y(b){}
void input()
{
scanf("%lf%lf",&x,&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);
}
friend bool operator ==(const point &a,const point &b)
{
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
friend point operator *(const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator*(const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator /(const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
double norm()
{
return sqrt(sqr(x)+sqr(y));
}
}; struct line
{
point a,b;
line(){};
line(point x,point y):a(x),b(y)
{ }
};
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;
} double dist(const point &a,const point &b)
{
return (a-b).norm();
} point rotate_point(const point &p,double A)
{
double tx=p.x,ty=p.y;
return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
} bool parallel(line a,line b)
{
return !cmp(det(a.a-a.b,b.a-b.b));
} bool line_joined(line a,line b,point &res)
{
if(parallel(a,b))return false;
double s1=det(a.a-b.a,b.b-b.a);
double s2=det(a.b-b.a,b.b-b.a);
res=(s1*a.b-s2*a.a)/(s1-s2);
return true;
} bool pointonSegment(point p,point s,point t)
{
return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
} void PointProjLine(const point p,const point s,const point t,point &cp)
{
double r=dot((t-s),(p-s))/dot(t-s,t-s);
cp=s+r*(t-s);
} struct polygon_convex
{
vector<point>P;
polygon_convex(int Size=0)
{
P.resize(Size);
}
}; bool comp_less(const point &a,const point &b)
{
return cmp(a.x-b.x)<0||cmp(a.x-b.x)==0&&cmp(a.y-b.y)<0; } polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end(),comp_less);
a.erase(unique(a.begin(),a.end()),a.end());//删去重复点
int m=0;
for(int i=0;i<a.size();i++)
{
while(m>1&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
int k=m;
for(int i=int(a.size())-2;i>=0;--i)
{
while(m>k&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
res.P.resize(m);
if(a.size()>1)res.P.resize(m-1);
return res;
} bool is_convex(vector<point> &a)
{
for(int i=0;i<a.size();i++)
{
int i1=(i+1)%int(a.size());
int i2=(i+2)%int(a.size());
int i3=(i+3)%int(a.size());
if((cmp(det(a[i1]-a[i],a[i2]-a[i1]))*cmp(det(a[i2]-a[i1],a[i3]-a[i2])))<0)
return false;
}
return true;
}
int containO(const polygon_convex &a,const point &b)
{
int n=a.P.size();
point g=(a.P[0]+a.P[n/3]+a.P[2*n/3])/3.0;
int l=0,r=n;
while(l+1<r)
{
int mid=(l+r)/2;
if(cmp(det(a.P[l]-g,a.P[mid]-g))>0)
{
if(cmp(det(a.P[l]-g,b-g))>=0&&cmp(det(a.P[mid]-g,b-g))<0)r=mid;
else l=mid;
}else
{
if(cmp(det(a.P[l]-g,b-g))<0&&cmp(det(a.P[mid]-g,b-g))>=0)l=mid;
else r=mid;
}
}
r%=n;
int z=cmp(det(a.P[r]-b,a.P[l]-b))-1;
if(z==-2)return 1;
return z;
} bool circle_in_polygon(point cp,double r,polygon_convex &pol)
{ polygon_convex pp=convex_hull(pol.P);
if(containO(pp,cp)!=1)return false;
for(int i=0;i<pol.P.size();i++)
{
int j;
if(i<pol.P.size()-1)j=i+1;
else j=0;
point prol;
PointProjLine(cp,pol.P[i],pol.P[j],prol);
double dis;
if(pointonSegment(prol,pol.P[i],pol.P[j]))dis=dist(prol,cp);
else dis=min(dist(cp,pol.P[i]),dist(pol.P[j],cp));
if(cmp(dis-r)==-1)return false;
}
return true;
} const int maxn=1e+6; point po[maxn+10]; double area(point a[],int n)
{
double sum=0;
a[n]=a[0];
for(int i=0;i<n;i++)
sum+=det(a[i+1],a[i]);
return sum/2.;
} int Point_in(point a[],int n,point t)
{
int num=0,i,d1,d2,k;
a[n]=a[0];
for(int 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;
} point pp[100];
int main()
{freopen("t.txt","r",stdin);
int n,m;
int tot=1;
while(scanf("%d",&n)&&n>0)
{
if(tot>1)printf("\n");
printf("Problem %d:\n",tot++);
scanf("%d",&m);
for(int i=0;i<n;i++)
pp[i].input();
point t;
for(int i=0;i<m;i++)
{
t.input();
int ans=Point_in(pp,n,t);
if(ans>0)printf("Within\n");
else printf("Outside\n");
}
}
return 0;
}

  

ZOJ1081 Points Within 点和多边形的位置关系的更多相关文章

  1. Cupid's Arrow---hdu1756(判断点与多边形的位置关系 模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1756 题意:中文题,套模板即可: /* 射线法:判断一个点是在多边形内部,边上还是在外部,时间复杂度为 ...

  2. LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...

  3. UVA 10256 The Great Divide (凸包,多边形的位置关系)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ...

  4. Intersecting Lines (计算几何基础+判断两直线的位置关系)

    题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...

  5. POJ 1269 Intersecting Lines (判断直线位置关系)

    题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...

  6. POJ 1269 Intersecting Lines(判断两直线位置关系)

    题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...

  7. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

  8. JS魔法堂:判断节点位置关系

    一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二 ...

  9. POJ 2398 - Toy Storage 点与直线位置关系

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 Descr ...

随机推荐

  1. python3 监控代码变化 自动重启 提高开发效率

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Michael Liao' import os, sys, time, sub ...

  2. uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)

    题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...

  3. 3.2.8 sed 的运作

        sed 的工作方式相当直接.命令行上的每个文件会依次打开与读取.如果没有文件,则使用标准输入,文件名“-”(单个破折号)可用于表示标准输入.       [many@avention Desk ...

  4. 动态创建div(鼠标放上显示二维码)

    最近的微信大行其道.各个网站上都给出的微信验证码,进行手机扫描加入. 怎么创建类似与点击鼠标弹出一个浮动的div显示二维码的这种效果. 1.首先制作好这样的图片,写css样式 <style ty ...

  5. hihoCoder#1082 然而沼跃鱼早就看穿了一切

    原题地址 字符串匹配+替换 注意替换串和原串长度是不等的,所以替换完还要进行收缩 可以顺带练习一下KMP 代码: #include <iostream> #include <cstr ...

  6. noip模拟赛 入阵曲

    分析:其实很容易想到O(n^3m^3)的算法,枚举x1,x2,y1,y2,再统计一下和.求和可以用前缀和,能优化到O(n^2m^2),能得到60分.对于特殊性质的点,求一下a[i][j]与k的最小公倍 ...

  7. 在mysql数据库中,文章表设计有啥好的思路

    Q: 用mysql设计一张文章表,不知道有啥好的思路! 我是这样的,应为考虑附件和图片,所以我的文章表除了有varchar(1000)的文章内容,还设置了个Bolb接收附件和图片. 我用的是mysql ...

  8. codevs——1017 乘积最大

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Desc ...

  9. Ubuntu 16.04安装QtCharts时报错:'qtConfig' is not a recognized test function.

    错误: 'qtConfig' is not a recognized test function. 解决方法: 其实5.9分支的版本有问题,转成5.7分支即可. git clone https://g ...

  10. jmeter的master-slave模式

    要求: 1.相同的jmeter版本 2.最好相同的java版本 jmeter可以通过master-slave的方式实现更大的并发,但是作为master的机器将会消耗更多的资源,因为所有的slave的压 ...