124. Broken line

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

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
0 0 0 3
3 3 3 0
0 3 3 3
3 0 0 0
2 2

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的更多相关文章

  1. SGU 124.Broken line

    时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...

  2. Broken line - SGU 124(判断点与多边形的关系)

    题目大意:RT 分析:构造一条射线,如果穿越偶数条边,那么就在多边形外面,如果穿越奇数条边,那么就在多边形里面. 代码如下: ===================================== ...

  3. LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...

  4. 射线法(1190 - Sleepwalking )

    题目:http://lightoj.com/volume_showproblem.php?problem=1190 参考链接:https://blog.csdn.net/gkingzheng/arti ...

  5. 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 ...

  6. WebGL模型拾取——射线法

    今天要把WebGL中一个非常重要的算法记录下来——raycaster射线法拾取模型.首先我们来了解一下为什么要做模型拾取,我们在做webgl场景交互的时候经常要选中场景中的某个模型,比如鼠标拖拽旋转, ...

  7. Revit API射线法读取空间中相交的元素

    Revit API提供根据射线来寻找经过的元素.方法是固定模式,没什么好说.关键代码:doc.FindReferencesWithContextByDirection(ptStart, (ptEnd  ...

  8. POJ3182 The Grove[射线法+分层图最短路]

    The Grove Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 904   Accepted: 444 Descripti ...

  9. matlab练习程序(射线法判断点与多边形关系)

    依然是计算几何. 射线法判断点与多边形关系原理如下: 从待判断点引出一条射线,射线与多边形相交,如果交点为偶数,则点不在多边形内,如果交点为奇数,则点在多边形内. 原理虽是这样,有些细节还是要注意一下 ...

随机推荐

  1. Asterisk1.8 转码策略分析

    最近在修改asterisk转码和编码协商的问题,发现asterisk的转码策略的选择还是有些问题的(基于1.8.9.3版本).——————————————相关的CLI命令转码路径的调试命令:core ...

  2. Log4j将不同Package的日志输出到不同的文件的方法

    随着项目规模的越来越大,会不断的引入新的模块,不同的模块都会打印自己的日志,最后就造成日志根本没法查看,比如我自己的项目中,就存在以下这些日志: 接收外界消息的日志.对外发送消息的日志: 后台常驻线程 ...

  3. HTML基本格式

    <html> <head> <title>放置文章标题</title> <meta http-equiv="Content-Type&q ...

  4. Delphi XE5 for Android (二)

    按照Embarcadero的说法:Windows平台只能使用VCL,Windows.iOS和Android都可以使用FMX,并且VCL和FMX不能同时在一个程序中.FMX的用法与VCL有一定的差异,先 ...

  5. DSDS,双模,双卡,双待,单待,双通,单通,概念及相互关系?【转】

    本文转载自:https://blog.csdn.net/dirk_it/article/details/7178058?utm_source=blogxgwz9 DSDS:双卡双待 DualSimDu ...

  6. 再谈vim中多窗口的编辑 ctrl+w+H窗口位置最大化和互换等操作

    参考:http://blog.csdn.net/shuangde800/article/details/11430659 很好 鼠标在各个窗口间循环移动: ctrl+w+(小写的 hjkl), &qu ...

  7. ubuntu12 root账户自动登录

    Ubuntu为了系统安全,root帐号的密码是随机的,如果临时需要提升至root权限以执行一些命令,需要使用sudo命令.产线上有几台使用Ubuntu的机器,因为使用者不固定,并且执行程序时需要使 用 ...

  8. C++作业:Circle_area

    Github链接: Circle_area 代码: main.cpp #include "circle_area.h" #include <iostream> #inc ...

  9. PYTHON HTML.PARSER库学习小结--转载

    前段时间,一朋友让我做个小脚本,抓一下某C2C商城上竞争对手的销售/价格数据,好让他可以实时调整自己的营销策略.自己之前也有过写爬虫抓某宝数据的经历,实现的问题不大,于是就答应了.初步想法是利用pyh ...

  10. c语言 数组合并

    #include<stdio.h> int main() { int m,n,i,j,k; printf("Enter no. of elements in array1:\n& ...