计算几何模板(未调试,更新中)
计算几何学习中,一边应用一边订正与更新模板
干脆写了一个double的和一个long long(避免实数运算)的
double
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const double eps=1e-7; struct point{ double x,y; point(){} point (double a,double b): x(a),y(b) {} friend point operator + (const point &a,const point &b){ return point(a.x+b.x,a.y+b.y); } friend point operator - (const point &a,const point &b){ return point(a.x-b.x,a.y-b.y); } friend point operator * (const double &r,const point &a){ return point(r*a.x,r*a.y); } friend bool operator == (const point &a,const point &b){ return (abs(a.x-b.x)<eps && abs(a.y-b.y)<eps); } double norm(){ return sqrt(x*x+y*y); } }; inline double det(point a,point b) {return a.x*b.y-a.y*b.x;} inline double dot(point a,point b) {return a.x*b.x+a.y*b.y;} inline double dist(point a,point b) {return (a-b).norm();} inline point rotate(point p,double A) { return point(p.x*cos(A)-p.y*sin(A),p.x*sin(A)+p.y*cos(A)); } inline bool line_cross_segment(point s,point t,point a,point b) { return !(det(s-a,t-a)*det(s-b,t-b)>eps); } inline bool seg_cross_seg(point a,point b,point c,point d) { if (min(c.x,d.x)>max(a.x,b.x) || min(a.x,b.x)>max(c.x,d.x) || min(c.y,d.y)>max(a.y,b.y) || min(a.y,b.y)>max(c.y,d.y)) return false; return det(a-c,d-c)*det(b-c,d-c)<eps && det(c-a,b-a)*det(d-a,b-a)<eps; } inline double p_line_dist(point p,point s,point t) {return det(s-p,t-p)/dist(s,t);} inline point Pro(point p,point s,point t) //垂足 { double r=dot(t-s,p-s)/dot(t-s,t-s); return s+r*(t-s); } inline bool On (point p,point s,point t){ //点在线段上 return (abs(det(p-s,t-s))<eps && dot(p-s,p-t)<eps); } inline double p_segment_dist(point p,point s,point t) { if (On(Pro(p,s,t),s,t)) return p_line_dist(p,s,t); else return min(dist(p,s),dist(p,t)); } inline bool parallel(point a,point b,point c,point d){return abs(det(a-b,c-d))<eps;} inline point line_make_point(point s1,point t1,point s2,point t2) //逻辑上必须先判断parallel { double a1=det(s1-s2,t2-s2); double a2=det(t1-s2,t2-s2); return 1/(a1-a2)*(a1*t1-a2*s1); }
long long
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct point{ long long x,y; point(){} point (long long a,long long b): x(a),y(b) {} friend point operator + (const point &a,const point &b){ return point(a.x+b.x,a.y+b.y); } friend point operator - (const point &a,const point &b){ return point(a.x-b.x,a.y-b.y); } friend point operator * (const long long &r,const point &a){ return point(r*a.x,r*a.y); } friend bool operator == (const point &a,const point &b){ return a.x==b.x && a.y==b.y; } long long sqrnorm(){ return x*x+y*y; } }; inline long long det(point a,point b) {return a.x*b.y-a.y*b.x;} inline long long dot(point a,point b) {return a.x*b.x+a.y*b.y;} inline long long sqrdist(point a,point b) {return (a-b).sqrnorm();} inline bool line_cross_segment(point s,point t,point a,point b) { return det(s-a,t-a)*det(s-b,t-b)<=0; } inline bool seg_cross_seg(point a,point b,point c,point d) { if (min(c.x,d.x)>max(a.x,b.x) || min(a.x,b.x)>max(c.x,d.x) || min(c.y,d.y)>max(a.y,b.y) || min(a.y,b.y)>max(c.y,d.y)) return false; return det(a-c,d-c)*det(b-c,d-c)<=0 && det(c-a,b-a)*det(d-a,b-a)<=0; } inline bool On (point p,point s,point t){ //点在线段上 return det(p-s,t-s)==0 && dot(p-s,p-t)<=0; } inline bool parallel(point a,point b,point c,point d){return det(a-b,c-d)==0;}