ZBT的计算几何模板
Basic template
一个基础型模板包括一个向量的实现
DATE: 2015-06-01
#define op operator
#define __ while
#define _0 return
typedef long long ll;
inline ll _(ll a,ll b){ll t;__(a){t=a;a=b%a;b=t;}_0 b;}
struct frac{
ll u,d;
frac(ll u=0,ll d=1):u(u),d(d){}
frac op()(){ll _1=_(u,d);_0 frac(u/_1,d/_1);}
frac op*(frac b){_0 (frac(u*b.u,d*b.d))();}
frac op/(frac b){_0 (frac(u*b.d,d*b.u))();}
frac op*(ll n){_0 (frac(u*n,d))();}
frac op/(ll n){_0 (frac(u,d*n))();}
frac op[](ll n){_0 frac(u*n,d*n);}
frac op+(ll n){_0 frac(u+d*n,d);}
frac op-(ll n){_0 frac(u-d*n,d);}
frac op+(frac b){frac _1=(*this)[b.d],_2=b[d];_0 (frac(_1.u+_2.u,_1.d))();}
frac op-(frac b){frac _1=(*this)[b.d],_2=b[d];_0 (frac(_1.u-_2.u,_1.d))();}
void op=(ll b){d=1,u=b;}
ll op()(frac b){return u*b.d-d*b.u;}//<=>
bool op==(frac b){return u==b.u&&d==b.d;}
bool op>(frac b){return b(*this)<0;}
bool op<(frac b){return b(*this)>0;}
};
frac op/(ll a,frac b){_0 (frac(b.d*a,b.u))();}
frac op-(ll a,frac b){_0 frac(a)-b;}
frac op+(ll a,frac b){_0 frac(a)+b;}
frac op*(ll a,frac b){_0 (frac(a*b.u,b.d))();}
typedef struct vec{
frac x,y;
vec(frac x,frac y):x(x),y(y){};
vec op+(vec b){_0 vec(x+b.x,y+b.y);}
vec op-(vec b){_0 vec(x-b.x,y-b.y);}
vec op*(frac b){_0 vec(x*b,y*b);}
vec op/(frac b){_0 vec(x/b,y/b);}
vec op*(ll b){_0 vec(x*b,y*b);}
vec op/(ll b){_0 vec(x/b,y/b);}
frac op*(vec b){_0 x*b.y-y*b.x;}//cross product
frac op[](vec b){_0 x*b.x+y*b.y;}//dot product
bool op==(vec b){_0 x==b.x&&y==b.y;}//equality test
} point;
本模板风格可能引起不适>_<
其实,用'[]'做dot product是因为C++中无法重载'.'运算符,而在大多数动态语言(比如javascript)中'.'与'[]'的作用几乎相等,且在javascript中'.'是一个'[]'的语法糖.
frac里就直接乱凑剩下的符号了>_<
有错误的话请提出来>_<...
Polygon & convex hull
Andrew法凸包.
#include <cstdio>
#include <malloc.h>
#include <cstring>
#include <algorithm>
#define op operator
#define __ while
#define _0 return
typedef long long ll;
using namespace std;
inline ll _(ll a,ll b){ll t;__(a){t=a;a=b%a;b=t;}_0 b;}
struct frac{
ll u,d;
frac(ll u=0,ll d=1):u(u),d(d){}
frac op()(){ll _1=_(u,d);if(d/_1<0)_1=-_1;_0 frac(u/_1,d/_1);}
frac op*(frac b){_0 (frac(u*b.u,d*b.d))();}
frac op/(frac b){_0 (frac(u*b.d,d*b.u))();}
frac op*(ll n){_0 (frac(u*n,d))();}
frac op/(ll n){_0 (frac(u,d*n))();}
frac op[](ll n){_0 frac(u*n,d*n);}
frac op+(ll n){_0 frac(u+d*n,d);}
frac op-(ll n){_0 frac(u-d*n,d);}
frac op+(frac b){frac _1=(*this)[b.d],_2=b[d];_0 (frac(_1.u+_2.u,_1.d))();}
frac op+(frac b){frac _1=(*this)[b.d],_2=b[d];_0 (frac(_1.u-_2.u,_1.d))();}
void op=(ll b){d=1,u=b;}
ll op()(frac b){return u*b.d-d*b.u;}//<=>
bool op==(frac b){return u==b.u&&d==b.d;}
}
frac op/(ll a,frac b){_0 (frac(b.d*a,b.u))();}
frac op-(ll a,frac b){_0 frac(a)-b;}
frac op+(ll a,frac b){_0 frac(a)+b;}
frac op*(ll a,frac b){_0 (frac(a*b.u,b.d))();}
typedef struct vec{
frac x,y;
vec(frac x,frac y):x(x),y(y){};
vec op+(vec b){_0 vec(x+b.x,y+b.y);}
vec op-(vec b){_0 vec(x-b.x,y-b.y);}
vec op*(frac b){_0 vec(x*b,y*b);}
vec op/(frac b){_0 vec(x/b,y/b);}
vec op*(ll a){_0 vec(x*b,y*b);}
vec op/(ll b){_0 vec(x/b,y/b);}
frac op*(vec b){_0 x*b.y-y*b.x;}//cross product
frac op[](vec b){_0 x*b.x+y*b.y;}//dot product
bool op==(vec b){_0 x==b.x&&y==b.y;}//equality test
} point;
bool pcmp(const point& p1,const point& p2){
ll a=p1.x(p2.x);
if(a>0) return false;
if(a<0) return true;
return p1.y(p2.y)<0;
}
struct polygon{
point* p;
int n,s;
polygon(int k,point* q){
s=(n=k)<<1;
p=(point*)malloc(s*sizeof(point));
for(int i=0;i<n;++i) p[i]=q[i];
}
inline void sizeup(int q){
s<<=1;
if(s>q){
free(p);
p=(point*)malloc(s*sizeof(point));
}
}
inline void copy(polygon a){
while(a.n>s){
sizeup(a.n);
}
n=a.n;
for(int i=0;i<n;++i) p[i]=a.p[i];
}
void op=(polygon a){
copy(a);
}
void op=(polygon* a){
copy(*a);
}
frac area(){
frac area(0,1);
for(int i=1;i<n-1;++i) area+=(p[i]-p[0])*(p[i+1]-p[0]);
return area/2;
}
void sort(){
sort(p,p+n,pcmp);
}
}
struct convex_hull{
polygon pol;//convex hull is also a polygon
convex_hull(polygon* x){
pol=x;
polygon pol2=x;
pol2.sort();
int m=0;
for(int i=0;i<pol2.n;++i){
while(m>1 && ((pol.p[m-1]-pol.p[m-2])*(pol2.p[i]-pol.p[m-2]))(frac(0,1))<0ll) m--;
pol.p[m++]=pol2.p[i];
}
int k=m;
for(int i=pol2.n-2;~i;--){
while(m>1 && ((pol.p[m-1]-pol.p[m-2])*(pol2.p[i]-pol.p[m-2]))(frac(0,1))<0ll) m--;
pol.p[m++]=pol2.p[i];
}
if(pol2.n>1) m--;
pol.n=m;
}
}
int main(){
return 0;
}
直接convex_hull(polygon)就求好凸包了>_<
凸包就是一个多边形嘛,就是凸的>_<
ZBT的计算几何模板的更多相关文章
- lrj计算几何模板
整理了一下大白书上的计算几何模板. #include <cstdio> #include <algorithm> #include <cmath> #include ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]
题目链接:https://cn.vjudge.net/problem/UVA-12304 题意: 作为题目大合集,有以下一些要求: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三 ...
- hdu 3060 Area2 (计算几何模板)
Problem Description 小白最近又被空军特招为飞行员,参与一项实战演习.演习的内容还是轰炸某个岛屿(这次的岛屿很大,很大很大很大,大到炸弹怎么扔都能完全在岛屿上引爆),看来小白确实是飞 ...
- [转] 计算几何模板Orz
#include<math.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define PI acos(-1.0) ...
- 计算几何模板 (bzoj 1336,poj 2451 ,poj3968)
poj 3968 (bzoj 2642) 二分+半平面交,每次不用排序,这是几个算几版综合. #include<iostream> #include<cstdio> #incl ...
- UVa 11178计算几何 模板题
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...
- BNU校赛总决赛J 小白兔小灰兔 相交计算几何模板
J 小白兔小灰兔 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K Special Judge, 64bit IO Format: %lld 题目描述 ...
- ACM计算几何模板——圆和球
#include <iostream> #include <cmath> using namespace std; #define eps 1e-10 /********** ...
随机推荐
- WCF入门(9)
前言 上次搬家空调出了点问题,和修空调的师傅商量了一下,感觉还是讲理的. 今天又在公司基本没有任何存在感的过了一天,纠结...领导还不在... 前些天往手机里面放了几集WCF入门视频,今天用暴风影音看 ...
- Bootstrap3.0学习第九轮(CSS补充)
详情请查看http://aehyok.com/Blog/Detail/15.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- JavaScript基础---作用域,匿名函数和闭包
匿名函数就是没有名字的函数,闭包是可访问一个函数作用域里变量的函数. 一.匿名函数 //普通函数 function box() { //函数名是 box return 'TT'; } //匿名函数 f ...
- u11-nav02
header:before, header:after ,.navigation:before, .navigation:after,.nav-row:before, .nav-row:after,. ...
- Maven 教程
Maven 教程 序:几次对Maven 的学习,都因为各种原因 而中途切断了,再一次学习的时候,又不得不重新开始,结果发现 又不记得步骤 又找不到对应的文档.别人写的再好,终究比不过自己亲手实践的得出 ...
- 【CodeForces 504A】Misha and Forest
题 题意 有n个点,代号分别为0到n-1,然后这n个点有d个相连点,相连点的XOR sum 为s,求所有的边. 分析 知识:a^b^a=b,a^b^b=a. 把相连点为1的i存进队列,i的唯一相连点就 ...
- 最短路算法floyd
内容: 对n个点(n<=450),已知他们的边,也就是相邻关系,求任意两个点的最短距离. 代码: for(int k=1; k<=n; k++)//k写在外面 for(int i=1; i ...
- Linux虚拟地址空间布局
在多任务操作系统中,每个进程都运行在属于自己的内存沙盘中.这个沙盘就是虚拟地址空间(Virtual Address Space),在32位模式下它是一个4GB的内存地址块.在Linux系统中, 内核进 ...
- 洛谷U5653 宋荣子的小饼干
题目描述 楼下机房的LYL有n个妹子,分别编号为a1,a2……an,每个妹子都拥有一定数量的小饼干.有一天,saruka没有吃晚饭,饿的不要不要的,这时,他忽然想起了LYL的妹子们有小饼干可以吃.于是 ...
- TCP/IP详解 学习三
网际协议 ip Ip 是不可靠和无连接的 ip首部 4个字节的 32 bit值以下面的次序传输:首先是 0-7 bit,其次 8-15 bit,然后 1 6-23 bit,最后是 24~31 bit. ...