The Battle for Wesnoth  1.19.5+dev
point.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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. This is a thin wrapper over SDL_Point. */
24 struct point : SDL_Point
25 {
26  /** Initialize to 0 by default. */
27  constexpr point() : SDL_Point{0, 0} {}
28 
29  constexpr point(int x, int y) : SDL_Point{x, y} {}
30 
31  constexpr point(const SDL_Point& p) : SDL_Point{p} {}
32 
33  constexpr bool operator==(const point& point) const
34  {
35  return x == point.x && y == point.y;
36  }
37 
38  constexpr bool operator!=(const point& point) const
39  {
40  return !operator==(point);
41  }
42 
43  constexpr bool operator<(const point& point) const
44  {
45  return std::tie(x, y) < std::tie(point.x, point.y);
46  }
47 
48  constexpr bool operator<=(const point& point) const
49  {
50  return x < point.x || (x == point.x && y <= point.y);
51  }
52 
53  constexpr point operator+(const point& point) const
54  {
55  return {x + point.x, y + point.y};
56  }
57 
58  constexpr point& operator+=(const point& point)
59  {
60  x += point.x;
61  y += point.y;
62  return *this;
63  }
64 
65  constexpr point operator-() const
66  {
67  return {-x, -y};
68  }
69 
70  constexpr point operator-(const point& point) const
71  {
72  return {x - point.x, y - point.y};
73  }
74 
75  constexpr point& operator-=(const point& point)
76  {
77  x -= point.x;
78  y -= point.y;
79  return *this;
80  }
81 
82  constexpr point operator*(int s) const
83  {
84  return {x * s, y * s};
85  }
86 
87  constexpr point& operator*=(int s)
88  {
89  x *= s;
90  y *= s;
91  return *this;
92  }
93 
94  constexpr point operator/(int s) const
95  {
96  return {x / s, y / s};
97  }
98 
99  constexpr point& operator/=(int s)
100  {
101  x /= s;
102  y /= s;
103  return *this;
104  }
105 
106  // Multiplication and division of points works elementwise.
107 
108  constexpr point operator*(const point& p) const
109  {
110  return {x * p.x, y * p.y};
111  }
112 
113  constexpr point& operator*=(const point& p)
114  {
115  x *= p.x;
116  y *= p.y;
117  return *this;
118  }
119 
120  constexpr point operator/(const point& p) const
121  {
122  return {x / p.x, y / p.y};
123  }
124 
125  constexpr point& operator/=(const point& p)
126  {
127  x /= p.x;
128  y /= p.y;
129  return *this;
130  }
131 
132 };
133 
134 std::ostream& operator<<(std::ostream& stream, const point& point);
std::ostream & operator<<(std::ostream &stream, const point &point)
Definition: point.cpp:22
Holds a 2D point.
Definition: point.hpp:25
constexpr bool operator==(const point &point) const
Definition: point.hpp:33
constexpr bool operator!=(const point &point) const
Definition: point.hpp:38
constexpr point operator+(const point &point) const
Definition: point.hpp:53
constexpr point operator/(const point &p) const
Definition: point.hpp:120
constexpr point operator*(const point &p) const
Definition: point.hpp:108
constexpr point operator-() const
Definition: point.hpp:65
constexpr point & operator*=(int s)
Definition: point.hpp:87
constexpr point & operator/=(int s)
Definition: point.hpp:99
constexpr bool operator<(const point &point) const
Definition: point.hpp:43
constexpr point & operator+=(const point &point)
Definition: point.hpp:58
constexpr point()
Initialize to 0 by default.
Definition: point.hpp:27
constexpr point operator*(int s) const
Definition: point.hpp:82
constexpr point & operator/=(const point &p)
Definition: point.hpp:125
constexpr bool operator<=(const point &point) const
Definition: point.hpp:48
constexpr point(const SDL_Point &p)
Definition: point.hpp:31
constexpr point(int x, int y)
Definition: point.hpp:29
constexpr point & operator-=(const point &point)
Definition: point.hpp:75
constexpr point operator/(int s) const
Definition: point.hpp:94
constexpr point & operator*=(const point &p)
Definition: point.hpp:113
constexpr point operator-(const point &point) const
Definition: point.hpp:70
mock_party p
static map_location::direction s