当前位置: 首页 > news >正文

【无聊放个模板系列】 半平面交

求半平面交的面积

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 1100

struct P {double x,y;};
struct L {P a,b;double slop;}l[Maxn];
//半平面只方向向量a->b的左部分
//slop 极角
int cnt;

P operator - (P x,P y)
{
    P tt;
    tt.x=x.x-y.x;
    tt.y=x.y-y.y;
    return tt;
}

P operator + (P x,P y)
{
    P tt;
    tt.x=x.x+y.x;
    tt.y=x.y+y.y;
    return tt;
}

double Dot(P x,P y) {return x.x*y.x+x.y*y.y;}
double Cross(P x,P y) {return x.x*y.y-x.y*y.x;}

bool operator < (L a,L b)
{
    if(a.slop!=b.slop)return a.slop<b.slop;
    return Cross(a.b-a.a,b.b-a.a)>0;
}

P operator * (P X,double y)
{
    P tt;
    tt.x=X.x*y;
    tt.y=X.y*y;
    return tt;
}

P a[Maxn];
L q[Maxn];
int tot;

P inter(L a,L b)
{
    P X=a.a-a.b,Y=b.a-b.b,nw;
    double tt;
    nw=b.a-a.a;
    tt=Cross(nw,X)/Cross(X,Y);
    P ans=b.a+Y*tt;
    return ans;
}

bool jud(L a,L b,L c)
{
    P p=inter(a,b);
    return Cross(c.b-c.a,p-c.a)<0;
}

void opp()
{
    for(int i=1;i<=cnt;i++)
    {
        printf("%.2lf %.2lf %.2lf %.2lf = %.2lf \n",l[i].a.x,l[i].a.y,l[i].b.x,l[i].b.y,l[i].slop);
    }
    printf("\n");
}

void output()
{
    for(int i=1;i<=tot;i++) printf("%2lf %.2lf\n",a[i].x,a[i].y);
    printf("\n");
}

void op(int L,int R)
{
    for(int i=L;i<=R;i++)
        printf("%lf %lf %lf %lf\n",l[i].a.x,l[i].a.y,l[i].b.x,l[i].b.y);
    printf("\n");
}

void ffind()
{
    for(int i=1;i<=cnt;i++)
      l[i].slop=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
    sort(l+1,l+1+cnt);
    
    // opp();
    
    int L=1,R=0;
    //去重?
    tot=0;
    for(int i=1;i<=cnt;i++)
    {
        if(l[i].slop!=l[i-1].slop) tot++;
        l[tot]=l[i];
    }
    cnt=tot;tot=0;
    // opp();
    q[++R]=l[1];q[++R]=l[2];
    for(int i=3;i<=cnt;i++)
    {
        while(L<R&&jud(q[R-1],q[R],l[i])) R--;
        while(L<R&&jud(q[L+1],q[L],l[i])) L++;
        q[++R]=l[i];
        // op(L,R);
    }
	if(L<R&&jud(q[R-1],q[R],q[L])) R--;
        op(L,R);
    q[R+1]=q[L];
    for(int i=L;i<=R;i++)
      a[++tot]=inter(q[i],q[i+1]);
  // output();
}

void init()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf%lf%lf\n",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
    }
    cnt=n;
}

void get_area()
{
    double ans=0;
    for(int i=1;i<tot;i++)
    {
        ans+=Cross(a[i],a[i+1]);
    }
    ans+=Cross(a[tot],a[1]);
    if(tot<3) ans=0;
    printf("%.3lf\n",ans/2);
}

int main()
{
    init();
    ffind();
    get_area();
    return 0;
}

  

 

没有删调试的。

输入半平面,输出面积。

 

样例:

6
4 5 2 0
9 2 5 6
3 1 9 5
5 1 7 5
8 2 9 4
5 7 3 5

 

输出:

10.459

 

2016-12-25 14:39:14

转载于:https://www.cnblogs.com/Konjakmoyu/p/6219514.html

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 最长公共子序列(LCS)问题
  • 01背包问题
  • JS控制文本框只能输入中文、英文、数字与指定特殊符号.
  • iOS僵尸对象之研究
  • 在一个大数组中有且仅有两个数相同,怎样尽快找出这两个数(未完成)
  • 字符串匹配算法
  • CXF:根据werservice代码生成WSDL(转)
  • 位运算的应用和实例
  • 深入理解css3中 nth-child 和 nth-of-type 的区别
  • 求最大公约数和最小公倍数
  • 方案撰写注意事项
  • Linux 常用命令使用方法大搜刮
  • 应用Hash函数(java描述)
  • 用java实现生产者和消费者问题
  • 【转】AngularJS 日期格式化 字典
  • 深入了解以太坊
  • 〔开发系列〕一次关于小程序开发的深度总结
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • export和import的用法总结
  • Fabric架构演变之路
  • gf框架之分页模块(五) - 自定义分页
  • Linux Process Manage
  • PHP的Ev教程三(Periodic watcher)
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • 二维平面内的碰撞检测【一】
  • 排序算法之--选择排序
  • 前端技术周刊 2019-01-14:客户端存储
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (0)Nginx 功能特性
  • (27)4.8 习题课
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (多级缓存)缓存同步
  • (二)pulsar安装在独立的docker中,python测试
  • (分布式缓存)Redis分片集群
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转) 深度模型优化性能 调参
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .NET 快速重构概要1
  • .skip() 和 .only() 的使用
  • @synthesize和@dynamic分别有什么作用?
  • [<死锁专题>]
  • [1]-基于图搜索的路径规划基础
  • [14]内置对象
  • [20181219]script使用小技巧.txt
  • [BUUCTF NewStarCTF 2023 公开赛道] week3 crypto/pwn
  • [BUUCTF]-PWN:wustctf2020_number_game解析(补码,整数漏洞)
  • [BZOJ1053][HAOI2007]反素数ant
  • [Contest20180313]灵大会议