poj2826(细节,计算几何)
题目链接:https://vjudge.net/problem/POJ-2826
题意:平面中摆两根木棍,雨水从上垂直下落,问木棍中能乘多少水。
思路:
细节很多,坑QAQ。。
首先不相交时肯定为0.00,然后其中有一条木棍是水平的也不行,最后是如果开口被堵住了也不行(通过判断其中一根木棍l1的上端点向上引射线是否与l2相交)。
最后输出答案时需要加上eps,因为会出现-0.0和0.0的情况,不然会wa到你哭!double的精度就是迷,我也想不通-0.0是怎么出现的,算是经验了,以后碰到输出double的情况注意下-0.0。
AC code:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std; const double eps=1e-;
const double inf=1e20; int sgn(double x){
if(abs(x)<eps) return ;
if(x<) return -;
return ;
} struct Point{
double x,y;
Point(){}
Point(double xx,double yy):x(xx),y(yy){}
Point operator + (const Point& b)const{
return Point(x+b.x,y+b.y);
}
Point operator - (const Point& b)const{
return Point(x-b.x,y-b.y);
}
double operator * (const Point& b)const{
return x*b.x+y*b.y;
}
double operator ^ (const Point& b)const{
return x*b.y-b.x*y;
}
//绕原点旋转角度b(弧度值),后x、y的变化
void transXY(double b){
double tx=x,ty=y;
x=tx*cos(b)-ty*sin(b);
y=tx*sin(b)+ty*cos(b);
}
}; struct Line{
Point s,e;
Line(){}
Line(Point ss,Point ee){
s=ss,e=ee;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == )
{
if(sgn((s-b.e)^(b.s-b.e)) == )
return make_pair(,res);//重合
else return make_pair(,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(,res);
}
};
//判断线段相交
bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&
max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&
max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&
max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=&&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=;
} double dis(Point a,Point b){
return sqrt((b-a)*(b-a));
} int T; int main(){
scanf("%d",&T);
double x1,yy1,x2,yy2,x3,yy3,x4,yy4;
Line l1,l2;
while(T--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&yy1,&x2,&yy2,&x3,&yy3,&x4,&yy4);
l1=Line(Point(x1,yy1),Point(x2,yy2));
l2=Line(Point(x3,yy3),Point(x4,yy4));
if(!inter(l1,l2)){
printf("0.00\n");
continue;
}
if(sgn(l1.s.y-l1.e.y)==||sgn(l2.s.y-l2.e.y)==){
printf("0.00\n");
continue;
}
if(sgn(l1.s.y-l1.e.y)<) swap(l1.s,l1.e);
if(sgn(l2.s.y-l2.e.y)<) swap(l2.s,l2.e);
if(inter(Line(l1.s,Point(l1.s.x,)),l2)||inter(Line(l2.s,Point(l2.s.x,)),l1)){
printf("0.00\n");
continue;
}
pair<int,Point> pr=l1&l2;
Point p=pr.second;
double ans1,ans2;
pr=l1&Line(Point(,l2.s.y),l2.s);
Point p1=pr.second;
ans1=abs((l2.s-p)^(p1-p))/;
pr=l2&Line(Point(,l1.s.y),l1.s);
Point p2=pr.second;
ans2=abs((l1.s-p)^(p2-p))/;
printf("%.2f\n",eps+min(ans1,ans2));
}
return ;
}
poj2826(细节,计算几何)的更多相关文章
- 计算几何细节梳理&模板
点击%XZY巨佬 向量的板子 #include<bits/stdc++.h> #define I inline using namespace std; typedef double DB ...
- poj2826 An Easy Problem?!(计算几何)
传送门 •题意 两根木块组成一个槽,给定两个木块的两个端点 雨水竖直下落,问槽里能装多少雨水, •思路 找不能收集到雨水的情况 我们令线段较高的点为s点,较低的点为e点 ①两条木块没有交点 ②平行或重 ...
- poj2826 An Easy Problem?!【计算几何】
含[三点坐标计算面积].[判断两线段是否有交点].[求线段交点]模板 An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Tot ...
- ACM 计算几何中的精度问题(转)
http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...
- 洛谷CF1071E Rain Protection(计算几何,闵可夫斯基和,凸包,二分答案)
洛谷题目传送门 CF题目传送门 对于这题,我无力吐槽. 虽然式子还是不难想,做法也随便口胡,但是一些鬼畜边界情况就是判不对. 首先显然二分答案. 对于每一个雨滴,它出现的时刻我们的绳子必须落在它上面. ...
- [NOI2005]月下柠檬树(计算几何+积分)
题目描述 李哲非常非常喜欢柠檬树,特别是在静静的夜晚,当天空中有一弯明月温柔 地照亮地面上的景物时,他必会悠闲地坐在他亲手植下的那棵柠檬树旁,独自思 索着人生的哲理. 李哲是一个喜爱思考的孩子,当他看 ...
- 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...
- 【洛谷5286】[HNOI2019] 鱼(计算几何)
点此看题面 大致题意: 给你\(n\)个点,让你求鱼形图的数量. 核心思路 首先,考虑到\(n\)这么小,我们可以枚举线段\(AD\),再去找符合条件的\(BC,EF\). 然后,不难发现\(BC\) ...
- 2019暑期金华集训 Day6 计算几何
自闭集训 Day6 计算几何 内积 内积不等式: \[ (A,B)^2\le (A,A)(B,B) \] 其中\((A,B)\)表示\(A\cdot B\). (好像是废话?) 叉积 \[ A\tim ...
随机推荐
- (转)实验文档3:在kubernetes集群里集成Apollo配置中心
使用ConfigMap管理应用配置 拆分环境 主机名 角色 ip HDSS7-11.host.com zk1.od.com(Test环境) 10.4.7.11 HDSS7-12.host.com zk ...
- Spring Cloud Eureka(七):DiscoveryClient 源码分析
1.本节概要 上一节文章主要介绍了Eureka Client 的服务注册的流程,没有对服务治理进行介绍,本文目的就是从源码角度来学习服务实例的治理机制,主要包括以下内容: 服务注册(register) ...
- 观察者模式在android网络监控下的运用
github:https://github.com/shonegg/NetMonitor 一.对观察者模式的理解: 1.观察者模式,又叫发布-订阅(Publish/Subscribe)模式,定义的是对 ...
- vue的一些随笔
一.点击路由后的样式,可以在路由文件index.js中设置 再在样式里面设置active的类名对应的样式. ———————————————————————————————————————————— 二 ...
- global 和 nonlocal关键字
当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字了. def fun(): global num1 num1=2 print("函数内修改后num1=&qu ...
- Multiline f-strings
多行字符串使用fstring需要注意每行都要加fstring >>> name = "Eric" >>> profession = " ...
- 学习ArrayList的扩容机制
基于jdk8 1.首先我们看new ArrayList中 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDA ...
- VUE -- Identifier 'n_type' is not in camel case
Identifier 'n_type' is not in camel case 参数名的 `_` 去掉就好了
- RGB-D(深度图像) & 图像深度
RGB-D(深度图像) 深度图像 = 普通的RGB三通道彩色图像 + Depth Map 在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...
- vector swap
#include <iostream>#include <vector>#include <list>#include <deque> using na ...