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方向任意.如果平面中存在一个点只有某个人到达的时间最短(即没有人比这个人到的时间更短或相同),那么我们定义这个店归这个人管辖,现在问这些人中哪些人的管辖范 ...
随机推荐
- delimiter
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx The first command is ...
- 【Demo】 生成二维码 和 条形码
条形码 和 二维码 对比 一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于阅读器的对准. 在水平和垂直方向的二维空间存储信息的条形码, 称为 ...
- Bluetooth GATT介绍
目录 1. 介绍 2 内容 2.1 Configured Broadcast 2.2 GATT Profile Hierarchy 3 Service Interoperability Require ...
- 使用Nginx在自己的电脑上实现负载均衡
我其实早就想弄这个负载均衡了,但是总觉得这玩意肯定不简单,今天星期六闲着没事终于下定决心来搞一搞他了,但是没想到这玩意这么简单,真的是出乎我的意料的简单(我现在陪的是最简单的那种).额是没有我想象中的 ...
- java JDK8 学习笔记——第11章 线程和并行API
第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...
- 欢迎大家提问Android技术及职业生涯等问题
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 最近有些时间,但QQ群问的问题比较多,不能一一解答,如果有价值 ...
- QFile文件操作-QT
#include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug&g ...
- Notepad++ install vi plugin
下载Notepad++,想安装vi插件. 使用Notepad++自带的插件管理器下载visimulator失败. 所以直接下载插件visimulator.dll,再导入. 下载地址: https:// ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- 详解js变量、作用域及内存
详解js变量.作用域及内存 来源:伯乐在线 作者:trigkit4 原文出处: trigkit4 基本类型值有:undefined,NUll,Boolean,Number和Strin ...
