C++: 二次元座標操作用クラス
ひとまず、必要最低限な機能のみ。
class Point { public: int x, y; Point(int x=0, int y=0) : x(x), y(y) {} Point operator+(Point const& rhs) const { return Point(x + rhs.x, y + rhs.y); } Point operator+=(Point const& rhs) { x += rhs.x; y += rhs.y; return *this; } Point operator-(Point const& rhs) const { return Point(x - rhs.x, y - rhs.y); } Point operator-=(Point const& rhs) { x -= rhs.x; y -= rhs.y; return *this; } Point operator*(int n) const { return Point(x * n, y * n); } Point operator*=(int n) { x *= n; y *= n; return *this; } Point operator/(int n) const { return Point(x / n, y / n); } Point operator/=(int n) { x /= n; y /= n; return *this; } bool operator==(Point const& rhs) const { return x == rhs.x && y == rhs.y; } double euclidean_distance(Point const& rhs) const { return std::sqrt((x - rhs.x) * (x - rhs.x) + (y - rhs.y) * (y - rhs.y)); } int manhattan_distance(Point const& rhs) const { return std::abs(x - rhs.x) + std::abs(y - rhs.y); } }; std::ostream & operator<<(std::ostream & stream, Point const& rhs) { stream << "Point(" << rhs.x << ", " << rhs.y << ")"; return stream; } Point gDirection[] = { Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1) }; class Rect { public: Point bottom_left, top_right; Rect(Point bottom_left=Point(), Point top_right=Point()) : bottom_left(bottom_left), top_right(top_right) {} bool contains(Point const& p) const { return p.x >= bottom_left.x && p.x <= top_right.x && p.y >= bottom_left.y && p.y <= top_right.y; } int width() const { return top_right.x - bottom_left.x; } int height() const { return top_right.y - bottom_left.y; } int area() const { return width() * height(); } };