简单几何(线段相交) POJ 2826 An Easy Problem?!
题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积
分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后看看是否高的点覆盖了低的点,用叉积判断方向,其他的情况见网上的解释。貌似没有什么卡精度的数据。最后膜拜楼教主,难以望其项背。。。

/************************************************
* Author :Running_Time
* Created Time :2015/10/30 星期五 18:36:27
* File Name :POJ_2826.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) {
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point {
double x, y;
Point () {}
Point (double x, double y) : x (x), y (y) {}
Point operator + (const Point &r) const {
return Point (x + r.x, y + r.y);
}
Point operator - (const Point &r) const {
return Point (x - r.x, y - r.y);
}
Point operator * (double p) const {
return Point (x * p, y * p);
}
Point operator / (double p) const {
return Point (x / p, y / p);
}
bool operator < (const Point &r) const {
return x < r.x || (!dcmp (x - r.x) && y < r.y);
}
bool operator == (const Point &r) const {
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector;
Point read_point(void) {
double x, y;
scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
}
Point line_line_inter(Point p, Vector V, Point q, Vector W) {
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
bool can_inter(Point a1, Point a2, Point b1, Point b2) {
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) <= 0 && dcmp (c3 * c4) <= 0;
}
double area_triangle(Point a, Point b, Point c) {
return fabs (cross (b - a, c - a)) / 2.0;
} int main(void) {
int T; scanf ("%d", &T);
Point a1, a2, b1, b2;
while (T--) {
a1 = read_point ();
a2 = read_point ();
b1 = read_point ();
b2 = read_point (); //a1,b1是纵坐标较高的点
if (dcmp (a1.y - a2.y) < 0 || (dcmp (a1.y - a2.y) == 0 && dcmp (a1.x - a2.x) > 0)) swap (a1, a2);
if (dcmp (b1.y - b2.y) < 0 || (dcmp (b1.y - b2.y) == 0 && dcmp (b1.x - b2.x) > 0)) swap (b1, b2);
if (dcmp (a1.x - a2.x) == 0 && dcmp (b1.x - b2.x) == 0) { //竖直平行
puts ("0.00"); continue;
}
if (dcmp (a1.y - a2.y) == 0 || dcmp (b1.y - b2.y) == 0) { //水平平行
puts ("0.00"); continue;
}
if (dcmp (cross (a1 - a2, b1 - b2)) == 0) { //共线
puts ("0.00"); continue;
}
if (!can_inter (a1, a2, b1, b2)) { //不能相交
puts ("0.00"); continue;
}
Point p = line_line_inter (a1, a2 - a1, b1, b2 - b1), q;
if (dcmp (a1.y - p.y) <= 0 || dcmp (b1.y - p.y) <= 0) { //有一个点纵坐标低于交点
puts ("0.00"); continue;
}
double ans = 0.0;
if (dcmp (a1.y - b1.y) == 0) {
ans = area_triangle (a1, b1, p);
}
else if (dcmp (a1.y - b1.y) < 0) {
if (dcmp (a1.x - p.x) > 0 && dcmp (b1.x - p.x) > 0) {
if (dcmp (b1.x - a1.x) >= 0 && cross (a1 - p, b1 - p) >= 0) { //入口被覆盖,以下同
puts ("0.00"); continue;
}
}
else if (dcmp (a1.x - p.x) < 0 && dcmp (b1.x - p.x) < 0) {
if (dcmp (b1.x - a1.x) <= 0 && cross (b1 - p, a1 - p) >= 0) {
puts ("0.00"); continue;
}
}
q = line_line_inter (a1, Vector (1, 0), b1, b2 - b1);
ans = area_triangle (a1, q, p);
}
else {
if (dcmp (a1.x - p.x) > 0 && dcmp (b1.x - p.x) > 0) {
if (dcmp (a1.x - b1.x) >= 0 && cross (b1 - p, a1 - p) >= 0) {
puts ("0.00"); continue;
}
}
else if (dcmp (a1.x - p.x) < 0 && dcmp (b1.x - p.x) < 0) {
if (dcmp (a1.x - b1.x) <= 0 && cross (a1 - p, b1 - p) >= 0) {
puts ("0.00"); continue;
}
}
q = line_line_inter (a1, a2 - a1, b1, Vector (1, 0));
ans = area_triangle (b1, q, p);
}
double eps = 1e-8;
printf ("%.2f\n", ans + eps);
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}
简单几何(线段相交) POJ 2826 An Easy Problem?!的更多相关文章
- 简单几何(线段相交) POJ 2653 Pick-up sticks
题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- 简单几何(线段相交) POJ 1066 Treasure Hunt
题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
- 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes
题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- POJ 2826 An Easy Problem?![线段]
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12970 Accepted: 199 ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- POJ 2826 An Easy Problem?!(线段交点+简单计算)
Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...
随机推荐
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- Spring事务传播、隔离等级
事务传播 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中.这是最常见的选择. PROPAGATION_SUPPORTS 支持当前事 ...
- ios图标和默认图像
Icon.png和Default.png是两个重要的图像文件.Icon.png充当应用程序的图标,这些图标用于在SpringBoard主屏幕上表示应用程序.Default.png(也称"启动 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------HtmlHelper
本文转载自powerzhang,如果给您带来不便请联系博主. 在实际的程序中,除了在View中展示数据外,还需要在View与后台的数据进行交互,在View中我就需要用的表单相关的元素: 在MVC3框架 ...
- Swap Nodes & Reverse Nodes in k-Group
Swap Nodes | Given a linked list, swap every two adjacent nodes and return its head. Example Given 1 ...
- EtherCAT数据帧结构
EtherCAT数据直接使用以太网数据帧(以太网帧解释http://blog.chinaunix.net/uid-23080322-id-118440.html)传输,使用的帧类型为0x88A4.Et ...
- 【原创】Mapped Statements collection does not contain value for DaoImpl.method
问题: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Pers ...
- iOS 中使用Base64编码方式编码图片数据
最近一个项目要求对图片数据简单加密下,就是那种不能直接看到图片内容就行.于是我使用了base64编码对图片数据进行编码,把图片2进制数据变成了base64的字符串,再把这个字符串保存到server的数 ...
- 【VirtualBox】端口转发,ssh
端口转发 VirualBox的设置 - 网络 - 端口转发 里面有主机IP.主机端口.子系统IP.子系统端口 设置后的含义是:当外部访问主机IP:主机端口后,将会把访问映射到虚拟机的子系统IP和子系统 ...
- VS2010设置C++包含目录和库目录
视图-属性管理器-随便选择一个项目例如MyProject-Debug|Win32-Microsoft.Cpp.Win32.user-右键“属性”-VC++目录 Release同理