计算几何整理

计算几何

1. 基本元素

1.1 点 && 向量

1.1.1 定义

.1.2.6 模长
typedef struct Point Vector;
struct Point
{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};

1.1.2 基本操作

1.1.2.1 加法
Vector operator +(const Vector &a,const Vector &b)
{
	return Vector(a.x+b.x,a.y+b.y);
}
1.1.2.2 减法
Vector operator -(const Vector &a,const Vector &b)
{
	return Vector(a.x-b.x,a.y-b.y);
}
1.1.2.3 数乘
Vector operator *(const double &phi,const Vector &a)
{
	return Vector(phi*a.x,phi*a.y);
}
1.1.2.4 点积
double operator *(const Vector &a,const Vector &b)
{
	return a.x*b.x+a.y*b.y;
}
1.1.2.5 叉积
double operator ^(const Vector &a,const Vector &b)
{
	return a.x*b.y-a.y*b.x;
}
1.1.2.6 模长
double Length(const Vector &a)
{
	return sqrt(a*a);
}
double Length2(const Vector &a)
{
	return a*a;
}
1.1.2.7 调整长度
Vector Adjust(const double &l,const Vector &a)
{
	return a*(l/Length(a));
}
1.1.2.8 两点间距离
double Dist(const Point &a,const Point &b)
{
	return len(a-b);
}
1.1.2.9 投影
double Shade(const Vector &a,const Vector &b)
{
	return a*b/Length(a);
}
1.1.2.10 垂直(右手向)
Vector Perpendicular(const Vector &a)
{
	return Vector(a.y,-a.x);
}

1.2 线

1.2.1 定义

typedef struct Line Segment,HalfLine;
struct Line
{
	Point p;
	Vector v;
	Line(Point p,Vector v):p(p),v(v){}
};

1.2.2 基本操作

1.2.2.1 点到直线距离
double DistLine(const Point &a,const Line &b)
{
	double x=Length2(a-b.p),y=Shade(a-b.p,b.v);
	return sqrt(x-y*y);
}
1.2.2.2 判断两直线是否相交
posted @ 2018-03-31 18:24  Capella  阅读(188)  评论(1编辑  收藏  举报

谢谢光临