Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Polygon.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 #ifndef GF_POLYGON_H
22 #define GF_POLYGON_H
23 
24 #include <vector>
25 
26 #include "ArrayRef.h"
27 #include "Matrix.h"
28 #include "Portability.h"
29 #include "Vector.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  struct Transform;
37 
38  /**
39  * @ingroup core
40  * @brief The direction of a polygon's rotation
41  */
42  enum class Winding {
43  Clockwise, ///< Same direction as a clock's hands
44  Counterclockwise, ///< Opposite direction of a clock's hands
45  };
46 
47  /**
48  * @ingroup core
49  * @brief A convex polygon
50  *
51  */
52  class GF_API Polygon {
53  public:
54  /**
55  * @brief Default constructor
56  */
57  Polygon() = default;
58 
59  /**
60  * @brief Constructor from an array
61  *
62  * @param points The array of points
63  */
64  Polygon(ArrayRef<Vector2f> points);
65 
66  /**
67  * @brief Constructor from points
68  *
69  * @param first Iterator to the first point
70  * @param last Iterator after the last point
71  */
72  template<typename Iterator>
73  Polygon(Iterator first, Iterator last)
74  : m_points(first, last)
75  {
76 
77  }
78 
79  /**
80  * @brief Add a point to the polygon
81  *
82  * @param point The point to add to the polygon
83  */
84  void addPoint(Vector2f point);
85 
86  /**
87  * @brief Get the number of points of the polygon
88  *
89  * @returns The number of points of the polygon
90  */
91  std::size_t getPointCount() const;
92 
93  /**
94  * @brief Get the i-th point of the polygon
95  *
96  * @param index The index of the point
97  */
98  Vector2f getPoint(std::size_t index) const;
99 
100  /**
101  * @brief Get the center of the polygon
102  *
103  * As the polygon is convex, the center is inside the polygon
104  *
105  * @returns The center of the polygon
106  */
107  Vector2f getCenter() const;
108 
109  /**
110  * @brief Get the farthest point in a direction
111  *
112  * @param direction The direction to search (in world coordinates)
113  * @param transform The transformation of the polygon
114  * @returns The farthest point of the polygon in the given direction
115  */
116  Vector2f getSupport(Vector2f direction, const Transform& transform) const;
117 
118  /**
119  * @brief Get the farthest point in a direction
120  *
121  * @param direction The direction to search
122  * @returns The farthest point of the polygon in the given direction
123  */
124  Vector2f getSupport(Vector2f direction) const;
125 
126  /**
127  * @brief Get an iterator to the first point
128  *
129  * @returns A pointer to the first point
130  *
131  * @sa end()
132  */
133  const Vector2f *begin() const;
134 
135  /**
136  * @brief Get an iterator past the last point
137  *
138  * @returns A pointer past the last point
139  *
140  * @sa begin()
141  */
142  const Vector2f *end() const;
143 
144  /**
145  * @brief Check if the polygon is convex
146  *
147  * This function should return true, otherwise you may have problems in
148  * other functions which assume that the polygon is convex.
149  *
150  * @returns True if the polygon is convex
151  */
152  bool isConvex() const;
153 
154  /**
155  * @brief Compute the winding of a convex polygon
156  *
157  * Complexity: @f$ O(n) @f$
158  *
159  * @returns The winding of the convex polygon
160  * @sa gf::Winding, isConvex()
161  * @sa [Curve orientation - Wikipedia](https://en.wikipedia.org/wiki/Curve_orientation)
162  */
163  Winding getWinding() const;
164 
165 
166  /**
167  * @brief Compute the area of the polygon
168  *
169  * Complexity: @f$ O(n) @f$
170  *
171  * @returns The area of the polygon
172  */
173  float getArea() const;
174 
175  /**
176  * @brief Apply a transformation to the polygon
177  *
178  * @param mat The transformation matrix
179  */
180  void applyTransform(const Matrix3f& mat);
181 
182  private:
183  std::vector<Vector2f> m_points;
184  };
185 
186 #ifndef DOXYGEN_SHOULD_SKIP_THIS
187 }
188 #endif
189 }
190 
191 #endif // GF_POLYGON_H
Polygon(Iterator first, Iterator last)
Constructor from points.
Definition: Polygon.h:73
bool isConvex() const
Check if the polygon is convex.
std::size_t getPointCount() const
Get the number of points of the polygon.
const Vector2f * end() const
Get an iterator past the last point.
Winding getWinding() const
Compute the winding of a convex polygon.
A simple transformation (rotation then translation)
Definition: Transform.h:204
Vector2f getSupport(Vector2f direction) const
Get the farthest point in a direction.
Polygon(ArrayRef< Vector2f > points)
Constructor from an array.
const Vector2f * begin() const
Get an iterator to the first point.
void applyTransform(const Matrix3f &mat)
Apply a transformation to the polygon.
Vector2f getSupport(Vector2f direction, const Transform &transform) const
Get the farthest point in a direction.
void addPoint(Vector2f point)
Add a point to the polygon.
Vector2f getCenter() const
Get the center of the polygon.
The namespace for gf classes.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:42
A convex polygon.
Definition: Polygon.h:52
Opposite direction of a clock&#39;s hands.
Vector2f getPoint(std::size_t index) const
Get the i-th point of the polygon.
Polygon()=default
Default constructor.
#define GF_API
Definition: Portability.h:35
Winding
The direction of a polygon&#39;s rotation.
Definition: Polygon.h:42
float getArea() const
Compute the area of the polygon.
Same direction as a clock&#39;s hands.