poj2826(线段相交)
题意:用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水。
分析:看起来很简单,写起来略麻烦,先排除不能接到水的情况:
1. 两条线段不相交;
2. 其中任意一条线段水平;
3. 两条线段重合;
4. 相交的情况下,最高的端点遮住了次高的端点
最后求线段交点确定三角形并用叉积求面积。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
const double PI = acos(-1.0);
const int N = ;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
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;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为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((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= ;
}
int main()
{
int T;
Line l1,l2;
scanf("%d",&T);
while(T--)
{
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
l1=Line(Point(a,b),Point(c,d));
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
l2=Line(Point(a,b),Point(c,d));
if(sgn(l1.s.y-l1.e.y)==||sgn(l2.s.y-l2.e.y)==||!inter(l1,l2))
{
puts("0.00");
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))
{
puts("0.00");
continue;
}
if(sgn(l1.s.y-l2.s.y)<)
{
Point p=(l1&l2).second;
Point p1=(Line(l1.s,Point(,l1.s.y))&l2).second;
double ans=fabs((l1.s-p)^(p1-p))/;
printf("%.2lf\n",ans);
}
else
{
Point p=(l1&l2).second;
Point p1=(Line(l2.s,Point(,l2.s.y))&l1).second;
double ans=fabs((l2.s-p)^(p1-p))/;
printf("%.2lf\n",ans);
}
}
}
poj2826(线段相交)的更多相关文章
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
- HDU1086You can Solve a Geometry Problem too(判断线段相交)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- POJ 2653 Pick-up sticks【线段相交】
题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- hdu 1558 线段相交+并查集
题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...
- poj 1269 线段相交/平行
模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...
随机推荐
- css图片上下垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- perl 函数回调 引用$client->run(sub {$client->sync});
匿名函数引用: [root@wx03 wx]# perl a1.pl CODE(0x2077b30) test [root@wx03 wx]# cat a1.pl $ref= sub {return ...
- SharePoint 2013 "通知我"简单的功能
简单的功能 "通知我"内部列表或文档库中的主要项目.加入/删除/修改等操作,用户的E- mail通知设定功能:设置列表或文档库通知的能力,有可能设置通知为一个单一的项目.这是Sha ...
- MSSQL、C# 、Winform、ASP.NET - 数据库备份与还原模块
数据库备份还原类: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- 从svn下载项目后build path为灰色
今天从svn上下载项目后,想加入下面jar包.可是build path为灰色. 解决的方法是:1.在项目上右键properties---project facts 如图所看到的: 点击右側conver ...
- hive udaf 用maven打包运行create temporary function 时报错
用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com. ...
- 关于Hibernate数据库连接进程释放
最近手里头又一桩事情蛮好玩的,就是用Hibernate进行批处理的时候,发现连接数暴增,oracle连接进程数吓死人.解决方案:不是把连接池设置成最大,那样服务器承载不了.及时清除缓存.另外在hibe ...
- 阻塞队列BlockingQueue用法(转)
多线程环境中,通过队列可以很容易实现数据共享,比如经典的“生产者”和“消费者”模型中,通过队列可以很便利地实现两者之间的数据共享. 假设我们有若干生产者线程,另外又有若干个消费者线程.如果生产者线程需 ...
- nohup命令与&区别,jobs,fg,bg,Ctrl-Z、Ctrl-C、Ctrl-D
&方式: Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/my ...
- JavaScript快速入门(二)——JavaScript变量
变量声明 JavaScript的变量声明分为显式声明跟隐式声明. 显式声明 即带var关键字声明,例如 var example = example; 要注意JavaScript里面声明的关键字只有fu ...