SGU 124. Broken line 射线法 eps的精准运用,计算几何 难度:3
124. Broken line time limit per test: 0.25 sec. There is a closed broken line on a plane with sides parallel to coordinate axes, without self-crossings and self-contacts. The broken line consists of K segments. You have to determine, whether a given point with coordinates (X0,Y0) is inside this closed broken line, outside or belongs to the broken line. Input The first line contains integer K (4 Ј K Ј 10000) - the number of broken line segments. Each of the following N lines contains coordinates of the beginning and end points of the segments (4 integerxi1,yi1,xi2,yi2; all numbers in a range from -10000 up to 10000 inclusive). Number separate by a space. The segments are given in random order. Last line contains 2 integers X0 and Y0 - the coordinates of the given point delimited by a space. (Numbers X0, Y0 in a range from -10000 up to 10000 inclusive). Output The first line should contain: INSIDE - if the point is inside closed broken line, OUTSIDE - if the point is outside, BORDER - if the point belongs to broken line. Sample Input 4 Sample Output INSIDE |
因为线条简单,所以随便射线法就行了,射线的"稍微移动",也就是对目标区间做半开半闭处理,可以用eps实现,很巧妙 |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-9;
struct pnt{
double x,y;
pnt():x(0),y(0){}
pnt(double tx,double ty):x(tx),y(ty){}
}p[20000][2],aim;
int n;
bool onborder(int ind){
if(fabs(p[ind][0].y-p[ind][1].y)>eps){
int low=min(p[ind][0].y,p[ind][1].y);
int high=max(p[ind][0].y,p[ind][1].y);
if(fabs(aim.x-p[ind][0].x)>eps)return false;
return aim.y>low+eps&&aim.y+eps<high;
}
else {
int low=min(p[ind][0].x,p[ind][1].x);
int high=max(p[ind][0].x,p[ind][1].x);
if(fabs(aim.y-p[ind][0].y)>eps)return false;
return aim.x+eps>low&&aim.x<high+eps;
}
}
bool cross(int ind){
if(fabs(p[ind][0].y-p[ind][1].y)<eps)return false;
if(aim.x+eps>p[ind][0].x)return false;
int low=min(p[ind][0].y,p[ind][1].y);
int high=max(p[ind][0].y,p[ind][1].y);
return aim.y+eps>low&&eps+aim.y<high;//ATTENTION aim.y>=low&&aim<high
} int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf",&p[i][0].x,&p[i][0].y,&p[i][1].x,&p[i][1].y);
}
scanf("%lf%lf",&aim.x,&aim.y);
int cnt=0;
for(int i=0;i<n;i++){
if(onborder(i)){puts("BORDER");return 0;}
else if(cross(i))cnt++;
}
if(cnt&1)puts("INSIDE");
else puts("OUTSIDE");
return 0;
}
SGU 124. Broken line 射线法 eps的精准运用,计算几何 难度:3的更多相关文章
- SGU 124.Broken line
时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...
- Broken line - SGU 124(判断点与多边形的关系)
题目大意:RT 分析:构造一条射线,如果穿越偶数条边,那么就在多边形外面,如果穿越奇数条边,那么就在多边形里面. 代码如下: ===================================== ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- 射线法(1190 - Sleepwalking )
题目:http://lightoj.com/volume_showproblem.php?problem=1190 参考链接:https://blog.csdn.net/gkingzheng/arti ...
- Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...
- WebGL模型拾取——射线法
今天要把WebGL中一个非常重要的算法记录下来——raycaster射线法拾取模型.首先我们来了解一下为什么要做模型拾取,我们在做webgl场景交互的时候经常要选中场景中的某个模型,比如鼠标拖拽旋转, ...
- Revit API射线法读取空间中相交的元素
Revit API提供根据射线来寻找经过的元素.方法是固定模式,没什么好说.关键代码:doc.FindReferencesWithContextByDirection(ptStart, (ptEnd ...
- POJ3182 The Grove[射线法+分层图最短路]
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 904 Accepted: 444 Descripti ...
- matlab练习程序(射线法判断点与多边形关系)
依然是计算几何. 射线法判断点与多边形关系原理如下: 从待判断点引出一条射线,射线与多边形相交,如果交点为偶数,则点不在多边形内,如果交点为奇数,则点在多边形内. 原理虽是这样,有些细节还是要注意一下 ...
随机推荐
- Python3基础 if嵌套示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 卸载vs2017
卸载enterprise版本 Microsoft.FSharp.SDK.Core卸载失败Package 'Microsoft.FSharp.SDK.Core,version=15.7.20180605 ...
- 谈谈java中的final关键字
知识点:final(最终的)关键字修饰类.方法.属性 1.final修饰类:那么这个就无法被继承,如String类.StringBuffer类.System类 2.final修饰方法:被修饰的方法不能 ...
- Vim打开文件中文乱码如何解决?
今天想找一下之前的随机数组demo复习,在源文件src里用vim打开.java文件,中文出现乱码 vim也是,选项里没有直接选择设置utf-8之类的 查了下网上,http://easwy.com/bl ...
- org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
原创:转载请注明出处 1.异常情况 org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected ...
- NYOJ 16 矩形嵌套(经典DP)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- React Native 组件之TouchableHightLight
TouchableHighlight 这个组件一般是用于封装视图 给视图添加事件“触摸按下”(类似于click事件) <TouchableHighlight onPress={this._onP ...
- python input输入元素相加
sum= number= while True: : break number=int(input('数字0为结束程序,请输入数字: ')) sum+=number print('目前累加的结果为: ...
- java 使用正则判断是不是一个数字
public class Numeric { public static void main(String[] args) { String string = "-1234.15" ...
- vscode python3 配置生成任务
一直用sublime,但是ubuntu下输入中文有问题,解决起来太麻烦: pycharm太重.虚拟机一开+Chrome打开10几个页面,然后再运行pycharm,静音轻薄笔记本CPU和8G内存基本都占 ...