【计算几何】【计算几何模板】【持续更新】

必须学计算几何了……

听说学计算几何要有好模板!

慢慢补ing……

Code:

#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const double eps=1e-7;
struct Point{
	double x,y;
	Point(double _x=0,double _y=0):
		x(_x),y(_y){}
	bool operator<(const Point &p)const{
		return x<p.x||x==p.x&&y<p.y;
	}
};
Point readPoint(){
	Point p;
	scanf("%lf%lf",&p.x,&p.y);
	return p;
}
void printPoint(Point p,char ch='\n'){
	printf("%lf %lf%c",p.x,p.y,ch);
}
typedef Point Vector;
Vector operator+(Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator-(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator*(Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator/(Vector A,double p){return Vector(A.x/p,A.y/p);}
int dcmp(double x){
	if(fabs(x)<eps)return 0;
	return x>0?1:-1;
}
bool operator==(Vector A,Vector B){return !dcmp(A.x-B.x)&&!dcmp(A.y-B.y);}
double sqr(double x){return x*x;}
double Dis(Point A,Point B){return sqrt(sqr(A.x-B.x)+sqr(A.y-B.y));}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Area(Point A,Point B,Point C){Vector u=B-A,v=B-C;return fabs(Cross(u,v))/2;}
double Length(Vector A){return sqrt(A.x*A.x+A.y*A.y);}
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}
Vector Rot(Vector A,double rad){return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A){double Len=Length(A);return Vector(-A.y/Len,A.x/Len);}
struct Line{
	Point p;Vector v;
	Line(Point _p=Point(0,0),Vector _v=Vector(0,0)):
		p(_p),v(_v){}
};
Point LineCross(Line A,Line B){
	Vector u=A.p-B.p;
	double t=Cross(B.v,u)/Cross(A.v,B.v);
	return A.p+A.v*t;
}
double Distance(Point P,Line L){
	Point A=L.p,B=L.p+L.v;
	Vector u=P-A,v=B-A;
	return fabs(Cross(u,v))/Length(v);
}
struct Circle{
	Point c;
	double r;
	Circle(Point _c,double _r):
		c(_c),r(_r){}
	Point point(double rad){
		return Point(c.x+r*cos(rad),c.y+r*sin(rad));
	}
};
vector<Point> LineCrossCircle(Line L,Circle C){
	Line CP(C.c,Normal(L.v));
	Point P=LineCross(CP,L);
	double d=Distance(P,L);
	if(dcmp(C.r-d)==-1)return vector<Point>();
	double le=sqrt(sqr(C.r)-sqr(d));
	vector<Point>v;
	v.push_back(P-L.v/Length(L.v)*le);
	v.push_back(P+L.v/Length(L.v)*le);
	return v;
}
int ConvexHull(Point *p,int n,Point *ch){
	sort(p,p+n);
	int m=0;
	for(int i=0;i<n;i++){
		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
		ch[m++]=p[i];
	}int k=m;
	for(int i=n-2;i>=0;i--){
		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
		ch[m++]=p[i];
	}return m;
}
double Area(Point *ch,int n){
	double ans=0;
	for(int i=1;i<n-1;i++)
	ans+=Area(ch[0],ch[i],ch[i+1]);
	return ans;
}
bool Onleft(Line L,Point p){
	return Cross(L.v,p-L.p)>0;
}
int main(){
	
	return 0;
}








你可能感兴趣的:(计算几何)