The Battle for Wesnoth  1.15.1+dev
point.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <SDL2/SDL_rect.h>
18 
19 #include <iosfwd>
20 #include <tuple>
21 
22 /** Holds a 2D point. */
23 struct point
24 {
26  : x(0)
27  , y(0)
28  {
29  }
30 
31  point(const int x_, const int y_)
32  : x(x_)
33  , y(y_)
34  {
35  }
36 
37  point(const SDL_Point& p)
38  : x(p.x)
39  , y(p.y)
40  {
41  }
42 
43  /** x coordinate. */
44  int x;
45 
46  /** y coordinate. */
47  int y;
48 
49  /** Allow implicit conversion to SDL_Point. */
50  operator SDL_Point() const;
51 
52  bool operator==(const point& point) const
53  {
54  return x == point.x && y == point.y;
55  }
56 
57  bool operator!=(const point& point) const
58  {
59  return !operator==(point);
60  }
61 
62  bool operator<(const point& point) const
63  {
64  return std::tie(x, y) < std::tie(point.x, point.y);
65  }
66 
67  bool operator<=(const point& point) const
68  {
69  return x < point.x || (x == point.x && y <= point.y);
70  }
71 
72  point operator+(const point& point) const
73  {
74  return {x + point.x, y + point.y};
75  }
76 
77  point& operator+=(const point& point);
78 
79  point operator-(const point& point) const
80  {
81  return {x - point.x, y - point.y};
82  }
83 
84  point& operator-=(const point& point);
85 };
86 
87 std::ostream& operator<<(std::ostream& stream, const point& point);
bool operator==(const point &point) const
Definition: point.hpp:52
point operator-(const point &point) const
Definition: point.hpp:79
point(const SDL_Point &p)
Definition: point.hpp:37
point & operator+=(const point &point)
Definition: point.cpp:26
int x
x coordinate.
Definition: point.hpp:44
point()
Definition: point.hpp:25
point operator+(const point &point) const
Definition: point.hpp:72
bool operator!=(const point &point) const
Definition: point.hpp:57
bool operator<(const point &point) const
Definition: point.hpp:62
mock_party p
point & operator-=(const point &point)
Definition: point.cpp:33
std::ostream & operator<<(std::ostream &stream, const point &point)
Definition: point.cpp:40
Holds a 2D point.
Definition: point.hpp:23
bool operator<=(const point &point) const
Definition: point.hpp:67
point(const int x_, const int y_)
Definition: point.hpp:31
int y
y coordinate.
Definition: point.hpp:47