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 ...
随机推荐
- MySQL---2、安装与部署
1.MySQL下载 MySQL版本的选择MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.MySQL Enterprise Edition 企业版本,需付费,可以 ...
- jQuery基础---动画效果
内容摘要: 1.显示.隐藏 2.滑动.卷动 3.淡入.淡出 4.自定义动画 5.列队动画方法 6.动画相关方法 7.动画全局属性 发文不易,转载请注明出处~ 一.显示.隐藏 jQuery 中显示方法 ...
- C#生成Excel
需要引用MyXls.SL2.dll的类库: 下载地址:http://sourceforge.net/projects/myxls/ 命名空间using org.in2bits.MyXls: //创建表 ...
- [javaSE] GUI(对话框Dialog)
对话框不能单独存在,依赖于窗体,有显示标题,有模式 获取Dialog对象,new出来,构造参数:Frame对象,String的标题,模式 窗体内部的内容,Label对象,Button对象,调用Dial ...
- Java包装类、拆箱和装箱详解
转载:https://www.cnblogs.com/ok932343846/p/6749488.html 虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程, ...
- 开博缘由 , 可点下看看 http://www.cnblogs.com/jshare
记录日常用中用到.遇到的问题 实现过程,仅供参考 不定时更新 ------------------- 之前看过一个文章,大概说的是开发和用到的过的代码,可以提取出一些代码片段,长时间下来,你会发现部分 ...
- django开发博客(1) 入门
现在正式开始博客开发 1.安装django1.4 如果你使用的是fedoraDVD版,安装时选择了web开发组建,这一步可以省略,因为它自带django环境 django下载地址 https://ww ...
- 图像去噪算法:NL-Means和BM3D
图像去噪是非常基础也是非常必要的研究,去噪常常在更高级的图像处理之前进行,是图像处理的基础.可惜的是,目前去噪算法并没有很好的解决方案,实际应用中,更多的是在效果和运算复杂度之间求得一个平衡,再一次验 ...
- Alaya Webdav Server 0.0.10 发布
Alaya Webdav Server 0.0.10 修复了很多 bug,Webdav 'Copy' 可以使用了. Alaya 是一个提供 WebDAV 支持的 Web 服务器,支持 HTTPS 和 ...
- org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JJ]]
1. 如果webapps下有同名的文件,会出现以上错误: 2.解决办法: 查看项目的目录结构是否正确,对于servlet项目,WEB-INF下应该有一个classes目录,存放编译好的.class文件 ...