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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#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.x<b.x||(a.x==b.x&&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 Dis(Point A, Point B)//两点距离
{
    return sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}
//向量长度
double Length(Vector A)
{
    return sqrt(Dot(A,A));
}
//向量夹角
double Angle(Vector A, Vector B)
{
    return acos(Dot(A,B) / Length(A) / Length(B));
}
// 三角形有向面积两倍
double Area2(Point A, Point B, Point C)
{
    return Cross(B-A,C-A);
}
//向量旋转 rad为弧度
Vector Rotate(Vector A, double rad)
{
    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//单位法线 (左转90度后的单位向量)
Vector Normal(Vector A)
{
    //调用需确定A为非零向量
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}


//直线向量参数方程 P+tv P为点,v为单位向量 (v长度无用)
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)//获取直线交点
{
    //调用应保证P,Q有交点 : 即 Cross(v,w)!=0
    Vector u = P-Q;
    double t = Cross(w,u) / Cross(v,w);
    return P+v*t;
}


//点到直线距离 使用叉积 即平行四边形的面积除以底
double DisToLine(Point P, Point A, Point B)
{
    Vector v1 = B - A, v2 = P - A;
    return fabs(Cross(v1,v2)) / Length(v1); //不取绝对值是有向距离
}
//点到线段的距离
double DistanceToSegment(Point P,Point A, Point B)
{
    if(A==B)return Length(P-A);
    Vector v1=B-A,v2=P-A,v3=P-B;
    if(dcmp(Dot(v1,v2))<0)return Length(v2);
    else if(dcmp(Dot(v1,v3))>0)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<n-1; i++)
        area+=Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
}
//凸包周长
double PolygonCircle(Point *p, int n)
{
    double circle = 0;
    p[n]=p[0];
    for(int i=0; i<n; i++)
        circle+=Dis(p[i],p[i+1]);
    return circle;
}
//三角形面积||判断两直线的方向
double Area(Point a,Point b,Point c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int Dis2(Point a,Point b)
{
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//求凸包
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 && 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;p<l;p++)
    {
        while(Area(ch[p+1],ch[q+1],ch[p])>Area(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<n;i++)
    {
        Point b=ma[i];
        double sa=Area(p,a,b);
        hx+=(p.x+a.x+b.x)*sa;
        hy+=(p.y+a.y+b.y)*sa;


        sum_area+=sa;
        a=b;
    }
    Point t;
    t.x=hx/sum_area/3; t.y=hy/sum_area/3;
    return t;
}
int main()
{
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值