您的位置:首页 > 其它

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

2014-05-23 20:57 232 查看
(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: