嘟嘟嘟

题面:给一个\(n\)个点的多边形和\(m\)个点,判断每一个点是否在多边形内。

解法:射线法。

就是从这个点引一条射线,如果与多边形有奇数个交点,则在多边形内部。

那么只用枚举每一条边,然后判断这条边与射线有无交点。为了方便,射线为水平的。然后可以用叉积判断三点共线,以及多边形的两个端点纵坐标的大小关系。

但要注意一些特殊情况,比如有一个交点是多边形的顶点,所以为了避免重复统计,需要规定交在每一条边的下断点还是上端点。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m; struct Vec
{
int x, y;
db operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
friend int dot(const Vec& A, const Vec& B)
{
return A.x * B.x + A.y * B.y;
}
};
struct Point
{
int x, y;
Vec operator - (const Point& oth)const
{
return (Vec){x - oth.x, y - oth.y};
}
}A[maxn], P; bool judge()
{
int cnt = 0;
A[n + 1] = A[1];
for(int i = 1; i <= n; ++i)
{
int d = (P - A[i]) * (P - A[i + 1]);
if(!d && dot(A[i] - P, A[i + 1] - P) <= 0) return 1; //点在边上
int d1 = A[i].y - P.y, d2 = A[i + 1].y - P.y;
if(d > 0 && d1 >= 0 && d2 < 0) cnt ^= 1;
if(d < 0 && d1 < 0 && d2 >= 0) cnt ^= 1;
}
return cnt;
} int main()
{
int cnt = 0;
while(scanf("%d", &n) && n)
{
if(++cnt != 1) enter;
printf("Problem %d:\n", cnt);
m = read();
for(int i = 1; i <= n; ++i) A[i].x = read(), A[i].y = read();
for(int i = 1; i <= m; ++i)
{
P.x = read(); P.y = read();
puts(judge() ? "Within" : "Outside");
}
}
return 0;
}

ZOJ1081 Points Within的更多相关文章

  1. ZOJ1081 Points Within 点和多边形的位置关系

    ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...

  2. ZOJ1081:Points Within——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1081 题目大意:给定一个点数为 n 的多边形,点按照顺序给出,再给出 m ...

  3. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  4. [LeetCode] Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. LeetCode:Max Points on a Line

    题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...

  6. K closest points

    Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...

  7. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  8. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  9. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

随机推荐

  1. js 千分位符号 正则方法

    function toThousands(num) {    return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');}

  2. 2017年12月24日 JS跟Jquery基础

    js基础 alert();confirm(); 基础语法:与C#一致数据类型及类型转换var (string,decimal)parseInt()parseFloat();isNaN(); 运算符:数 ...

  3. centos文件/文件夹操作-检查磁盘、内存、cpu使用情况-vi操作命令

    Part1:CentOS文件/文件夹操作 1.新建文件夹 即创建目录 mkdir 文件名 新建一个名为test的文件夹在home下 vi source1 mkdir /home/test 注意:当创建 ...

  4. Android中自定义弹出PopupWindow后返回按键失效的问题解决

    PopupWindow出现之后,默认的是所有的操作都无效的,除了HOME键.而且是可以操作后面的界面的.想要锁定后面的界面,很简单,只需要让PopupWindow是focusable的:popupWi ...

  5. mybatis之typehandles

    mybatis之typehandles 无论是Mybatis在预处理语句PreparedStatement中设置一个参数时,还是从结果集中取出一个值时,都会用类型处理器将获取的值以合适的方式转换成ja ...

  6. 并发模型之Future设计模式

    一.Futrue模式 客户端发送一个长时间的请求,服务端不需等待该数据处理完成便立即返回一个伪造的代理数据(相当于商品订单,不是商品本身),用户也无需等待,先去执行其他的若干操作后,再去调用服务器已经 ...

  7. Java 集合:迭代器(Iterator, Iterable)

    Iterator接口 public interface Iterator<E> { boolean hasNext(); E next(); void remove(); } 访问元素前需 ...

  8. Node.js IO处理输入和回显,以及当今web应用程序的发展史

    1.关于Node.js IO处理输入和回显 在Windows终端或者CD中输入   echo  'I must learn about Node.js' 结果将刚刚输入的   echo  'I mus ...

  9. PoPo数据可视化周刊第4期

    PoPo数据可视化 聚焦于Web数据可视化与可视化交互领域,发现可视化领域有意思的内容.不想错过可视化领域的精彩内容, 就快快关注我们吧 :) 微信号:popodv_com   由于国庆节的原因,累计 ...

  10. 新建虚拟机,每次都提示无法连接虚拟设备 ide1:0

    处理方式:看到了这个老哥http://www.cnblogs.com/dean-du/p/6888513.html的博客,发现问题是一样的,所以记录一下. 将虚拟机设置中的CD/DVD选项中的连接更改 ...