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 /********** ...
随机推荐
- GDI+中发生一般性错误的解决办法(转帖)
今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常.于是开始“ ...
- OC和Java的比较
1.Cocoa是什么?Cocoa是使用OC语言编写的工具包,里面有大量的类库.结构体,说白了其实就相当于java中的标准API.C++中的标准库.OC中没有命名空间的概念,所以使用加前缀来防止命名冲突 ...
- Ibatis中传List参数
Ibatis中用list传参数的方式. Java代码 select count(id) from `user` where id in #[]# and status=1 . <select ...
- 处理Json数据中的日期类型.如/Date(1415169703000)/格式
在asp.net mvc后台返回到视图中的json数据中想对数据进行操作,发现日期类型无法直接进行操作,需要转换为指定格式才行.在网上也搜了下方法也很多,觉得有点麻烦,最终使用正则搞定了,分享下: v ...
- 【BZOJ-2809】dispatching派遣 Splay + 启发式合并
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2334 Solved: 1192[Submi ...
- BZOJ-1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法+乱搞
1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 1383 Solved: 7 ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
- poj1056 (Trie入门)寻找字符串前缀
题意:给你一堆字符串,问是否满足对于任意两个字符串a.b,a不是b的前缀 字典树==前缀树==Trie树 trie入门题,只用到了insert和query操作 #include <cstdio& ...
- Linux数据包路由原理、Iptables/netfilter入门学习
相关学习资料 https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html http://zh.wik ...
- 设定JS全局的公共变量
1. 新建一个标签文件 javaScriptVariables.tag 新建一个标签文件 javaScriptVariables.tag放在 %/HelloWorldWebPro/webroot/WE ...