Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Circ.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 
111  constexpr Vector<T, 2> getCenter() const noexcept {
112  return center;
113  }
114 
123  constexpr T getRadius() const noexcept {
124  return radius;
125  }
126 
134  constexpr bool isEmpty() const noexcept {
135  return radius == 0;
136  }
137 
143  constexpr Vector<T, 2> getTop() const noexcept {
144  return { center.x, center.y - radius };
145  }
146 
152  constexpr Vector<T, 2> getBottom() const noexcept {
153  return { center.x, center.y + radius };
154  }
155 
161  constexpr Vector<T, 2> getLeft() const noexcept {
162  return { center.x - radius, center.y };
163  }
164 
170  constexpr Vector<T, 2> getRight() const noexcept {
171  return { center.x + radius, center.y };
172  }
173 
181  inline bool contains(const Vector<T, 2>& point) const noexcept {
182  return gf::squareDistance(center, point) <= gf::square(radius);
183  }
184 
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 
206 
213  using CircI = Circ<int>;
214 
222 
230 
231 // MSVC does not like extern template
232 #ifndef _MSC_VER
233  extern template struct Circ<float>;
234  extern template struct Circ<int>;
235  extern template struct Circ<unsigned>;
236 #endif
237 
246  template<typename T>
247  inline
248  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
249  return lhs.center == rhs.center && lhs.radius == rhs.radius;
250  }
251 
260  template<typename T>
261  inline
262  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
263  return lhs.center != rhs.center || lhs.radius != rhs.radius;
264  }
265 
266 #ifndef DOXYGEN_SHOULD_SKIP_THIS
267 }
268 #endif
269 }
270 
271 
272 #endif // GF_CIRC_H
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:470
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:248
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:471
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: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:262
The namespace for gf classes.
Definition: Action.h:34
bool contains(const Vector< T, 2 > &point) const noexcept
Check if a point is insied a circle&#39;s area.
Definition: Circ.h:181
A 2D vector.
Definition: Vector.h:298
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