题意: 求一条直线分凸包两边的面积。

解法: 因为题意会说一定穿过,那么不会有直线与某条边重合的情况。我们只要找到一个直线分成的凸包即可,另一个的面积等于总面积减去那个的面积。

怎么得到分成的一个凸包呢?

从0~n扫过去,如果扫到的边与直线不相交,那么把端点加进新凸包中,如果直线与扫到的边相交了,那么就将交点加入新凸包,然后以后不相交的话也不加入点到新凸包中,直到遇到下一个与直线相交的边,则把交点又加入新凸包,然后在扫到末尾加入点。这样就得到了。

即找到如图:

注意四舍五入。

代码:

#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 Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } Point DisP(Point A,Point B) { return Length(B-A); }
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));
}
double CalcConvexArea(Point* p,int n)
{
double area = 0.0;
for(int i=;i<n-;i++)
area += Cross(p[i]-p[],p[i+]-p[]);
return fabs(area*0.5);
}
Point p[],ch[];
Point P,A,B; int main()
{
int n,i,m;
while(scanf("%d",&n)!=EOF && n)
{
for(i=;i<n;i++) p[i].input();
A.input(), B.input();
Point tmpA = B+(A-B)*, tmpB = A+(B-A)*;
A = tmpA, B = tmpB;
double Total = CalcConvexArea(p,n);
int tot = , fir = , add = ;
ch[tot++] = p[];
for(i=;i<n;i++)
{
Point C = p[i], D = p[(i+)%n];
if(SegmentIntersection(A,B,C,D))
{
SegIntersectionPoint(P,A,B,C,D);
ch[tot++] = P;
if(!fir) fir = ;
else fir = , add = ;
if(P == D) i++;
}
else if(!fir) ch[tot++] = p[(i+)%n];
if(add) ch[tot++] = p[(i+)%n];
}
double Now = CalcConvexArea(ch,tot);
double Other = Total-Now;
int N = (int)(Now+0.5), O = (int)(Other+0.5);
if(O > N) swap(N,O);
printf("%d %d\n",N,O);
}
return ;
}

UESTC 33 Area --凸包面积的更多相关文章

  1. POJ 1654 Area 凸包面积

    水题直接码... /********************* Template ************************/ #include <set> #include < ...

  2. POJ 3348 - Cows 凸包面积

    求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...

  3. poj 3348(凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8063   Accepted: 3651 Description ...

  4. poj 1654 Area 多边形面积

    /* poj 1654 Area 多边形面积 题目意思很简单,但是1000000的point开不了 */ #include<stdio.h> #include<math.h> ...

  5. poj 3348 Cow 凸包面积

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description ...

  6. maya cmds pymel 选择 uv area(uv 面积) 为0 的面

    maya cmds pymel 选择 uv area(uv 面积) 为0 的面 cmds.selectType( pf=True ) cmds.polySelectConstraint( m=3, t ...

  7. poj 3348:Cows(计算几何,求凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6199   Accepted: 2822 Description ...

  8. uva109求凸包面积,判断点是不是在凸包内

    自己想了一个方法判断点是不是在凸包内,先求出凸包面积,在求由点与凸包上每两个点之间的面积(点已经排好序了),如果两者相等,则点在凸包内,否则不在(时间复杂度可能有点高)但是这题能过 #include& ...

  9. poj3348凸包面积

    用叉积求凸包面积 如图所示,每次找p[0]来计算,(叉积是以两个向量构成的平行四边形的面积,所以要/2) #include<map> #include<set> #includ ...

随机推荐

  1. Skytte:一款令人印象深刻的 HTML5 射击游戏

    Skytte 是一款浏览器里的 2D 射击游戏.使用 Canvas 元素和大量的 JavaScript 代码实现.Skytte 是用我们的开源和现代的前端技术创造的.经典,快节奏的横向滚动射击游戏,探 ...

  2. Struts2详细教程

    Struts2详细教程:http://www.yiibai.com/struts_2/

  3. RMS问题整理

    1. 客户端提示"102错误"或者"意外错误,请与管理员联系" 解决方法:在确定Office2003已经安装SP3补丁以后请安装KB978551补丁 下载地址: ...

  4. Linux学习心得之 jnlp的文件和java应用程序安全设置

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 jnlp的文件和java应用程序安全设置 1.前言2. jnlp的文件打开3.java应用程 ...

  5. Win7重装系统遇到的问题以及MysQL的问题解决

    连续三天因为系统的错误,android方面的软件一直不能正确运行.而且每次开机的时候QQ 微信等聊天工具也出现损坏.虽然重新下载一个可以保证在电脑不管的情况下正常的运行.可是作为玩电脑时间不长的我来说 ...

  6. iOS 获取User-Agent

    第一种方法 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];    NSString *userAgent = [w ...

  7. 你真的了解UIViewController生命周期吗?

    一:首先了解一下生命周期图 二:UIViewController 生命周期介绍 1.通过alloc init 分配内存,初始化controller. 2.loadView loadView方法默认实现 ...

  8. view渐变色,透明度渐变

    1 功能描述 开发中经常遇到这样的需求:view2显示在view1上面,透过view2可以渐渐的看到view1.效果如图1所示:view1是一个imageView,view2是一个普通view.vie ...

  9. UEditor无法复制的解决方法

    今天终于知道UEditor不能复制的真正原因啦,还是自己一直没有仔细研究. UEditor 粘贴 Excell 中的表格时报错导致无法粘贴的解决办法 在UEditor一些版本中,如果粘贴Excell中 ...

  10. 敏捷软件开发(4)--- TEMPLATE METHOD & STRATEGY 模式

    1.TEMPLATE METHOD 泛型,也就是这个模式,是可以基于泛型的. 我们往往会有一些算法,比如排序算法.它的算法部分,我可以把它放在一个基类里面,这样具体类型的比较可以放在子类里面. 看如下 ...