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练习程序(射线法判断点与多边形关系)
依然是计算几何. 射线法判断点与多边形关系原理如下: 从待判断点引出一条射线,射线与多边形相交,如果交点为偶数,则点不在多边形内,如果交点为奇数,则点在多边形内. 原理虽是这样,有些细节还是要注意一下 ...
随机推荐
- C# 获取当前IIS请求地址
using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...
- 安装PYthon+Kivy环境(记录)
在线翻译 https://www.bing.com/translator/ Cython 0.27 发布了.准确说Cython是单独的一门语言,专门用来写在Python里面import用的扩展库.实际 ...
- url rewrite导致的500.19 0x8007000d
https://stackoverflow.com/questions/13532447/http-error-500-19-iis-7-5-error-0x8007000d It seems you ...
- SPA (单页应用程序)
单页Web应用 编辑 单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.单页应用程序 (SPA) 是加载单个HTML 页面并在用户与应用程 ...
- VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?
本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...
- python3.7 安装pyqt5
pip install pyqt5
- 正则表达式提取HTML中img标签的src地址
一般来说一个 HTML 文档有很多标签,比如“”.“”.“”等, 想把文档中的 img 标签提取出来并不是一件容易的事. 由于 img 标签样式变化多端,使提取的时候用程序寻找并不容易. 于是想要寻找 ...
- STL_std::iterator
1. VC6里面 看到,std::iterator 就是一个指针,但是 vs2010中貌似不是这样(感觉像是一个类...具体是啥还不太确定...)... 2.
- Unity 4.x 资源加载
using UnityEngine; using System.Collections; using System.IO; public class LoadResource : MonoBehavi ...
- 关于一致性hash详细
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...