hdu 3060 Area2 (计算几何模板)
Problem Description
这一次,小白扔的炸弹比较奇怪,爆炸的覆盖区域不是圆形,而是一个不规则的简单多边形,请你再次帮助小白,计算出炸到了多少面积。
需要注意的是,这次小白一共扔了两枚炸弹,但是两枚炸弹炸到的公共部分的面积只能计算一次。
接着输入n行,每行输入一个(x,y)坐标,代表第一枚炸弹爆炸范围图形的顶点(按顺势针或者逆时针给出)。
最后输入m行,每行输入一个(x',y')坐标,代表第二枚炸弹爆炸范围图形的顶点(按顺势针或者逆时针给出)。
(3<= n,m <= 500)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 510
const double eps=1E-;
int sig(double d){
return(d>eps)-(d<-eps);
}
struct Point{
double x,y; Point(){}
Point(double x,double y):x(x),y(y){}
bool operator==(const Point&p)const{
return sig(x-p.x)==&&sig(y-p.y)==;
}
};
double cross(Point o,Point a,Point b){ //叉积
return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
double area(Point* ps,int n){
ps[n]=ps[];
double res=;
for(int i=;i<n;i++){
res+=ps[i].x*ps[i+].y-ps[i].y*ps[i+].x;
}
return res/2.0;
}
int lineCross(Point a,Point b,Point c,Point d,Point&p){
double s1,s2;
s1=cross(a,b,c);
s2=cross(a,b,d);
if(sig(s1)==&&sig(s2)==) return ;
if(sig(s2-s1)==) return ;
p.x=(c.x*s2-d.x*s1)/(s2-s1);
p.y=(c.y*s2-d.y*s1)/(s2-s1);
return ;
}
//多边形切割
//用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果
//如果退化为一个点,也会返回去,此时n为1
void polygon_cut(Point*p,int&n,Point a,Point b){
static Point pp[maxn];
int m=;p[n]=p[];
for(int i=;i<n;i++){
if(sig(cross(a,b,p[i]))>) pp[m++]=p[i];
if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+])))
lineCross(a,b,p[i],p[i+],pp[m++]);
}
n=;
for(int i=;i<m;i++)
if(!i||!(pp[i]==pp[i-]))
p[n++]=pp[i];
while(n>&&p[n-]==p[])n--;
}
//---------------华丽的分隔线-----------------//
//返回三角形oab和三角形ocd的有向交面积,o是原点//
double intersectArea(Point a,Point b,Point c,Point d){
Point o(,);
int s1=sig(cross(o,a,b));
int s2=sig(cross(o,c,d));
if(s1==||s2==)return 0.0;//退化,面积为0
if(s1==-) swap(a,b);
if(s2==-) swap(c,d);
Point p[]={o,a,b};
int n=;
polygon_cut(p,n,o,c);
polygon_cut(p,n,c,d);
polygon_cut(p,n,d,o);
double res=fabs(area(p,n));
if(s1*s2==-) res=-res;return res;
}
//求两多边形的交面积
double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
if(area(ps1,n1)<) reverse(ps1,ps1+n1);
if(area(ps2,n2)<) reverse(ps2,ps2+n2);
ps1[n1]=ps1[];
ps2[n2]=ps2[];
double res=;
for(int i=;i<n1;i++){
for(int j=;j<n2;j++){
res+=intersectArea(ps1[i],ps1[i+],ps2[j],ps2[j+]);
}
}
return res;//assumeresispositive!
}
Point ps1[maxn],ps2[maxn];
int n1,n2;
int main(){
while(scanf("%d%d",&n1,&n2)!=EOF){
for(int i=;i<n1;i++)
scanf("%lf%lf",&ps1[i].x,&ps1[i].y);
for(int i=;i<n2;i++)
scanf("%lf%lf",&ps2[i].x,&ps2[i].y);
double ans=intersectArea(ps1,n1,ps2,n2);
ans=fabs(area(ps1,n1))+fabs(area(ps2,n2))-ans;//容斥
printf("%.2f\n",ans);
}
return ;
}
hdu 3060 Area2 (计算几何模板)的更多相关文章
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- lrj计算几何模板
整理了一下大白书上的计算几何模板. #include <cstdio> #include <algorithm> #include <cmath> #include ...
- HDU 4998 Rotate (计算几何)
HDU 4998 Rotate (计算几何) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4998 Description Noting is more ...
- hdu 4643 GSM 计算几何 - 点线关系
/* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...
- UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]
题目链接:https://cn.vjudge.net/problem/UVA-12304 题意: 作为题目大合集,有以下一些要求: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三 ...
- hdu 4667 Building Fence < 计算几何模板>
//大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <string ...
- HDU - 5572 An Easy Physics Problem (计算几何模板)
[题目概述] On an infinite smooth table, there's a big round fixed cylinder and a little ball whose volum ...
- HDU 1532 最大流模板题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
随机推荐
- 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 ...
- 20175213 2018-2019-2 《Java程序设计》第11周学习总结
教材学习内容总结 URL类是java.net包中的一个重要的类,URL的实例封装着一个统一资源定位符(Uniform Resource Locator),使用URL创建对象的应用程序称作客户端程序 U ...
- meta标签 使用说明(http-equiv、refresh、seo)
meta标签 使用说明(http-equiv.refresh.seo) meta标签,是在head标签里面,一般用做页面描述的.它的内容,用来描述页面一些信息的,如类型.编码.作者.简介等!虽然,它不 ...
- Windows 08R2_AD图文详解
目录 目录 软件环境 Active Directory域服务 AD的应用 创建ADDS域 使用Windows窗口来创建ADDS域控制器 使用Powershell来创建ADDS域控制器 检查ADDC域控 ...
- CET-6 分频周计划生词筛选(Week 2)
点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...
- Python 与 C 对比
到目前为止,我接触最多两种语言应该就是python 和 C 语言了. 个人理解 1. 执行速度不同, python为解释性语言,C是编译型语言(需要编译器) 2. python 是基于C的实现,C中很 ...
- Cocos2d-x之数据的处理
| 版权声明:本文为博主原创文章,未经博主允许不得转载. FileUtils 在游戏中,用户要保存自己的偏好设置和玩家的信息,都需要涉及到游戏数据的处理.首先要想处理数据,则要找到文件,创建文件, ...
- vue动态组件 互相之间传输数据 和指令的定义
地址:https://blog.csdn.net/zhanghuanhuan1/article/details/77882595 地址:https://www.cnblogs.com/xiaohuoc ...
- C++中的异常处理(下)
1,catch 语句块中可以抛出异常: 1,示意图: 2,func() 在 try 语句块中,说明它有可能抛出异常,抛出的异常有可能是整型或其它类型: 3,catch 语句块处理方式是将异常重新抛出去 ...
- 在Emacs中使用plantuml画UML图
在Emacs中使用plantuml画UML图 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #83 ...