POJ 2826 An Easy Problem?!(线段交点+简单计算)
Description

Your mission is to calculate how much rain these two boards can collect.
Input
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x1, y1, x2, y2, x3, y3, x4, y4. (x1, y1), (x2, y2) are the endpoints of one board, and (x3, y3), (x4, y4) are the endpoints of the other one.
Output
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; const double EPS = 1e-;
const double PI = acos(-1.0);//3.14159265358979323846 inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} struct Point {
double x, y;
Point() {}
Point(double x, double y): x(x), y(y) {}
void read() {
scanf("%lf%lf", &x, &y);
}
bool operator < (const Point &rhs) const {
if(y != rhs.y) return y < rhs.y;
return x < rhs.x;
}
Point operator + (const Point &rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
Point operator * (const int &b) const {
return Point(x * b, y * b);
}
Point operator / (const int &b) const {
return Point(x / b, y / b);
}
double length() const {
return sqrt(x * x + y * y);
}
Point unit() const {
return *this / length();
}
};
typedef Point Vector; double dist(const Point &a, const Point &b) {
return (a - b).length();
} double cross(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
}
//ret >= 0 means turn left
double cross(const Point &sp, const Point &ed, const Point &op) {
return sgn(cross(sp - op, ed - op));
} double area(const Point& a, const Point &b, const Point &c) {
return fabs(cross(a - c, b - c)) / ;
} struct Seg {
Point st, ed;
Seg() {}
Seg(Point st, Point ed): st(st), ed(ed) {}
void read() {
st.read(); ed.read();
}
};
typedef Seg Line; bool isIntersected(const Point &s1, const Point &e1, const Point &s2, const Point &e2) {
return (max(s1.x, e1.x) >= min(s2.x, e2.x)) &&
(max(s2.x, e2.x) >= min(s1.x, e1.x)) &&
(max(s1.y, e1.y) >= min(s2.y, e2.y)) &&
(max(s2.y, e2.y) >= min(s1.y, e1.y)) &&
(cross(s2, e1, s1) * cross(e1, e2, s1) >= ) &&
(cross(s1, e2, s2) * cross(e2, e1, s2) >= );
} bool isIntersected(const Seg &a, const Seg &b) {
return isIntersected(a.st, a.ed, b.st, b.ed);
} bool isParallel(const Seg &a, const Seg &b) {
return sgn(cross(a.ed - a.st, b.ed - b.st)) == ;
} //return Ax + By + C =0 's A, B, C
void Coefficient(const Line &L, double &A, double &B, double &C) {
A = L.ed.y - L.st.y;
B = L.st.x - L.ed.x;
C = L.ed.x * L.st.y - L.st.x * L.ed.y;
} Point intersection(const Line &a, const Line &b) {
double A1, B1, C1;
double A2, B2, C2;
Coefficient(a, A1, B1, C1);
Coefficient(b, A2, B2, C2);
Point I;
I.x = - (B2 * C1 - B1 * C2) / (A1 * B2 - A2 * B1);
I.y = (A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1);
return I;
} bool isEqual(const Line &a, const Line &b) {
double A1, B1, C1;
double A2, B2, C2;
Coefficient(a, A1, B1, C1);
Coefficient(b, A2, B2, C2);
return sgn(A1 * B2 - A2 * B1) == && sgn(A1 * C2 - A2 * C1) == && sgn(B1 * C2 - B2 * C1) == ;
} /*******************************************************************************************/ Seg a, b;
int n; double solve() {
if(!isIntersected(a, b)) return ;
Seg tmp(Point(, ), Point(, ));
if(isParallel(a, tmp) || isParallel(b, tmp) || isParallel(a, b)) return ;
if(a.st.y > a.ed.y) swap(a.st, a.ed);
if(b.st.y > b.ed.y) swap(b.st, b.ed);
Point O = intersection(a, b);
if(b.ed < a.ed) swap(a, b);
if(sgn(a.ed.x - O.x) == sgn(b.ed.x - O.x) && (sgn(b.ed.x - O.x) * sgn(cross(b.ed, O, a.ed)) >= ) &&
sgn(fabs(b.ed.x - O.x) - fabs(a.ed.x - O.x)) >= ) return ;
Point A = a.ed;
tmp = Seg(A, Point(A.x + , A.y));
Point B = intersection(tmp, b);
return area(A, B, O);
} int main() {
scanf("%d", &n);
for(int i = ; i < n; ++i) {
a.read(), b.read();
printf("%.2f\n", solve() + EPS);
}
}
POJ 2826 An Easy Problem?!(线段交点+简单计算)的更多相关文章
- POJ 2826 An Easy Problem?![线段]
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12970 Accepted: 199 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- POJ 2826 An Easy Problem?! 好的标题
受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...
- 简单几何(线段相交) POJ 2826 An Easy Problem?!
题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...
- POJ 2826 An Easy Problem!(简单数论)
Description Have you heard the fact "The base of every normal number system is 10" ? Of co ...
- POJ 2826 An Easy Problem?! --计算几何,叉积
题意: 在墙上钉两块木板,问能装多少水.即两条线段所夹的中间开口向上的面积(到短板的水平线截止) 解法: 如图: 先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上 ...
- POJ 1152 An Easy Problem! (取模运算性质)
题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- Spring注解配置(1)——@Autowired
@Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法.在使用@Autowired之前,我们对一个b ...
- 关于document.write()加载JS等静态资源 和 异步async加载JS
现流行浏览器对于静态资源的预加载 传统的浏览器,对于静态资源加载,会阻塞 HTML 解析器的线程进行,无论内联还是外链. 例如: <script src="test1.js" ...
- Jquery之倒计时计算
setInterval(); function setDate(setTime){ var date = new Date();//获取系统当前时间 )+)+"-"+date. ...
- 关于CoreLocation定位服务的简单使用
在我们发微博,发表空间内容,以及在朋友圈发表动态的时候,会发现有一个位置信息的控件.iOS中是如何定位我们的位置信息的呢?基于此写一个小Demo,供大家参考使用. 在iOS中,用于定位时需要我们导入以 ...
- 分享一个hybrid框架ionic
ionic 是一个 HTML5 应用程序开发框架. 可以使用 HTML.CSS 和 Javascript 构建接近原生体验的移动应用程序.具有速度快,界面现代化.美观等特点.下面一起看一下如何使用 安 ...
- Spring Security学习笔记(一)
认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...
- ckeditor + ckfinder + oss存储
ckeditor 与 ckfinder 的整合方法 网上有很多,这里我也就不说了. (主要是以前整合的现在忘记咋弄的了0.0) 我这里整合后直接使用js代码 <script type=&quo ...
- ELK的端口以及加入x-pack的密码问题
ElasticSearch的端口: http://localhost:9200 http://localhost:9200/_plugin/head Kibana的端口: http://localho ...
- Django项目中关于redis包版本的坑
1.环境 python:3.6 django:1.11.8 redis:3.2.1 2.遇到的问题 报错:redis.exceptions.DataError: Invalid input of ty ...
- linux普通用户su root切换提示没有文件或目录
1. 首先进入单用户模式: 1). ubuntu : 上述情况可以在grub界面选择第二项修复,但没有grub可以参考: 1.重启ubuntu,随即长按shirft进入grub菜单: 2.选择reco ...