Gamedev Framework (gf)  0.12.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 
73  template<typename T>
74  struct Circ {
77 
84  constexpr Circ() noexcept
85  : center(0, 0)
86  , radius(0)
87  {
88 
89  }
90 
97  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
98  : center(circCenter)
99  , radius(circRadius)
100  {
101 
102  }
103 
107  Circ(const Circ&) = default;
108 
112  Circ& operator=(const Circ&) = default;
113 
122  constexpr Vector<T, 2> getCenter() const noexcept {
123  return center;
124  }
125 
134  constexpr T getRadius() const noexcept {
135  return radius;
136  }
137 
145  constexpr bool isEmpty() const noexcept {
146  return radius == 0;
147  }
148 
154  constexpr Vector<T, 2> getTop() const noexcept {
155  return { center.x, center.y - radius };
156  }
157 
163  constexpr Vector<T, 2> getBottom() const noexcept {
164  return { center.x, center.y + radius };
165  }
166 
172  constexpr Vector<T, 2> getLeft() const noexcept {
173  return { center.x - radius, center.y };
174  }
175 
181  constexpr Vector<T, 2> getRight() const noexcept {
182  return { center.x + radius, center.y };
183  }
184 
192  inline bool contains(const Vector<T, 2>& point) const noexcept {
193  return gf::squareDistance(center, point) <= gf::square(radius);
194  }
195 
203  inline bool intersects(const Circ<T>& other) const noexcept {
204  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
205  }
206 
207  };
208 
209 
217 
225 
232  using CircI = Circ<int>;
233 
241 
249 
250 // MSVC does not like extern template
251 #ifndef _MSC_VER
252  extern template struct GF_API Circ<float>;
253  extern template struct GF_API Circ<double>;
254  extern template struct GF_API Circ<int>;
255  extern template struct GF_API Circ<unsigned>;
256 #endif
257 
266  template<typename T>
267  inline
268  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
269  return lhs.center == rhs.center && lhs.radius == rhs.radius;
270  }
271 
280  template<typename T>
281  inline
282  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
283  return lhs.center != rhs.center || lhs.radius != rhs.radius;
284  }
285 
294  template<typename Archive, typename T>
295  Archive& operator|(Archive& ar, Circ<T>& circ) {
296  return ar | circ.center | circ.radius;
297  }
298 
299 #ifndef DOXYGEN_SHOULD_SKIP_THIS
300 }
301 #endif
302 }
303 
304 
305 #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:295
T radius
Radius of the circle.
Definition: Circ.h:76
constexpr Vector< T, 2 > getBottom() const noexcept
Get the bottom of the circle.
Definition: Circ.h:163
constexpr T getRadius() const noexcept
Get the radius of the circle.
Definition: Circ.h:134
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:172
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:154
bool intersects(const Circ< T > &other) const noexcept
Check the intersection between two circles.
Definition: Circ.h:203
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:84
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:268
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:74
constexpr Vector< T, 2 > getRight() const noexcept
Get the right of the circle.
Definition: Circ.h:181
constexpr bool isEmpty() const noexcept
Check if the circle is empty.
Definition: Circ.h:145
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:282
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:192
A 2D vector.
Definition: Vector.h:316
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the circle.
Definition: Circ.h:122
Vector< T, 2 > center
Center of the circle.
Definition: Circ.h:75
constexpr Circ(const Vector< T, 2 > &circCenter, T circRadius) noexcept
Construct the circle from center and radius.
Definition: Circ.h:97