Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
Circ.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
72  template<typename T>
73  struct Circ {
76 
83  constexpr Circ() noexcept
84  : center(0, 0)
85  , radius(0)
86  {
87 
88  }
89 
96  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
97  : center(circCenter)
98  , radius(circRadius)
99  {
100 
101  }
102 
106  Circ(const Circ&) = default;
107 
111  Circ& operator=(const Circ&) = default;
112 
121  constexpr Vector<T, 2> getCenter() const noexcept {
122  return center;
123  }
124 
133  constexpr T getRadius() const noexcept {
134  return radius;
135  }
136 
144  constexpr bool isEmpty() const noexcept {
145  return radius == 0;
146  }
147 
153  constexpr Vector<T, 2> getTop() const noexcept {
154  return { center.x, center.y - radius };
155  }
156 
162  constexpr Vector<T, 2> getBottom() const noexcept {
163  return { center.x, center.y + radius };
164  }
165 
171  constexpr Vector<T, 2> getLeft() const noexcept {
172  return { center.x - radius, center.y };
173  }
174 
180  constexpr Vector<T, 2> getRight() const noexcept {
181  return { center.x + radius, center.y };
182  }
183 
191  inline bool contains(const Vector<T, 2>& point) const noexcept {
192  return gf::squareDistance(center, point) <= gf::square(radius);
193  }
194 
202  inline bool intersects(const Circ<T>& other) const noexcept {
203  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
204  }
205 
206  };
207 
208 
216 
223  using CircI = Circ<int>;
224 
232 
240 
241 // MSVC does not like extern template
242 #ifndef _MSC_VER
243  extern template struct Circ<float>;
244  extern template struct Circ<int>;
245  extern template struct Circ<unsigned>;
246 #endif
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 T>
271  inline
272  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
273  return lhs.center != rhs.center || lhs.radius != rhs.radius;
274  }
275 
284  template<typename Archive, typename T>
285  Archive& operator|(Archive& ar, Circ<T>& circ) {
286  return ar | circ.center | circ.radius;
287  }
288 
289 #ifndef DOXYGEN_SHOULD_SKIP_THIS
290 }
291 #endif
292 }
293 
294 
295 #endif // GF_CIRC_H
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:509
Archive & operator|(Archive &ar, Circ< T > &circ)
Serialize a circle.
Definition: Circ.h:285
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:162
constexpr T getRadius() const noexcept
Get the radius of the circle.
Definition: Circ.h:133
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:171
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:153
bool intersects(const Circ< T > &other) const noexcept
Check the intersection between two circles.
Definition: Circ.h:202
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:83
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:258
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:520
constexpr T square(T val)
Square function.
Definition: Math.h:275
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:180
constexpr bool isEmpty() const noexcept
Check if the circle is empty.
Definition: Circ.h:144
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:272
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:191
A 2D vector.
Definition: Vector.h:316
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the circle.
Definition: Circ.h:121
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