The Battle for Wesnoth  1.17.12+dev
point.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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 point& point) const
66  {
67  return {x - point.x, y - point.y};
68  }
69 
70  constexpr point& operator-=(const point& point)
71  {
72  x -= point.x;
73  y -= point.y;
74  return *this;
75  }
76 
77  constexpr point operator*(int s) const
78  {
79  return {x * s, y * s};
80  }
81 
82  constexpr point& operator*=(int s)
83  {
84  x *= s;
85  y *= s;
86  return *this;
87  }
88 
89  constexpr point operator/(int s) const
90  {
91  return {x / s, y / s};
92  }
93 
94  constexpr point& operator/=(int s)
95  {
96  x /= s;
97  y /= s;
98  return *this;
99  }
100 
101  // Multiplication and division of points works elementwise.
102 
103  constexpr point operator*(const point& p) const
104  {
105  return {x * p.x, y * p.y};
106  }
107 
108  constexpr point& operator*=(const point& p)
109  {
110  x *= p.x;
111  y *= p.y;
112  return *this;
113  }
114 
115  constexpr point operator/(const point& p) const
116  {
117  return {x / p.x, y / p.y};
118  }
119 
120  constexpr point& operator/=(const point& p)
121  {
122  x /= p.x;
123  y /= p.y;
124  return *this;
125  }
126 
127 };
128 
129 std::ostream& operator<<(std::ostream& stream, const point& point);
constexpr point & operator/=(int s)
Definition: point.hpp:94
constexpr point operator*(const point &p) const
Definition: point.hpp:103
constexpr point(int x, int y)
Definition: point.hpp:29
constexpr point & operator*=(const point &p)
Definition: point.hpp:108
constexpr point operator*(int s) const
Definition: point.hpp:77
constexpr point operator/(const point &p) const
Definition: point.hpp:115
constexpr point & operator/=(const point &p)
Definition: point.hpp:120
constexpr point operator-(const point &point) const
Definition: point.hpp:65
constexpr point & operator+=(const point &point)
Definition: point.hpp:58
constexpr point()
Initialize to 0 by default.
Definition: point.hpp:27
constexpr point & operator-=(const point &point)
Definition: point.hpp:70
constexpr point & operator*=(int s)
Definition: point.hpp:82
constexpr bool operator<(const point &point) const
Definition: point.hpp:43
constexpr point(const SDL_Point &p)
Definition: point.hpp:31
constexpr point operator+(const point &point) const
Definition: point.hpp:53
mock_party p
static map_location::DIRECTION s
std::ostream & operator<<(std::ostream &stream, const point &point)
Definition: point.cpp:22
constexpr bool operator!=(const point &point) const
Definition: point.hpp:38
Holds a 2D point.
Definition: point.hpp:24
constexpr bool operator<=(const point &point) const
Definition: point.hpp:48
constexpr point operator/(int s) const
Definition: point.hpp:89
constexpr bool operator==(const point &point) const
Definition: point.hpp:33