POJ 2826 An Easy Problem?! --计算几何,叉积
题意: 在墙上钉两块木板,问能装多少水。即两条线段所夹的中间开口向上的面积(到短板的水平线截止)
解法: 如图:
先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上方的。
然后求出y值比较小的,建一条水平线,求出与另一条的交点,然后求面积。
要注意的是:
这种情况是不能装水的,要判掉。
还有 交G++会WA, 交C++就可以了, 不知道是POJ的问题还是 G++/C++的问题。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #define eps 1e-8
- using namespace std;
- struct Point{
- double x,y;
- Point(double x=, double y=):x(x),y(y) {}
- void input() { scanf("%lf%lf",&x,&y); }
- };
- typedef Point Vector;
- int dcmp(double x) {
- if(x < -eps) return -;
- if(x > eps) return ;
- return ;
- }
- template <class T> T sqr(T x) { return x * x;}
- Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
- Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
- Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
- Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
- bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
- bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
- bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
- bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
- double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
- double Length(Vector A) { return sqrt(Dot(A, A)); }
- double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
- double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
- bool SegmentIntersection(Point A,Point B,Point C,Point D) {
- return max(A.x,B.x) >= min(C.x,D.x) &&
- max(C.x,D.x) >= min(A.x,B.x) &&
- max(A.y,B.y) >= min(C.y,D.y) &&
- max(C.y,D.y) >= min(A.y,B.y) &&
- dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= &&
- dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= ;
- }
- void SegIntersectionPoint(Point& P,Point a,Point b,Point c,Point d) //需保证ab,cd相交
- {
- P.x = (Cross(d-a,b-a)*c.x - Cross(c-a,b-a)*d.x)/(Cross(d-a,b-a)-Cross(c-a,b-a));
- P.y = (Cross(d-a,b-a)*c.y - Cross(c-a,b-a)*d.y)/(Cross(d-a,b-a)-Cross(c-a,b-a));
- }
- //Data Segment
- Point A,B,C,D;
- //Data Ends
- int main()
- {
- int t,i,j;
- scanf("%d",&t);
- while(t--)
- {
- A.input(), B.input(), C.input(), D.input();
- if(!SegmentIntersection(A,B,C,D)) { puts("0.00"); continue; }
- Point P,Insec;
- SegIntersectionPoint(P,A,B,C,D);
- Vector PA = A-P, PB = B-P;
- Vector PC = C-P, PD = D-P;
- Point S1,S2,S3,S4,K1,K2;
- if(dcmp(PA.y) > ) K1 = A;
- else if(dcmp(PB.y) > ) K1 = B;
- else { puts("0.00"); continue; }
- if(dcmp(PC.y) > ) K2 = C;
- else if(dcmp(PD.y) > ) K2 = D;
- else { puts("0.00"); continue; }
- S3 = Point(K1.x,K1.y), S4 = Point(K1.x,);
- if(SegmentIntersection(S3,S4,P,K2)) { puts("0.00"); continue; }
- S3 = Point(K2.x,K2.y), S4 = Point(K2.x,);
- if(SegmentIntersection(S3,S4,P,K1)) { puts("0.00"); continue; }
- if(dcmp(K1.y-K2.y) < )
- {
- S1 = Point(-,K1.y), S2 = Point(,K1.y);
- SegIntersectionPoint(Insec,S1,S2,P,K2);
- printf("%.2f\n",fabs(Cross(K1-P,Insec-P)*0.5));
- }
- else
- {
- S1 = Point(-,K2.y), S2 = Point(,K2.y);
- SegIntersectionPoint(Insec,S1,S2,P,K1);
- printf("%.2f\n",fabs(Cross(K2-P,Insec-P)*0.5));
- }
- }
- return ;
- }
POJ 2826 An Easy Problem?! --计算几何,叉积的更多相关文章
- 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?![线段]
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12970 Accepted: 199 ...
- 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?!(线段交点+简单计算)
Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...
- POJ 1152 An Easy Problem! (取模运算性质)
题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...
- [POJ] 2453 An Easy Problem [位运算]
An Easy Problem Description As we known, data stored in the computers is in binary form. The probl ...
随机推荐
- 详细解读XMLHttpRequest(一)同步请求和异步请求
本文主要参考:MDN XMLHttpRequest 让发送一个HTTP请求变得非常容易.你只需要简单的创建一个请求对象实例,打开一个URL,然后发送这个请求.当传输完毕后,结果的HTTP状态以及返回的 ...
- overflow 属性
写在前面的话: 2016年5月4日青年节,作为一名正青春的学生党,开始了博客生涯,励志做个勤奋上进的好青年.幻想着毕业后月薪W+ .走上人生巅峰的职场生活...... 然而 然而 然而 ,自制力有限的 ...
- SharePoint 2013 搭建负载均衡(NLB)
服务器架构(三台虚机:AD和Sql在一台,前端两台) DC.Sql Server,其中包括:AD.DNS.DHCP服务(非必须): SPWeb01,其中包括:IIS.SharePoint: SPWeb ...
- Javascript - Arraylike的7种实现
jQuery的崛起让ArrayLike(类数组)在javascript中大放异彩,它的出现为一组数据的行为(函数)扩展提供了基础. 类数组和数组相似,具有数组的某些行为,但是它相比数组可以更加自由的扩 ...
- [转]MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集
注:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin stsadm –o add ...
- xmpp整理笔记:环境的快速配置(附安装包)
现在虽然环信的xmpp框架很火,但是也有一些弊端.环信的框架部分代码不开源,而且收费模式不科学,用户量一直低于免费线则好,一旦超过,收费极高. xmpp感觉还是从xmppFramework框架学起比较 ...
- JQuery隐藏显示详情功能
放置两个DIV:初始DIV :在Repetr绑定设置文字隐藏(三元运算符):'<%# Eval("字段2").ToString().Length>11?Eval(&qu ...
- Android微信登陆
前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.co ...
- Javascript之旅——第七站:说说js的调试
最近比较吐槽,大家都知道,现在web前端相对几年前来说已经变得很重了,各种js框架,各种面对对象,而且项目多了,就会提取公共模块, 这些模块的UI展示都一样,不一样的就是后台逻辑,举个例子吧,我们做企 ...
- SQLServer中ISNULL、NULLIF和CONVERT函数
create view sss as(select ISNULL(operate_time, CONVERT(VARCHAR(20),create_time,120)) time from s_pro ...