hdu 2528 Area
2014-07-30 http://acm.hdu.edu.cn/showproblem.php?pid=2528
解题思路:
求多边形被一条直线分成两部分的面积分别是多少。因为题目给的直线一定能把多边形分成两部分,所以就不用考虑多边形是否与直线相交。直接求问题。
将多边形的每一条边与直线判断是否相交。若相交,就从这点开始计算面积,直到判断到下一个边与直线相交的点。这之间的面积求出来为area2。
area1为多边形的总面积。多边形被直线分成的另外一部分面积 = area1 - area2 有一个特殊的情况:当直线与多边形的顶点相交时,应该考虑下如何处理 1 #include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; #define MAXN 30
#define EPS 0.00000001 int dcmp(double x){
if(fabs(x) < EPS)
return ;
return x < ? - : ;
} struct Point{
double x, y;
Point(double x = , double y = ): x(x), y(y) {} bool operator != (const Point & other){
return dcmp(x - other.x) != || (dcmp(x - other.x) == && dcmp(y - other.y) != );
}
}; struct Line{
Point A, B;
//Line(Point A = Point(0, 0), Point B = Point(0, 0)): A(A), B(B){}
}; typedef Point Vector; Vector operator + (Vector A, Vector B){
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Point A, Point B){
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Vector A, double d){
return Vector(A.x * d, A.y * d);
} Vector operator / (Vector A, double d){
return Vector(A.x / d, A.y / d);
} 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;
} int n;
Point p[MAXN];
Line line; bool input(){
scanf("%d", &n );
if(!n){
return false;
}
for(int i = ; i < n; i++ ){
scanf("%lf%lf", &p[i].x, &p[i].y );
}
scanf("%lf%lf%lf%lf", &line.A.x, &line.A.y, &line.B.x, &line.B.y );
return true;
} double polygon_area(){//求多边形面积
double area = ;
for(int i = ; i < n - ; i++ ){
area += cross(p[i] - p[], p[i + ] - p[]);
}
return fabs(area) * 0.5;
} bool line_segment_intersect(Line L, Point A, Point B, Point &P){//直线和线段相交
Vector a = A - L.B, b = L.A - L.B, c = B - L.B;
if(dcmp(cross(a, b)) * dcmp(cross(b, c)) >= ){//若直线和线段相交 求出交点 《算法入门经典训练之南》上的公式
Vector u = L.A - A;
double t = cross(A - B, u) / cross(b, A - B);
P = L.A + b * t;
return true;
}
return false;
} void solve(){
int flag = ;
double area1 = polygon_area(), area2 = ;//area1算出多边形总面积
Point P, T; p[n] = p[];
for(int i = ; i < n; i++ ){
if(flag == && line_segment_intersect(line, p[i], p[i + ], P)){//第一次相交点
area2 += cross(P, p[i + ]);
flag++;
}else if(flag == && line_segment_intersect(line, p[i], p[i + ], T) && P != T){//第二次相交点
area2 += cross(p[i], T);
area2 += cross(T, P);
flag++;
break;
}else if(flag == ){
area2 += cross(p[i], p[i + ]);
}
}
area2 = fabs(area2) * 0.5;
area1 -= area2;//area1 获取多边形另外一半的面积
if(area1 < area2){//规定大面积在前面 输出
swap(area1, area2);
}
printf("%.0lf %.0lf\n", area1, area2);
} int main(){
//freopen("data.in", "r", stdin );
while(input()){
solve();
}
return ;
}
hdu 2528 Area的更多相关文章
- hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)
Area Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2892 Area
http://acm.hdu.edu.cn/showproblem.php?pid=2892 解题思路: 求多边形与圆的相交的面积是多少. 以圆心为顶点,将多边形划分为n个三角形. 接下来就求出每个三 ...
- hdu 4946 Area of Mushroom(凸包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946 Area of Mushroom Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 4946 Area of Mushroom(构造凸包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946 题目大意:在一个平面上有n个点p1,p2,p3,p4....pn,每个点可以以v的速度在平面上移 ...
- HDU 4946 Area of Mushroom 凸包
链接:pid=4946">http://acm.hdu.edu.cn/showproblem.php?pid=4946 题意:有n个人.在位置(xi,yi),速度是vi,假设对于某个点 ...
- HDU 4946 Area of Mushroom 凸包 第八次多校
题目链接:hdu 4946 题意:一大神有N个学生,各个都是小神,大神有个二次元空间,每一个小神都有一个初始坐标,如今大神把这些空间分给徒弟们,规则是假设这个地方有一个人比谁都先到这,那么这个地方就是 ...
- hdu 1451 Area in Triangle(计算几何 三角形)
Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope t ...
- HDU 4946 Area of Mushroom(2014 Multi-University Training Contest 8)
思路: 只有速度最大才有可能为1,速度不是最大肯定为0,那么就是 只需要操作那些速度最大的点,这些点求一个凸包,判断一下是不是在凸包边上即可. 有几个需要注意的地方: 1.最大速度如果为0 那么肯 ...
- HDU 4946 Area of Mushroom (几何凸包)
题目链接 题意:给定n个人,每个人有一个速度v方向任意.如果平面中存在一个点只有某个人到达的时间最短(即没有人比这个人到的时间更短或相同),那么我们定义这个店归这个人管辖,现在问这些人中哪些人的管辖范 ...
随机推荐
- hot code replace
http://wiki.eclipse.org/FAQ_What_is_hot_code_replace%3F https://social.msdn.microsoft.com/Forums/vst ...
- git 第一次初始化
Command line instructions Git global setup git config --global user.name "{名字}({工号})" git ...
- jquery mobile 方法收集.
1.在列表项和按钮上禁用文本截断 如果你的列表项或者按钮上是一个很长的文本,它将会被jQuery Mobile自动截断,要禁用这个截断设置,需要在CSS选择器上添加属性"white- ...
- zepto源码--几个判断函数--学习笔记
几个需要经常用到的类型判断: 自定义一个类似于typeof的函数,提供更多的类型判断. class2type[toString.call(obj)] 是对class2type的取值 在后面通过循环对c ...
- C# 中==与Equals方法比较
先来段代码,如下: static void Main(string[] args) { string a = new string(new char[] { 'h', 'e', 'l', 'l', ' ...
- css3 loading效果
file:///E:/zhangqiangWork/2014/SPDbank/index.html 参考该网站 http://tobiasahlin.com/spinkit/ 查看源代码把里面的dom ...
- LightOj1007 - Mathematically Hard(欧拉函数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1007 题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ...
- Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager.responseSerializer.acceptab ...
- Selenium2学习-020-WebUI自动化实战实例-018-获取浏览器窗口位置大小
在 UI 自动化测试过程中,每个机器上浏览器的默认大小.默认位置不尽相同,需要截图的时候,页面元素可能显示不完全,因而我们需要知道浏览器的宽度,或者直接在启动浏览器时,设置浏览器的宽度或位置(此文暂不 ...
- Advanced REST client的使用说明
1. 为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...
