#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct Point
{
double x,y;
Point(double x,double y):x(x),y(y){}
Point(){}
};
//点类和构造函数,方便编写代码
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.x/p);
}
//点的除法等于两点除以同一值
bool operator<(const Point &a, const Point & b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
//方便坐标比较大小,
const double eps = 1e-10;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
//精度比较,当不等于精度的时候,返回x是正还是负
bool operator== (const Point & a, const Point & b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
//精度比较大小,含精度epsdouble
Dot(Vector a,Vector b)
{
return a.x*b.x+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));
}
//两向量的夹角,两向量的点积 = 两向量的模长 * 向量的夹角的余弦,返回的弧度
Vector Rotate(Vector a, double rad)
{
return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
//向量的旋转,向量绕着起点旋转,旋转后的坐标公式 x =a.x*cos(rad)-a.y*sin(rad) , y =a.x*sin(rad)+a.y*cos(rad),rad都为弧度
double Cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
//向量的叉乘,意义组成平行四边形的面积,可判断点的顺逆时针
double Area2(Point a,Point b,Point c)
{
return Cross(b-a,c-a);
}
//a,b,c组成面积的二倍
Vector Normal(Vector a)
{
double l = Length(a);
return Vector(-a.y/l,a.x/l);
}
//向量的单位法向量,左转90度,长度归一化,单位向量即该向量除以自己的模长
double DistanceToLine(Point p,Point a,Point b)
{
Vector v = b-a;double a = Cross(v,p-a);return fabs(a) / Length(v);
}
//点到直线的距离,面积/低
double DistanceToSegment(Point p,Point a,Point b)
{
if(a == b) return Length(p-a);
Vector v1 = p-a,v2 = b - a,v3 = p-b;
if(dcmp(Dot(v1,v2)) < 0) return Length(v1);
else if(dcmp(v1,v3) > 0) return Length(v3);
else return Cross(v2,v1) / Length(v2);
}
//点到线段的距离,如果不在延长线上,在是点到直线的距离,如果点在ab延长线,那么距离为点到b的距离,反之,点到a的距离
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u = P - Q;
double t = Cross(w,u) / Cross(v,w);
return P+v*t;
}
//两直线相交求交点
二位计算几何基础
最新推荐文章于 2024-03-15 23:00:00 发布