POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客
下面三种情况比较特殊,特别是第三种
G++怎么交都是WA,同样的代码C++A了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0);
const double INF = 1e5; int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1:1;
} struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x,y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
}; struct Line
{
Point p,q;
Line() {};
Line(Point _p,Point _q)
{
p = _p,q = _q;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = p;
if(sgn((p-q)^(b.p-b.q)) == 0)
{
if(sgn((p-b.q)^(b.p-b.q)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((p-b.p)^(b.p-b.q))/((p-q)^(b.p-b.q));
res.x += (q.x-p.x)*t;
res.y += (q.y-p.y)*t;
return make_pair(2,res);
}
}; //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.p.x,l1.q.x) >= min(l2.p.x,l2.q.x) &&
max(l2.p.x,l2.q.x) >= min(l1.p.x,l1.q.x) &&
max(l1.p.y,l1.q.y) >= min(l2.p.y,l2.q.y) &&
max(l2.p.y,l2.q.y) >= min(l1.p.y,l1.q.y) &&
sgn((l2.p-l1.q)^(l1.p-l1.q))*sgn((l2.q-l1.q)^(l1.p-l1.q)) <= 0 &&
sgn((l1.p-l2.q)^(l2.p-l2.q))*sgn((l1.q-l2.q)^(l2.p-l2.q)) <= 0;
} Point pot[105],peg;
double rad; int main()
{
// freopen("in.txt","r",stdin);
int t;
double x1,y1,x2,y2;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L1=Line(Point(x1,y1),Point(x2,y2));
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L2=Line(Point(x1,y1),Point(x2,y2));
if(sgn(L1.p.y - L1.q.y)==0 || sgn(L2.p.y - L2.q.y)==0)
{
puts("0.00");
continue;
}
if(!inter(L1,L2))
{
puts("0.00");
continue;
}
if(sgn(L1.p.y - L1.q.y) < 0) swap(L1.p,L1.q);
if(sgn(L2.p.y - L2.q.y) < 0) swap(L2.p,L2.q);
if(inter(Line(L1.p,Point(L1.p.x,INF)),L2))
{
puts("0.00");
continue;
}
if(inter(Line(L2.p,Point(L2.p.x,INF)),L1))
{
puts("0.00");
continue;
}
pair<int,Point> pr=L1 & L2;
Point cp=pr.second;
pr=Line(L1.p,Point(INF,L1.p.y)) & L2;
double ans=fabs((L1.p-cp)^(pr.second-cp))/2;
pr=Line(L2.p,Point(INF,L2.p.y)) & L1;
ans=min(ans,fabs((L2.p-cp)^(pr.second-cp))/2);
printf("%.2lf\n",ans);
}
return 0;
}
POJ 2826 An Easy Problem? 判断线段相交的更多相关文章
- POJ 2826 An Easy Problem?!(线段交点+简单计算)
Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- POJ 2826 An Easy Problem?![线段]
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12970 Accepted: 199 ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...
- 简单几何(线段相交) POJ 2826 An Easy Problem?!
题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...
- POJ 2826 An Easy Problem?! 好的标题
受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...
随机推荐
- Docker创建镜像以及私有仓库
Docker的安装及镜像.容器的基本操作详见博客https://blog.51cto.com/11134648/2160257下面介绍Docker创建镜像和创建私有仓库的方法,详细如下: 创建镜像 创 ...
- 在Linux中通过Top运行进程查找最高内存和CPU使用率
按内存使用情况查找前15个进程,在批处理模式下为"top" 使用top命令查看有关当前状态,系统使用情况的更详细信息:正常运行时间,负载平均值和进程总数. 分类:Linux命令操作 ...
- 055.Python前端Django模型ORM
由于前面在centos实验的过程中,pymql一直有属性错误,很难排查出问题,重新做了一个ubuntu的桌面系统同时使用pycharm开发工具作为学习开发工具,具体原因是因为在项目命名出现问题,和自己 ...
- Linux中级之ansible配置(playbook)
一.playbooks 如果用模块形式一般有幂等性,如果用shell或者command没有幂等性 playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用 task ...
- IDEA 2019.2.4 破解安装教程
将下载的 IDEA 压缩包解压,找到 idealIU-2019.2.4.exe 安装文件,然后双击进行安装 安装完后不要运行,打开解压包中破解补丁与激活码文件夹,找到 jetbrains-agent. ...
- 如何像如何像 NASA 顶级程序员一样编程 — 10 条重要原则
https://www.oschina.net/news/90499/nasa-programmer-rule?from=20171112#0-qzone-1-7898-d020d2d2a4e8d1a ...
- python rpc 的实现
所谓RPC,是远程过程调用(Remote Procedure Call)的简写,网上解释很多,简单来说,就是在当前进程调用其他进程的函数时,体验就像是调用本地写的函数一般.本文实现的是在本地调用远端的 ...
- Navicat注册机报错No all pattern found! file already patched
第一步:先把注册机放入安装目录. (这一步非常关键,先不要打开桌面上安装好的快捷方式!!) 第二步:如果之前下载过,把注册表清理干净 计算机\HKEY_CURRENT_USER\SOFTWARE\Pr ...
- CVPR2020论文解读:3D Object Detection三维目标检测
CVPR2020论文解读:3D Object Detection三维目标检测 PV-RCNN:Point-Voxel Feature Se tAbstraction for 3D Object Det ...
- 如何在TVM上集成Codegen(上)
如何在TVM上集成Codegen(上) 许多常用的深度学习内核,或者提供DNNL或TensorRT等框架和图形引擎,让用户以某种方式描述他们的模型,从而获得高性能.此外,新兴的深度学习加速器也有自己的 ...