#ifndef VECTOR_H // -*- c++ -*- #define VECTOR_H /// // Copyright (C) 2002 - 2004, Fredrik Arnerup & Rasmus Kaj, See COPYING /// #include // max #include // sqrt template C sqr(const C& c) { return c*c; } template struct VectorOf { C x, y; VectorOf(): x(C()), y(C()) {} // the zero vector VectorOf(C xx, C yy): x(xx), y(yy) {} VectorOf& operator += (const VectorOf& d) { x += d.x; y += d.y; return *this; } VectorOf& operator /= (const C& d) { x /= d; y /= d; return *this; } }; typedef VectorOf Vector; typedef VectorOf IVector; template inline VectorOf operator - (const VectorOf& a, const VectorOf b) { return VectorOf(a.x - b.x, a.y - b.y); } template inline VectorOf operator + (const VectorOf& a, const VectorOf b) { return VectorOf(a) += b; } template inline VectorOf operator / (const VectorOf& a, const C& b) { return VectorOf(a) /= b; } template inline std::ostream& operator << (std::ostream& out, const VectorOf v) { return out << v.x << ',' << v.y; } template inline C dist(const VectorOf& a, const VectorOf& b) { return C(sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))); } template inline C chessboard_dist(const VectorOf& a, const VectorOf& b) { return C(std::max(fabs(a.x - b.x), fabs(a.y - b.y))); } #endif