Gamedev Framework (gf)  0.2.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 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  /**
36  * @ingroup core
37  * @brief Utility class for manipulating circles
38  *
39  * A circle is defined by its center and its radius.
40  * It is a very simple class defined for convenience, so
41  * its member variables (`center` and `radius`) are public
42  * and can be accessed directly.
43  *
44  * gf::Circ is a template and may be used with any numeric type, but
45  * for simplicity, some common typedef are defined:
46  *
47  * - gf::CircI with `int` as `T`
48  * - gf::CircU with `unsigned` as `T`
49  * - gf::CircZ with `std::size_t` as `T`
50  * - gf::CircF with `float` as `T`
51  *
52  * So that you don't have to care about the temlate syntax.
53  *
54  * Usage example:
55  *
56  * ~~~{.cc}
57  * // Define a circle, with a center at (10, 10) and a radius of 20
58  * gf::CircI c1({ 10, 10 }, 20);
59  *
60  * // Define a circle with a center at (0, 0) and a radius of 2
61  * gf::CircI c2({ 0, 0 }, 2);
62  *
63  * // Test intersections with the point (3, 1)
64  * bool b1 = c1.contains({ 3, 1 }); // true
65  * bool b2 = c2.contains({ 3, 1 }); // false
66  *
67  * // Test the intersection between c1 and c2
68  * bool b3 = c1.intersects(c2); // true
69  * ~~~
70  */
71  template<typename T>
72  struct Circ {
73  Vector<T, 2> center; ///< Center of the circle
74  T radius; ///< Radius of the circle
75 
76  /**
77  * @brief Default constructor
78  *
79  * Creates an empty circle (it is equivalent to calling
80  * `Circ({ 0, 0 }, 0`).
81  */
82  constexpr Circ() noexcept
83  : center(0, 0)
84  , radius(0)
85  {
86 
87  }
88 
89  /**
90  * @brief Construct the circle from center and radius
91  *
92  * @param circCenter Center of the circle
93  * @param circRadius Radius of the circle
94  */
95  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
96  : center(circCenter)
97  , radius(circRadius)
98  {
99 
100  }
101 
102  /**
103  * @brief Get the center of the circle
104  *
105  * It is a synonym for the `center` member
106  *
107  * @return The center of the circle
108  * @sa getRadius()
109  */
110  constexpr Vector<T, 2> getCenter() const noexcept {
111  return center;
112  }
113 
114  /**
115  * @brief Get the radius of the circle
116  *
117  * It is a synonym for the `radius` member
118  *
119  * @return The radius of the circle
120  * @sa getCenter()
121  */
122  constexpr T getRadius() const noexcept {
123  return radius;
124  }
125 
126  /**
127  * @brief Check if the circle is empty
128  *
129  * An empty circle is a circle with a zero radius.
130  *
131  * @return True if the circle is empty
132  */
133  constexpr bool isEmpty() const noexcept {
134  return radius == 0;
135  }
136 
137  /**
138  * @brief Get the top of the circle
139  *
140  * @return The top of the circle
141  */
142  constexpr Vector<T, 2> getTop() const noexcept {
143  return { center.x, center.y - radius };
144  }
145 
146  /**
147  * @brief Get the bottom of the circle
148  *
149  * @return The bottom of the circle
150  */
151  constexpr Vector<T, 2> getBottom() const noexcept {
152  return { center.x, center.y + radius };
153  }
154 
155  /**
156  * @brief Get the left of the circle
157  *
158  * @return The left of the circle
159  */
160  constexpr Vector<T, 2> getLeft() const noexcept {
161  return { center.x - radius, center.y };
162  }
163 
164  /**
165  * @brief Get the right of the circle
166  *
167  * @return The right of the circle
168  */
169  constexpr Vector<T, 2> getRight() const noexcept {
170  return { center.x + radius, center.y };
171  }
172 
173  /**
174  * @brief Check if a point is insied a circle's area
175  *
176  * @param point Point to test
177  * @return True if the point is inside, false otherwise
178  * @sa intersects()
179  */
180  inline bool contains(const Vector<T, 2>& point) const noexcept {
181  return gf::squareDistance(center, point) <= gf::square(radius);
182  }
183 
184  /**
185  * @brief Check the intersection between two circles
186  *
187  * @param other Circle to test
188  * @return True if circles overlap, false otherwise
189  * @sa contains()
190  */
191  inline bool intersects(const Circ<T>& other) const noexcept {
192  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
193  }
194 
195  };
196 
197 
198  /**
199  * @ingroup core
200  * @brief A `float` circle
201  */
202  using CircF = Circ<float>;
203 
204  /**
205  * @ingroup core
206  * @brief A `int` circle
207  */
208  using CircI = Circ<int>;
209 
210  /**
211  * @ingroup core
212  * @brief A `unsigned` circle
213  */
214  using CircU = Circ<unsigned>;
215 
216  /**
217  * @ingroup core
218  * @brief A `std::size_t` circle
219  */
220  using CircZ = Circ<std::size_t>;
221 
222 // MSVC does not like extern template
223 #ifndef _MSC_VER
224  extern template struct Circ<float>;
225  extern template struct Circ<int>;
226  extern template struct Circ<unsigned>;
227 #endif
228 
229  /**
230  * @relates Circ
231  * @brief Equality operator
232  *
233  * @param lhs First circle
234  * @param rhs Second circle
235  * @return True if the two circles are the same
236  */
237  template<typename T>
238  inline
239  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
240  return lhs.center == rhs.center && lhs.radius == rhs.radius;
241  }
242 
243  /**
244  * @relates Circ
245  * @brief Inequality operator
246  *
247  * @param lhs First circle
248  * @param rhs Second circle
249  * @return True if the two circles are different
250  */
251  template<typename T>
252  inline
253  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
254  return lhs.center != rhs.center || lhs.radius != rhs.radius;
255  }
256 
257 #ifndef DOXYGEN_SHOULD_SKIP_THIS
258 }
259 #endif
260 }
261 
262 
263 #endif // GF_CIRC_H
T radius
Radius of the circle.
Definition: Circ.h:74
constexpr Vector< T, 2 > getBottom() const noexcept
Get the bottom of the circle.
Definition: Circ.h:151
constexpr T getRadius() const noexcept
Get the radius of the circle.
Definition: Circ.h:122
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:160
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:142
bool intersects(const Circ< T > &other) const noexcept
Check the intersection between two circles.
Definition: Circ.h:191
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:82
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:239
Utility class for manipulating circles.
Definition: Circ.h:72
constexpr Vector< T, 2 > getRight() const noexcept
Get the right of the circle.
Definition: Circ.h:169
constexpr bool isEmpty() const noexcept
Check if the circle is empty.
Definition: Circ.h:133
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:253
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:180
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the circle.
Definition: Circ.h:110
Vector< T, 2 > center
Center of the circle.
Definition: Circ.h:73
constexpr Circ(const Vector< T, 2 > &circCenter, T circRadius) noexcept
Construct the circle from center and radius.
Definition: Circ.h:95
General purpose math vector.
Definition: Vector.h:61