简单几何(线段相交) 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 ...
随机推荐
- [BZOJ1177][Apio2009]Oil
[BZOJ1177][Apio2009]Oil 试题描述 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地为一个矩形区域,被划分为M ...
- Firemonkey的旁门左道[六]
转载:http://blog.csdn.net/qustdong/article/details/9992033 今天还是讲讲和图形有关的事情,这次的难度再增加些,不是直接改源代码了, 而是通过RTT ...
- 2015安徽省赛 B.求和
题目描述 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1<=n ...
- win7+ubuntu双系统中卸载ubuntu方法
双系统中,如果要卸载ubuntu是不能够直接卸载的,需要使用一些特殊的方法.下面就为大家详细的介绍介绍. Step1 MBR引导区修复: 进入win7,下载个软件MbrFix,放在C:\windows ...
- mac os x查看端口命令
`netstat` 命令 a. `netstat -nat | grep <端口号>` 转自: http://my.oschina.net/foreverich/blog/402252
- Java异常的栈轨迹fillInStackTrace和printStackTrace的用法
本文转自wawlian 捕获到异常时,往往需要进行一些处理.比较简单直接的方式就是打印异常栈轨迹Stack Trace.说起栈轨迹,可能很多人和我一样,第一反应就是printStackTrace()方 ...
- Java中static静态关键字的使用
我们可以基于一个类创建多个对象,每个对象都拥有自己的成员,所有成员变量的值是根据对象而存在的,有些时候我们希望一个类的所有对象共享一个成员,这就用到了static静态关键字 被静态关键字修饰的成员属于 ...
- Java for LeetCode 149 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- POJ 3904 Sky Code (容斥原理)
B - Sky Code Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 全排列(next_permutation)
next_permutation函数既可用于非重排列也可用于重排列: #include <bits/stdc++.h> #define MAXN 200000+10 #define ll ...