ZOJ1081 Points Within
题面:给一个\(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的更多相关文章
- ZOJ1081 Points Within 点和多边形的位置关系
ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...
- ZOJ1081:Points Within——题解
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1081 题目大意:给定一个点数为 n 的多边形,点按照顺序给出,再给出 m ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [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. ...
- 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 ...
- K closest points
Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...
- 【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 ...
- 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. ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
随机推荐
- EF 查询数据不读取缓存的解决办法
EF查询(不使用缓存):Set<T>().AsNoTracking() 今天工作中发现一个很妖的问题,修改产品界面,修改数据后,数据库的值发生变化,感觉掉坑里了. 然后发现读取对象的方法是 ...
- jQuery Ajax实例 ($.ajax_$.post_$.get)
Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. $.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到j ...
- OC与JS交互之WKWebView
上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能.稳定性.功能大幅度提升,也更好的支 ...
- SpringBoot之整合MyBatis
今天了解一下SpringBoot如何与我们最常用的ORM框架Mybatis整合. 一. 需要在pom.xml文件里加入mybatis的依赖 <dependency> <groupId ...
- 【Java学习经历系列-1】19岁的我,没遇见生命中的她,却遇见了java
[写在前面]正直青春年少的你,遇到了你的她了吗?还是你也和我们今天的主人公一样,在最美好的年级,正在为你的初衷努力着,坚持着,奔波着..... 作者:李伟 我的黑客时代 01 大学专业是电子信息工 ...
- 设计模式 UML & java code
A: 创造性模式 1. 工厂方法模式(FactoryMethod) 1.1 类图 1.2 代码1 public interface Pet { public String petSound(); } ...
- iView定制主题报错问题的解决方法
按照iView官网来是这样的: 1. 在main.js当前目录下新建themes文件夹,里面新建一个叫blue.less的文件 2. 在mian.js里面引入blue.less文件 3. blue.l ...
- (WCF) 利用WCF实现两个Process之间的通讯。
目的: 实现两个独立的Process 之间的通讯. 实现思路: 建立一个WCF Service,然后将其Host到一个Console 程序中,然后在另外一个Console程序中引用WCF的Servic ...
- Ubuntu install TensorFlow 1.10 + CUDA 9.2 + cuDNN 7.2
为了装TensorFlow 1.10 下面升级一下系统的软件环境 NVIDIA DRIVER 去官网下载最新的linux驱动 http://www.nvidia.com/Download/in ...
- js不管条件是否成立都要进行变量提升
### 不管条件是否成立都要进行变量提升 > 不管条件是否成立,判断体中出现的var/function都会进行变量提升:但是在最新浏览器版本当中,function声明的变量只能提前声明,不能定义 ...