算法竞赛入门经典训练指南-4.1学习笔记

来源:互联网 发布:中国第四人口普查数据 编辑:程序博客网 时间:2024/05/17 05:12

(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;}




0 0
原创粉丝点击