计算几何点,直线,线段模板

#include 
#include 
#include 
#include 
#include 
#include 
#define PI acos(-1)
using namespace std;
struct Point//点 向量
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
};
typedef  Point  Vector;
//向量使用点作为表示方法 结构相同 为了代码清晰
const double eps = 1e-8;


int dcmp(double x) //三态函数 处理与double零有关的精度问题
{
    if(fabs(x) < eps)    return 0;
    return x<0 ? -1 : 1;
}
//向量运算
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);
}
bool operator == (const Vector& A, const Vector& B)
{
    return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
bool operator < (const Point&a,const Point &b)
{
    return a.x0)return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
//点在直线上的投影
Point GetlineProjection(Point P,Point A,Point B)
{
    Vector v=B-A;
    return A+v*(Dot(v,P-A)/Dot(v,v));
}
//判断线段相交(每条线段的两个端点都在另一条线段的两侧)(含端点的话把<改为<=)
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
//点是否在一条线段上(不含线段的端点)(含端点的话把<改为<=)
bool OnSegment(Point p,Point a1,Point a2)
{
    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
//多边形有向面积 支持非凸多边形
double PolygonArea(Point *p, int n)
{
    double area = 0;
    for(int i=1; i	sort(p,p+n);
	int m = 0;
	for(int i = 0; i 	{
		while(m > 1 && dcmp(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 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
		ch[m++] = p[i];
	}
	if(n > 1) m--;
	return m;
}
//旋转卡壳求凸包最大距离
void RC(Point ch[],int n)
{
   int l=n;
   int q=1;
   int ans=0;
 ch[n]=ch[0];
    for(int p=0;pArea(ch[p+1],ch[q],ch[p]))
            q=(q+1)%l;
        ans=max(ans,max(Dis2(ch[p],ch[q]),Dis2(ch[p+1],ch[q+1])));
     }
  printf("%d\n",ans);
}
//凸包求其重心
Point tubaozhongxin(Point ma[],int n)
{
    int i;
    Point p=ma[0];
    Point a=ma[1];
    double hx=0.0,hy=0.0,sum_area=0.0;
    for(i=2;i

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