Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Circ.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_CIRC_H
25 #define GF_CIRC_H
26 
27 #include "Portability.h"
28 #include "Vector.h"
29 #include "VectorOps.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  /**
37  * @ingroup core
38  * @brief Utility class for manipulating circles
39  *
40  * A circle is defined by its center and its radius.
41  * It is a very simple class defined for convenience, so
42  * its member variables (`center` and `radius`) are public
43  * and can be accessed directly.
44  *
45  * gf::Circ is a template and may be used with any numeric type, but
46  * for simplicity, some common typedef are defined:
47  *
48  * - gf::CircI with `int` as `T`
49  * - gf::CircU with `unsigned` as `T`
50  * - gf::CircZ with `std::size_t` as `T`
51  * - gf::CircF with `float` as `T`
52  *
53  * So that you don't have to care about the temlate syntax.
54  *
55  * Usage example:
56  *
57  * ~~~{.cc}
58  * // Define a circle, with a center at (10, 10) and a radius of 20
59  * gf::CircI c1({ 10, 10 }, 20);
60  *
61  * // Define a circle with a center at (0, 0) and a radius of 2
62  * gf::CircI c2({ 0, 0 }, 2);
63  *
64  * // Test intersections with the point (3, 1)
65  * bool b1 = c1.contains({ 3, 1 }); // true
66  * bool b2 = c2.contains({ 3, 1 }); // false
67  *
68  * // Test the intersection between c1 and c2
69  * bool b3 = c1.intersects(c2); // true
70  * ~~~
71  */
72  template<typename T>
73  struct Circ {
74  Vector<T, 2> center; ///< Center of the circle
75  T radius; ///< Radius of the circle
76 
77  /**
78  * @brief Default constructor
79  *
80  * Creates an empty circle (it is equivalent to calling
81  * `Circ({ 0, 0 }, 0`).
82  */
83  constexpr Circ() noexcept
84  : center(0, 0)
85  , radius(0)
86  {
87 
88  }
89 
90  /**
91  * @brief Construct the circle from center and radius
92  *
93  * @param circCenter Center of the circle
94  * @param circRadius Radius of the circle
95  */
96  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
97  : center(circCenter)
98  , radius(circRadius)
99  {
100 
101  }
102 
103  /**
104  * @brief Get the center of the circle
105  *
106  * It is a synonym for the `center` member
107  *
108  * @return The center of the circle
109  * @sa getRadius()
110  */
111  constexpr Vector<T, 2> getCenter() const noexcept {
112  return center;
113  }
114 
115  /**
116  * @brief Get the radius of the circle
117  *
118  * It is a synonym for the `radius` member
119  *
120  * @return The radius of the circle
121  * @sa getCenter()
122  */
123  constexpr T getRadius() const noexcept {
124  return radius;
125  }
126 
127  /**
128  * @brief Check if the circle is empty
129  *
130  * An empty circle is a circle with a zero radius.
131  *
132  * @return True if the circle is empty
133  */
134  constexpr bool isEmpty() const noexcept {
135  return radius == 0;
136  }
137 
138  /**
139  * @brief Get the top of the circle
140  *
141  * @return The top of the circle
142  */
143  constexpr Vector<T, 2> getTop() const noexcept {
144  return { center.x, center.y - radius };
145  }
146 
147  /**
148  * @brief Get the bottom of the circle
149  *
150  * @return The bottom of the circle
151  */
152  constexpr Vector<T, 2> getBottom() const noexcept {
153  return { center.x, center.y + radius };
154  }
155 
156  /**
157  * @brief Get the left of the circle
158  *
159  * @return The left of the circle
160  */
161  constexpr Vector<T, 2> getLeft() const noexcept {
162  return { center.x - radius, center.y };
163  }
164 
165  /**
166  * @brief Get the right of the circle
167  *
168  * @return The right of the circle
169  */
170  constexpr Vector<T, 2> getRight() const noexcept {
171  return { center.x + radius, center.y };
172  }
173 
174  /**
175  * @brief Check if a point is insied a circle's area
176  *
177  * @param point Point to test
178  * @return True if the point is inside, false otherwise
179  * @sa intersects()
180  */
181  inline bool contains(const Vector<T, 2>& point) const noexcept {
182  return gf::squareDistance(center, point) <= gf::square(radius);
183  }
184 
185  /**
186  * @brief Check the intersection between two circles
187  *
188  * @param other Circle to test
189  * @return True if circles overlap, false otherwise
190  * @sa contains()
191  */
192  inline bool intersects(const Circ<T>& other) const noexcept {
193  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
194  }
195 
196  };
197 
198 
199  /**
200  * @ingroup core
201  * @brief A `float` circle
202  */
203  using CircF = Circ<float>;
204 
205  /**
206  * @ingroup core
207  * @brief A `int` circle
208  */
209  using CircI = Circ<int>;
210 
211  /**
212  * @ingroup core
213  * @brief A `unsigned` circle
214  */
215  using CircU = Circ<unsigned>;
216 
217  /**
218  * @ingroup core
219  * @brief A `std::size_t` circle
220  */
221  using CircZ = Circ<std::size_t>;
222 
223 // MSVC does not like extern template
224 #ifndef _MSC_VER
225  extern template struct Circ<float>;
226  extern template struct Circ<int>;
227  extern template struct Circ<unsigned>;
228 #endif
229 
230  /**
231  * @relates Circ
232  * @brief Equality operator
233  *
234  * @param lhs First circle
235  * @param rhs Second circle
236  * @return True if the two circles are the same
237  */
238  template<typename T>
239  inline
240  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
241  return lhs.center == rhs.center && lhs.radius == rhs.radius;
242  }
243 
244  /**
245  * @relates Circ
246  * @brief Inequality operator
247  *
248  * @param lhs First circle
249  * @param rhs Second circle
250  * @return True if the two circles are different
251  */
252  template<typename T>
253  inline
254  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
255  return lhs.center != rhs.center || lhs.radius != rhs.radius;
256  }
257 
258 #ifndef DOXYGEN_SHOULD_SKIP_THIS
259 }
260 #endif
261 }
262 
263 
264 #endif // GF_CIRC_H
T radius
Radius of the circle.
Definition: Circ.h:75
constexpr Vector< T, 2 > getBottom() const noexcept
Get the bottom of the circle.
Definition: Circ.h:152
constexpr T getRadius() const noexcept
Get the radius of the circle.
Definition: Circ.h:123
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:161
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:143
bool intersects(const Circ< T > &other) const noexcept
Check the intersection between two circles.
Definition: Circ.h:192
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:83
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:240
Utility class for manipulating circles.
Definition: Circ.h:73
constexpr Vector< T, 2 > getRight() const noexcept
Get the right of the circle.
Definition: Circ.h:170
constexpr bool isEmpty() const noexcept
Check if the circle is empty.
Definition: Circ.h:134
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:254
Definition: Action.h:34
bool contains(const Vector< T, 2 > &point) const noexcept
Check if a point is insied a circle's area.
Definition: Circ.h:181
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the circle.
Definition: Circ.h:111
Vector< T, 2 > center
Center of the circle.
Definition: Circ.h:74
constexpr Circ(const Vector< T, 2 > &circCenter, T circRadius) noexcept
Construct the circle from center and radius.
Definition: Circ.h:96
General purpose math vector.
Definition: Vector.h:60