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