嘟嘟嘟

题面:给一个\(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. VS2013漂亮字体

    使用字体:Fixedsys Excelsior 3.011.首先下载字体:http://www.fixedsysexcelsior.com/ 2.安装字体:控制面板 -> 字体,复制下载的文件进 ...

  2. DynamicJasper入门

    JasperReport 在了解DynamicJasper之前不得不先了解一下jasperReport,jasperReport是基于Java的一个开源的报表工具,它使得你可以在Java环境下像其它的 ...

  3. HTTP完整请求过程

    重要申明:感谢原文作者——沐风的心,想看原文请戳这里!我借鉴了原文的页面请求流程,原文见时序图之后正文. 重要申明:感谢原文作者——一行两行,想看原文请戳这里!我借鉴了原文的完整HTTP请求流程. 增 ...

  4. input输入框限制(座机,手机号码)

    记录一下 座机input输入框: <input style="width:100px;" id="tel" type="text" o ...

  5. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  6. BZOJ3512:DZY Loves Math IV

    传送门 Sol 好神仙的题目.. 一开始就直接莫比乌斯反演然后就 \(GG\) 了 orz 题解 permui 枚举 \(n\),就是求 \(\sum_{i=1}^{n}S(i,m)\) 其中\(S( ...

  7. PHP new StdClass() 创建空对象

    PHP可以用 $object = new StdClass(); 创建一个没有成员方法和属性的空对象.很多时候,程序员们会将一些参数配置项之类的信息放在数组中使用,但是数组操作起来并不是很方便,很多时 ...

  8. .net reflector+reflexil修改编译后的dll文件

    1.用reflector打开相关的dll文件. 2.如果reflector中没有reflexil插件,点击工具栏中的Tools->Add-Ins 3.找到需要修改的文件,双击打开该文件:点击To ...

  9. Grunt入门学习之(3) -- Gruntfile具体示例

    经过前面的学习,将测试的Gruntfile整合在一起! /** * Created by Administrator on 2017/6/22. */ module.exports = functio ...

  10. apple 下安装mysql 以及 碰到的问题

    直接说过程: 1.从mysql 官网下载 mysql 5.7最新版:有 两种 一种是 dmg 的傻瓜包,一种是tar.gz的压缩包.我下载的是tar.gz的压缩包. 2.下载成功后,先试用命令sudo ...