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方向任意.如果平面中存在一个点只有某个人到达的时间最短(即没有人比这个人到的时间更短或相同),那么我们定义这个店归这个人管辖,现在问这些人中哪些人的管辖范 ...
随机推荐
- Virtual Memory PAGE TABLE STRUCTURE
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The basic mechanism f ...
- 【转】说说如何使用unity Vs来进行断点调试
大家可以从这下载最新版的unity vs. UnityVs1.81下载 1. 安装unity vs.首先我们打开我们下载的unity vs.然后就会看见里面有3个文件,我们双击UnityVS 2 ...
- MySQL安装图解教程
安装文件存放路径:不能有中文和空格! 校验 1 安装MySQL 2 校验MySQL 登录MySQL:mysql -uroot -p123 退出MySQL:exit | quit 查看数据库:show ...
- 在Vista或更高版本Windows系统中, 获取超大图标的办法
这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...
- Chart控件,把Y轴设置成百分比
这次所有属性设置都用代码(就当整理便于以后查询). 在窗体放置一个Chart控件,未做任何设置:然后编写代码: //设置 chart2.Legends[ ].Enabled = false;//不显示 ...
- css3 transition属性变化与animation动画的相似性以及不同点
下面列子中的2个图片的效果. http://zqtest.e-horse.cn/DongXueImportedCar/assets/mouseOverAnimate.html 第一个为transiti ...
- graphviz - Node Shapes
Node Shapes There are three main types of shapes : polygon-based, record-based and user-defined. The ...
- The Four Stages of Recovering a Project
If a project is in trouble, the project manager needs to work to recover it and get the schedule bac ...
- jQuery层次选择器
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- mac下安装使用svn
mac自带了svn服务端和客户端,所以只需要简单配置一下就可以使用 转自 http://blog.sina.com.cn/s/blog_677fb16e01011i6l.html 1.创建svn ...
