Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Circ.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 
60  template<typename T>
61  struct Circ {
64 
71  constexpr Circ() noexcept
72  : center(gf::Zero)
73  , radius(T(0))
74  {
75  }
76 
83  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
84  : center(circCenter)
85  , radius(circRadius)
86  {
87  }
88 
97  constexpr Vector<T, 2> getCenter() const noexcept {
98  return center;
99  }
100 
109  constexpr T getRadius() const noexcept {
110  return radius;
111  }
112 
120  constexpr bool isEmpty() const noexcept {
121  return radius == T(0);
122  }
123 
131  inline bool contains(const Vector<T, 2>& point) const noexcept {
132  return gf::squareDistance(center, point) <= gf::square(radius);
133  }
134 
142  inline bool intersects(const Circ& other) const noexcept {
143  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
144  }
145 
151  constexpr Vector<T, 2> getTop() const noexcept {
152  return this->center - gf::diry(this->radius);
153  }
154 
160  constexpr Vector<T, 2> getBottom() const noexcept {
161  return this->center + gf::diry(this->radius);
162  }
163 
169  constexpr Vector<T, 2> getLeft() const noexcept {
170  return this->center - gf::dirx(this->radius);
171  }
172 
178  constexpr Vector<T, 2> getRight() const noexcept {
179  return this->center + gf::dirx(this->radius);
180  }
181 
182 
183  };
184 
185 
193 
201 
208  using CircI = Circ<int>;
209 
217 
225 
226 // MSVC does not like extern template
227 #ifndef _MSC_VER
228  extern template struct GF_API Circ<float>;
229  extern template struct GF_API Circ<double>;
230  extern template struct GF_API Circ<int>;
231  extern template struct GF_API Circ<unsigned>;
232 #endif
233 
242  template<typename T>
243  inline
244  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
245  return lhs.center == rhs.center && lhs.radius == rhs.radius;
246  }
247 
256  template<typename T>
257  inline
258  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
259  return lhs.center != rhs.center || lhs.radius != rhs.radius;
260  }
261 
270  template<typename Archive, typename T>
271  Archive& operator|(Archive& ar, Circ<T>& circ) {
272  return ar | circ.center | circ.radius;
273  }
274 
275 #ifndef DOXYGEN_SHOULD_SKIP_THIS
276 }
277 #endif
278 }
279 
280 
281 #endif // GF_CIRC_H
Archive & operator|(Archive &ar, Circ< T > &circ)
Serialize a circle.
Definition: Circ.h:271
T radius
Radius of the circle.
Definition: Circ.h:63
constexpr Vector< T, 2 > getBottom() const noexcept
Get the bottom of the circle.
Definition: Circ.h:160
constexpr T getRadius() const noexcept
Get the radius of the circle.
Definition: Circ.h:109
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:169
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:151
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:71
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:244
constexpr T square(T val)
Square function.
Definition: Math.h:301
Utility class for manipulating circles.
Definition: Circ.h:61
constexpr Vector< T, 2 > getRight() const noexcept
Get the right of the circle.
Definition: Circ.h:178
constexpr bool isEmpty() const noexcept
Check if the circle is empty.
Definition: Circ.h:120
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:258
The namespace for gf classes.
Definition: Action.h:35
bool contains(const Vector< T, 2 > &point) const noexcept
Check if a point is insied a circle&#39;s area.
Definition: Circ.h:131
A 2D vector.
Definition: Vector.h:316
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the circle.
Definition: Circ.h:97
Vector< T, 2 > center
Center of the circle.
Definition: Circ.h:62
constexpr Circ(const Vector< T, 2 > &circCenter, T circRadius) noexcept
Construct the circle from center and radius.
Definition: Circ.h:83
bool intersects(const Circ &other) const noexcept
Check the intersection between two circles.
Definition: Circ.h:142