跳到正文

算法·理论

平面几何模板(包括点、向量、多边形、圆)

wangbinfeng约 18 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 18 分钟

版本

  1. v1当前
C++
namespace Geometry_2D{
//应当注意的是,必须包含cmath/math.h头文件
	const double PI=acos(-1.0);
	template<class T>int dcmp(T x,const double eps=1e-6){return (fabs(x)<eps?0:(x<0?-1:1));}
	template<class T=double>class Vector;template<class T=double>class Line;
	template<class T=double>class Point{
	public:
		T x,y;
		double eps=1e-10;
		Point()=default;
		Point(const T x,const T y,const double eps=1e-10):x(x),y(y),eps(eps){}
		Point(const Point&rhy){x=rhy.x,y=rhy.y,eps=rhy.eps;}
		Point&operator=(const Point&rhy){x=rhy.x,y=rhy.y,eps=rhy.eps;return *this;}
		bool operator==(const Point&rhy)const{return (fabs(x-rhy.x)<eps&&fabs(y-rhy.y)<eps);}
		bool operator<(const Point&rhy)const{return (*this==rhy?false:(fabs(x-rhy.x)<eps?y<rhy.y:x<rhy.x));}
		Point operator+(const Vector<T>&rhy)const{return Point(x+rhy.x,y+rhy.y,eps);}
		Point&operator+=(const Vector<T>&rhy){return Point(x=x+rhy.x,y=y+rhy.y,eps);}
		Point operator+(const Point&rhy)const{return Point(x+rhy.x,y+rhy.y,eps);}
		Vector<T> operator-(const Point&rhy)const{return Vector<T>(x-rhy.x,y-rhy.y,eps);}
		//Line<T> operator-(const Point&rhy)const{return Line<T>(*this,Vector<T>(x-rhy.x,y-rhy.y,eps));}
		Point operator*(const T p)const{return Point(x*p,y*p,eps);}
		Point operator/(const T p)const{return Point(x/p,y/p,eps);}
		T area2(const Point&b,const Point&c)const{return (b-*this)*(c-*this);}
		T getDistanceToPoint(const Point&a)const{return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));}//两点之间距离
		T getDistanceToLine(const Point&a,const Point&b)const{//点到直线的距离
			Vector<T> v1=b-a,v2=*this-a;
			return fabs(v1*v2)/v1.length();
		}
		T getDistanceToSegment(const Point&a,const Point&b){//点到线段的距离
			if(a==b)return (*this-a).length();
			Vector<T> v1=b-a,v2=*this-a,v3=*this-b;
			if(dcmp(v1.dot(v2))<0)return v2.length();
			if(dcmp(v1.dot(v3))>0)return v3.length();
			return fabs((v1*v2)/v1.length());
		}
		Point getLineProjection(const Point&a,const Point&b){//返回点在直线上的投影
			Vector<T>v=b-a;
			return a+v*(v.dot(*this-a),v.dot(v));
		}
		bool onSegment(const Point a1,const Point a2){
			return dcmp((a1-*this)*(a2-*this))==0 && dcmp((a1-*this).dot(a2-*this))<0;
		}
	};
	template<class T>class Vector{
	public:
		T x,y;
		double eps=1e-10;
		Vector()=default;
		Vector(const T x,const T y,const double eps=1e-10):x(x),y(y),eps(eps){}
		Vector(const Vector*rhy){x=rhy.x,y=rhy.y,eps=rhy.eps;}
		Vector&operator=(const Vector*rhy){x=rhy.x,y=rhy.y,eps=rhy.eps;return *this;}
		bool operator==(const Vector&rhy)const{return (fabs(x-rhy.x)<eps&&fabs(y-rhy.y)<eps);}
		Vector operator+(const Vector &rhy)const{return Vector(x+rhy.x,y+rhy.y,eps);}
		Vector&operator+=(const Vector &rhy){return Vector(x=x+rhy.x,y=y+rhy.y,eps);}
		Point<T> operator+(const Point<T> &rhy)const{return Point<T>(x+rhy.x,y+rhy.y,eps);}
		template<typename T2>Vector operator*(const T2&rhy)const{return Vector(x*rhy,y*rhy,eps);}
		template<typename T2>Vector&operator*=(const T2&rhy){return Vector(x=x*rhy,y=y*rhy,eps);}//数乘操作(和auto一样,但为了兼容低版本C++只能用函数模板)
		template<typename T2>Vector operator/(const T2&rhy)const{return Vector(x/rhy,y/rhy,eps);}
		template<typename T2>Vector&operator/=(const T2&rhy){return Vector(x=x/rhy,y=y/rhy,eps);}//数除操作
		T dot(const Vector&rhy)const{return x*rhy.x+y*rhy.y;}
		T length()const{return sqrt(dot(*this));}
		T angle(const Vector&rhy)const{return acos(dot(rhy)/length()/rhy.length());}
		T angle()const{return atan2(y,x);}
		T cross(const Vector&rhy)const{return x*rhy.y-y*rhy.x;}
		T operator*(const Vector&rhy)const{return x*rhy.y-y*rhy.x;}
		Vector operator<<(const double rad)const{return Vector(x*cos(rad)-y*sin(rad),x*sin(rad)+y*cos(rad));}//逆时针
		Vector&operator<<=(const double rad){return *this=*this<<rad;}//逆时针
		Vector rotate(const double rad)const{return *this<<rad;}
		Vector operator>>(const double rad)const{return *this<<-rad;;}//顺时针
		Vector&operator>>=(const double rad){return *this=*this>>rad;}//顺时针
		template<class T2>Line<T2> toLine(const Point<T2> p)const{return Line<T2>(p,*this);}
		T lineAngleDegree()const{
			T ang=angle()*180.0/PI;
			while(dcmp(ang)<0)ang+=360.0;
			while(dcmp(ang-180)>=0)ang-=180.0;
			return ang;
		}
	};
	//template<typename T>Point<T>getLineIntersection(const Point<T>&p,const Vector<T>&v,const Point<T>&q,const Vector<T>&w){//获取两直线交点
	template<typename T>Point<T>getLineIntersection(Point<T>&p,Vector<T>&v,Point<T>&q,Vector<T>&w){//获取两直线交点
		return p+v*(w*(p-q)/(v*w));
	}
/*	template<typename T>bool segmentProperIntersection(const Point<T>&a1,const Point<T>&a2,const Point<T>&b1,const Point<T>&b2,const bool onSegment=true){//判断两线段是否相交(onSegment=true:允许在端点处相交)
		T c1=(a2-a1)*(b1-a1),c2=(a2-a1)*(b2-a1),c3=(b2-b1)*(a1-b1),c4=(b2-b1)*(a2-b1);
		return (dcmp(c1,a1.eps)*dcmp(c2,a1.eps)<0) && (dcmp(c3,a1.eps)*dcmp(c4,a1.eps)<0);
	}*/
	template<int maxn,class T=double>class Polygon{
	public:
		Point<T>p[maxn];
		long long n;
		T Area(){
			T ret=0;
			for(int i=1;i<n-1;i++)ret+=(p[i]-p[0])*(p[i+1]-p[0]);
			return ret/2;
		}
	};
	template<class T>class Line{
		Point<T>getLineIntersection(const Point<T>p,const Vector<T>v,const Point<T>q,const Vector<T>w)const{//获取两直线交点
			return p+v*(w*(p-q)/(v*w));
		}
		Vector<T> Normal(const Vector<T>t)const{
			T l=t.length();
			return Vector<T>(-t.y/l,t.x/l);
		}
	public:
		Point<T>p;Vector<T>v;
		Line(const Point<T>&p,const Vector<T>&v):p(p),v(v){}
		Point<T> point(const T t)const{return p+v*t;}
		Line move(const T d)const{return Line(p+Normal(v)*d,v);}
		int length()const{return v.length();}
		Point<T> getLineIntersection(const Line&rhy)const{return this->getLineIntersection(p,v,rhy.p,rhy.v);}
	};
	template<class T=double>class Circle{
	public:
		Point<T>c;T r;
		Circle()=default;
		Circle(const Point<T> c,const T r):c(c),r(r){}
		Circle(const T x,const T y,const T r):c(Point<T>(x,y)),r(r){}
		Point<T> getPoint(const T a)const{return Point<T>(c.x+cos(a)*r,c.y+sin(a)*r);}
		int getLineCircleIntersection(const Line<T>l,T &t1,T &t2,Point<T>sol[2])const{
			T a=l.v.x,b=l.p.x-this->c.x,c=l.v.y,d=l.p.y-this->c.y,
				e=a*a+c*c,f=2*(a*b+c*d),g=b*b+d*d-this->r*this->r;
			T delta=f*f-4*e*g;
			switch(dcmp(delta)){
				case -1:return 0;//相离
				case 0:	t1=t2=-f/(2*e);sol[0]=l.point(t1);return 1;//相切
				case 1:	t1=(-f-sqrt(delta))/(2*e),sol[0]=l.point(t1),
						t2=(-f+sqrt(delta))/(2*e),sol[1]=l.point(t2);
						return 2;//相交
			}
		}
		template<typename T2>int getCircleCircleIntersection(const Circle<T2>&c,Point<T>sol[2])const{
			T d=(this->c-c.c).length();
			if(dcmp(d)==0){
				if(dcmp(this->r-c.r)==0)return -1;
				return 0;
			}
			if(dcmp(this->r+c.r-d)<0)return 0;
			if(dcmp(fabs(this->r-c.r)-d)>0)return 0;
			T a=(c.c-this->c).angle(),da=acos((this->r*this->r+d*d-c.r*c.r)/(2*this->r*d));
			Point<T> p1=this->getPoint(a-da),p2=this->getPoint(a+da);
			sol[0]=p1;
			if(p1==p2)return 1;
			sol[1]=p2;return 2;
		}
		template<typename T2>int getTangents(const Point<T2>&p,Point<T>v[2])const{
			Vector<T> u=c-p;
			T dist=u.length();
			if(dist<r)return 0;
			if(dcmp(dist-r)==0){v[0]=u.rotate(PI/2.0);return 1;}
			v[0]=u.rotate(-asin(r/dist)),v[1]=u.rotate(asin(r/dist));
			return 2;
		}
		template<typename T2>int getTangents(Circle<T2>&cb,pair<Point<T>,Point<T> >p[4])const{
			int cnt=0;
			Circle ca=*this;
			if(ca.r<cb.r)swap(ca,cb);
			int d2=(ca.c.x-cb.c.x)*(ca.c.x-cb.c.x)+(ca.c.y-cb.c.y)*(ca.c.y-cb.c.y);
			int rdiff=ca.r-cb.r;
			int rsum=ca.r+cb.r;
			if(d2<rdiff*rdiff)return 0;//内含
			T base=atan2(cb.c.y-ca.c.y,cb.c.x-ca.c.x);
			if(d2==0&&ca.r==cb.r)return -1;//重合
			if(d2==rdiff*rdiff){
				p[0].first=ca.getPoint(base),p[0].second=cb.getPoint(base);
				return 1;
			}
			T ang=acos((ca.r-cb.r)/sqrt(d2));
			p[cnt].first=ca.getPoint(base+ang),p[cnt].second=cb.getPoint(base+ang),cnt++;
			p[cnt].first=ca.getPoint(base-ang),p[cnt].second=cb.getPoint(base-ang),cnt++;//两条外公切线
			if(d2==rsum*rsum)p[cnt].first=ca.getPoint(base),p[cnt].second=cb.getPoint(base+PI),cnt++;//一条内公切线
			if(d2>rsum*rsum){
				ang=acos((ca.r+cb.r)/sqrt(d2));
				p[cnt].first=ca.getPoint(base+ang),p[cnt].second=cb.getPoint(base+ang+PI),cnt++;
				p[cnt].first=ca.getPoint(base-ang),p[cnt].second=cb.getPoint(base-ang+PI),cnt++;
			}
			return cnt;
		}
#if defined(_GLIBCXX_UTILITY)
		int getLineCircleIntersection(const Line<T>l,std::pair<T,T>&t,std::pair<Point<T>,Point<T> >&sol)const{
			T t1,t2;
			Point<T>soll[2];
			int ret=getLineCircleIntersection(l,t1,t2,soll);
			switch(ret){
				case 0:return 0;//相离
				case 1:t={t1,t2},sol.first=sol.second=soll[0];return 1;
				case 2:t={t1,t2},sol.first=soll[0],sol.second=soll[1];return 2;
			}
		}
		template<typename T2>int getLineCircleIntersection(const Circle<T2>&c,std::pair<Point<T>,Point<T> >&sol)const{
			Point<T>soll[2];
			int ret=getLineCircleIntersection(c,soll);
			switch(ret){
				case 0:return 0;//相离
				case 1:sol.first=sol.second=soll[0];return 1;
				case 2:sol.first=soll[0],sol.second=soll[1];return 2;
			}
		}
		template<typename T2>int getTangents(const Point<T2>&p,std::pair<Point<T>,Point<T> >v)const{
			Point<T>vv[2];
			int ret=getTangents(p,vv);
			if(ret==0)return 0;
			v={vv[0],vv[1]};
			return ret;
		}
#endif
	};
}
using namespace Geometry_2D;

值得注意的是,为经过测试,如有bug烦请反馈,orz

原文