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. WCF 入门(15)

    前言 度过了一个阳光明媚的周末. 一个阴霾的周日夜晚. 第15集 WCF里面的异常处理(1)  Exception handling in WCF 大致看了一下,这个WCF视频里面总共有6集,这个是第 ...

  2. Javascript基础系列之(七)函数(定义和调运函数)

    函数是一个可以随时运行的语句,简单说,函数是完成某个功能的一组语句,它接受0或者多个参数. 函数的基本语法如下 function functionName([arg0,arg1,......argN] ...

  3. c#中的protected和internal

    protected限制子类访问,可以跨程序集 internal 限制此程序集访问,可以跨类 protected internal 限制此程序集的子类中访问

  4. OVER(PARTITION BY)函数介绍

    问题场景 最近在项目中遇到了对每一个类型进行求和并且求该类型所占的比例,当时考虑求出每种类型的和,并在java中分别对每一种类型的和与总和相除求出所占比例.后来,想到这样有点麻烦,并且项目中持久层使用 ...

  5. NS图绘制工具推荐

    世界上要画NS图的人肯定很少,这种无聊的东西= = 我根据个人经验和直觉,推荐三个套工具. 一.签字笔(铅笔+橡皮)+作业纸+拍照的手机 鉴于我以前手绘版ns图已经找不到了,就用室友之前画的做个例子. ...

  6. 【转】pageX、clientX、screenX、offsetX、layerX、x

    参考:http://www.cnblogs.com/xesam/archive/2011/12/08/2280509.html chrome: e.pageX——相对整个页面的坐标e.layerX—— ...

  7. Spring Boot - fish

    1. @RestController combines @Controller and @ResponseBody, 这是不是意味着不用再import jakson的包(@ResponseBody时用 ...

  8. 【poj1182】 食物链

    http://poj.org/problem?id=1182 (题目链接) 题意 中文题 Solution 带权并查集. 神犇博客,秒懂 fa记录父亲,r记录与父亲的关系.%3运用的很巧妙. 代码 / ...

  9. python urllib2使用心得

    python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...

  10. ASP.NET MVC ActionFilterAttribute的执行顺序

    http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...