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 /********** ...
随机推荐
- java--- Map详解
Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...
- jQuery Mobile学习日记(二)
首先依HTML5方式加载,DOCTYPE表示格式为HTML5:主要适用于iPhone.Android等,viewport表示适用于移动终端的适中显示,initial-scale缩放比例在1.0~1.3 ...
- Mathematical operation
(1)Using let let result=2+1 let result=2-1 let result=2*1 let result=2/1(2) Using bracket echo $(($p ...
- 【HDU 5105】Math Problem
题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...
- yii授权
ACF (访问控制过滤器) 在你控制器的添加下列的 行为 方法 use yii\filters\AccessControl; class DefaultController extends Contr ...
- python 学习5--matplotlib画图实践
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...
- Blog Explanation
有疑问或blog中说明错误的欢迎评论或联系QQ:30056882,邮箱BLADEVIL@outlook.com.本人水平不高,欢迎讨论问题. blog中所有随笔均为原创,转载请注明来源. (已退役.)
- BZOJ-2037 Sue的小球 DP+费用提前
似乎很早时学长考过很类似的? 2037: [Sdoi2008]Sue的小球 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 558 Solved: 300 ...
- [NOIP2009] 提高组 洛谷P1073 最优贸易
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- ZOJ 2110 Tempter of the Bone
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...