算法競賽入門經典訓練指南-4.1學習筆記

(1)平面座標系下,向量和點一樣也用x,y表示,等於向量的起點到終點的位移,也相當於把起點平移到座標原點後終點的座標。

//向量基本運算代碼
struct Point
{
	double x, y;
	Point(double = 0, double y = 0):x(x), y(y){ }
};

typedef Point Vector;//從程序實現上,Vector只是Point的別名

//向量+向量=向量, 點+向量=點
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 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;//fabs(x)x的浮點數絕對值
	else 
		return x < 0 ? -1 : 1;
}

bool operator ==(const Point& a, const Point& b)
{
	return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章