Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Ball.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_BALL_H
25 #define GF_BALL_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 
45  template<typename T, std::size_t N>
46  struct Ball {
49 
55  constexpr Ball() noexcept
56  : center(gf::Zero)
57  , radius(T(0))
58  {
59  }
60 
67  constexpr Ball(const Vector<T, N>& ballCenter, T ballRadius) noexcept
68  : center(ballCenter)
69  , radius(ballRadius)
70  {
71 
72  }
73 
82  constexpr Vector<T, N> getCenter() const noexcept {
83  return center;
84  }
85 
94  constexpr T getRadius() const noexcept {
95  return radius;
96  }
97 
105  constexpr bool isEmpty() const noexcept {
106  return radius == T(0);
107  }
108 
116  inline bool contains(const Vector<T, N>& point) const noexcept {
117  return gf::squareDistance(center, point) <= gf::square(radius);
118  }
119 
127  inline bool intersects(const Ball<T, N>& other) const noexcept {
128  return gf::squareDistance(center, other.center) <= gf::square(radius + other.radius);
129  }
130 
131  };
132 
141  template<typename T, std::size_t N>
142  constexpr
143  bool operator==(const Ball<T, N>& lhs, const Ball<T, N>& rhs) {
144  return lhs.center == rhs.center && lhs.radius == rhs.radius;
145  }
146 
155  template<typename T, std::size_t N>
156  constexpr
157  bool operator!=(const Ball<T, N>& lhs, const Ball<T, N>& rhs) {
158  return lhs.center != rhs.center || lhs.radius != rhs.radius;
159  }
160 
169  template<typename Archive, typename T, std::size_t N>
170  Archive& operator|(Archive& ar, Ball<T, N>& ball) {
171  return ar | ball.center | ball.radius;
172  }
173 
174 #ifndef DOXYGEN_SHOULD_SKIP_THIS
175 }
176 #endif
177 }
178 
179 
180 #endif // GF_BALL_H
constexpr Vector< T, N > getCenter() const noexcept
Get the center of the ball.
Definition: Ball.h:82
bool intersects(const Ball< T, N > &other) const noexcept
Check the intersection between two balls.
Definition: Ball.h:127
Archive & operator|(Archive &ar, Ball< T, N > &ball)
Serialize a ball.
Definition: Ball.h:170
A n-dimension ball.
Definition: Ball.h:46
constexpr T square(T val)
Square function.
Definition: Math.h:289
constexpr bool operator!=(const Ball< T, N > &lhs, const Ball< T, N > &rhs)
Inequality operator.
Definition: Ball.h:157
constexpr T getRadius() const noexcept
Get the radius of the ball.
Definition: Ball.h:94
The namespace for gf classes.
Definition: Action.h:35
constexpr bool operator==(const Ball< T, N > &lhs, const Ball< T, N > &rhs)
Equality operator.
Definition: Ball.h:143
T radius
Radius of the ball.
Definition: Ball.h:48
constexpr bool isEmpty() const noexcept
Check if the ball is empty.
Definition: Ball.h:105
constexpr Ball(const Vector< T, N > &ballCenter, T ballRadius) noexcept
Construct the ball from center and radius.
Definition: Ball.h:67
General purpose math vector.
Definition: Vector.h:61
Vector< T, N > center
Center of the ball.
Definition: Ball.h:47
bool contains(const Vector< T, N > &point) const noexcept
Check if a point is insied a ball&#39;s area.
Definition: Ball.h:116
constexpr Ball() noexcept
Default constructor.
Definition: Ball.h:55