计算几何入门向

这是一个计算几何入门的教程。

 

与计算几何密切相关的就是坐标系,坐标系上的点可以用下面数据结构实现。

struct Point
{
	double x,y;
	Point(int x,int y) :x(x),y(y){};
};
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.y/p);
} 

我们也可以用STL的虚数类来实现点和向量。

typedef complex<double> Point;
typedef Point Vector;

double Dot(Vector A,Vector B)
{
	return real(B*conj(A));
}

double Cross(Vector A,Vector B)
{
	return imag(B*conj(A));
}

这样我们就可以直接使用自带的构造函数和重载的运算符,我们只需自己实现点乘和叉乘(同样很简单,设A为a+b i,则Conj(A)=a - b  i),可以思考一下为什么是这样写。

这样实现很简单,很好记忆,但效率并不是很高。

posted @ 2017-12-24 21:36  wdvxdr  阅读(146)  评论(0编辑  收藏  举报