An Easy Problem?! - POJ 2826(求面积)

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = ;
const double oo = 1e4+;
const double EPS = 1e-; int sign(double val)
{
if(val > EPS)return ;
if(fabs(val) < EPS)return ;
return -;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y){}
Point operator - (const Point &tmp) const{
return Point(x-tmp.x, y-tmp.y);
}
double operator ^(const Point &tmp) const{
return x*tmp.y - y*tmp.x;
}
bool operator == (const Point &tmp) const{
return fabs(x-tmp.x) < EPS && fabs(y-tmp.y) < EPS;
}
double operator *(const Point &tmp) const{
return x*tmp.x + y*tmp.y;
}
};
struct Segment
{
Point s, e;
double a, b, c;///ax + by = c
Segment(Point s=, Point e=):s(s), e(e){
a = s.y - e.y;
b = e.x - s.x;
c = e.x*s.y - s.x*e.y;
}
bool Inter(const Segment &t)const{
int v1 = sign((s-e)^(t.s-e));
int v2 = sign((s-e)^(t.e-e)); if(!v1 && !v2)return false;///共线 if(!v1 && t.s.x >= min(s.x, e.x) && t.s.x <= max(s.x, e.x)
&& t.s.y >= min(s.y, e.y) && t.s.y <= max(s.y, e.y)
|| !v2 && t.e.x >= min(s.x, e.x) && t.e.x <= max(s.x, e.x)
&& t.e.y >= min(s.y, e.y) && t.e.y <= max(s.y, e.y)
|| v1 * v2 == -)return true; return false;
}
Point CrossNode(const Segment &t) const{
Point ans;
ans.x = (c*t.b-t.c*b)/(a*t.b-t.a*b);
ans.y = (c*t.a-t.c*a)/(b*t.a-t.b*a); return ans;
}
};
double Dist(Point a, Point b)
{
return sqrt((a-b) * (a-b));
}
double Find(Point crs, Point p[], int N)
{
double sum = ; for(int i=; i<N; i++)
for(int j=i+; j<N; j++)
{
int k = sign((p[i]-crs)^(p[j]-crs)); if(p[i] == p[j])continue; if(crs.x>=min(p[i].x, p[j].x) && crs.x<=max(p[i].x, p[j].x)
|| crs.x>=p[i].x && crs.x>=p[j].x && (k>&&(p[i].x-p[j].x>EPS) || k<&&(p[j].x-p[i].x>EPS))
|| crs.x<=p[i].x && crs.x<=p[j].x && (k<&&(p[j].x-p[i].x>EPS) || k>&&(p[i].x-p[j].x>EPS)))
if(p[i].y-crs.y > EPS && p[j].y-crs.y > EPS)
{
Point A = p[i].y < p[j].y ? p[i] : p[j];
Point B = (A == p[i] ? p[j] : p[i]);
Segment t1(Point(-oo, A.y), Point(oo, A.y));
Segment t2(crs, B); B = t1.CrossNode(t2); double La = Dist(A, B);
double Lb = Dist(A, crs);
double Lc = Dist(B, crs);
double p = (La+Lb+Lc) / ; sum += sqrt(p*(p-La)*(p-Lb)*(p-Lc));
}
} return sum;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
Point p[MAXN], crs; scanf("%lf%lf%lf%lf", &p[].x, &p[].y, &p[].x, &p[].y);
scanf("%lf%lf%lf%lf", &p[].x, &p[].y, &p[].x, &p[].y);
Segment L1(p[], p[]), L2(p[], p[]); double ans = ; if(L1.Inter(L2) && L2.Inter(L1))
{
crs = L1.CrossNode(L2);
ans = Find(crs, p, );
} printf("%.2f\n", ans+EPS);
} return ;
}
An Easy Problem?! - POJ 2826(求面积)的更多相关文章
- POJ 1152 An Easy Problem! (取模运算性质)
题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...
- hdu2601 An easy problem(数学)
题目意思: http://acm.hdu.edu.cn/showproblem.php? pid=2601 给出一个数N,求N=i*j+i+j一共同拥有多少种方案. 题目分析: 此题直接暴力模拟就可以 ...
- 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?! --计算几何,叉积
题意: 在墙上钉两块木板,问能装多少水.即两条线段所夹的中间开口向上的面积(到短板的水平线截止) 解法: 如图: 先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上 ...
- POJ 2826 An Easy Problem?! 好的标题
受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...
- 【解题报告】PKU 2826 An Easy Problem?!
原题链接:http://poj.org/problem?id=2826 一题很蛋疼的一题.目前为止还有一个问题我没搞清楚,问题注在代码中. 题目大意: 外面下雨了,农民Johnoson的bull(?? ...
- poj 3348--Cows(凸包求面积)
链接:http://poj.org/problem?id=3348 Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: ...
随机推荐
- 防止sql注入式攻击 SQL注入学习——三层架构
解决方案是:1.首先在UI录入时,要控制数据的类型和长度.防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交:2.业务逻辑层控制,通过在方法内部将SQL关键字用 ...
- IO流基础加强
字节流对象:InputStream,OutputStream 缓冲字节流对象:BufferedInputStream , BufferedOutputStream 用法和字符流对象一样,但也有区别, ...
- android入门到熟练(二)----活动
1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...
- 关于A*寻路算法的认识
最近要参加学校的APP比赛,我们组做的是一个3D迷宫的小APP,我负责的是迷宫的生成与寻路. 寻路算法选择的是A*寻路算法,具体参考的是下面的这篇博客. 本文主要是谈谈自己对A*算法的理解,具体细节, ...
- jquery live()只支持css选择器
昨天在处理过keypress键盘事件后,今天要把用户在页面上动态添加的字段条目加上删除功能,就是在每个字段后面加上一个漂亮的小按钮,当用户点击这个按钮,相应的条目就被从数据库中删除. 为了实现这种功能 ...
- JavaScript学习总结【4】、JS深入
1.JS流程控制语句 (1).if 判断 if 语句是基于条件成立时才执行相应的代码. if...else 语句是在指定的条件成立时执行if后的代码,在条件不成立时执行else后的代码. if...e ...
- CSS鼠标点击式变化图片透明度
今天分享前端代码主题:jequery控制css图片透明度 很多时候在网站图片处理上需要实现一些辅助效果,比如鼠标在图片上滑动时或点击时改变图片颜色(变灰或者其他),其实一个简单的办法就是改变图片css ...
- C#一个字符串的加密与解密
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- yii2源码学习笔记(十六)
Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...
- Ubuntu系统、开发环境配置
在VMware10下安装成功了Ubuntu 13.10桌面版,刚安装完需要配置很多内容,下面为记录: 1. 更新源: 想了解更新地址的可以查看apt-get的源列表文件 $ sudo gedit /e ...