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的计算几何模板的更多相关文章

  1. lrj计算几何模板

    整理了一下大白书上的计算几何模板. #include <cstdio> #include <algorithm> #include <cmath> #include ...

  2. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  3. UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]

    题目链接:https://cn.vjudge.net/problem/UVA-12304 题意: 作为题目大合集,有以下一些要求: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三 ...

  4. hdu 3060 Area2 (计算几何模板)

    Problem Description 小白最近又被空军特招为飞行员,参与一项实战演习.演习的内容还是轰炸某个岛屿(这次的岛屿很大,很大很大很大,大到炸弹怎么扔都能完全在岛屿上引爆),看来小白确实是飞 ...

  5. [转] 计算几何模板Orz

    #include<math.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define PI acos(-1.0) ...

  6. 计算几何模板 (bzoj 1336,poj 2451 ,poj3968)

    poj 3968 (bzoj 2642) 二分+半平面交,每次不用排序,这是几个算几版综合. #include<iostream> #include<cstdio> #incl ...

  7. UVa 11178计算几何 模板题

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...

  8. BNU校赛总决赛J 小白兔小灰兔 相交计算几何模板

    J 小白兔小灰兔 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K Special Judge, 64bit IO Format: %lld 题目描述 ...

  9. ACM计算几何模板——圆和球

    #include <iostream> #include <cmath> using namespace std; #define eps 1e-10 /********** ...

随机推荐

  1. js中的with语句

    javascript中的with语句是什么?       with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象.   看起来 ...

  2. html5_canvas-记忆力卡片游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. poj3237 树链剖分 暴力

    NEGATE a,b 将a b间的线段取反,这题应该用线段树+成段更新.我成段更新写的挫了,试了暴力修改过了(数据水). 也是简单的题目.不过要注意点和边的区别. #include<queue& ...

  4. java 关键字 transient

    一个对象实现了Serilizable 接口,该对象就可以被序列化. 然而在实际开发工程中,我们会遇到,这个类的有些属性不需要序列化,比如包含用户的敏感信息(如密码),为了安全起见,不希望在网络操作(主 ...

  5. ORACLE在存储过程中记录日志的处理包

    Java开发过程中一般使用LOG4J来将程序的运行日志记录到文件中,在ORACLE存储过程中也需要记录日志,我将工作中自己整理的一个记录日志的包分享出来,其实很简单,希望大家多提意见. 一.表结构 为 ...

  6. myEclipse中新建的项目导入到Eclipse之后项目出现一个红色的叉叉

    1.在eclipse中打开Problems,然后看看报哪些错,

  7. The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法

    添加 xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/sc ...

  8. c++内存分配(new和delete)

    c中malloc和free是函数,包含在stdlib.h头文件中,分配成功返回指针,失败返回空指针. 与new的区别是: 1,malloc与free是C++/C语言的标准库函数,new/delete是 ...

  9. c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常

    #include"stdafx.h" #include"string" #include<iostream> #include<vector& ...

  10. 安卓系统源码编译系列(六)——单独编译内置浏览器WebView教程

    原文                   http://blog.csdn.net/zhaoxy_thu/article/details/18883015                 本文主要对从 ...