Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Polygon.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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  /**
37  * @brief A convex polygon
38  *
39  */
40  class GF_API Polygon {
41  public:
42  /**
43  * @brief Default constructor
44  */
45  Polygon() = default;
46 
47  /**
48  * @brief Constructor from an array
49  *
50  * @param points The array of points
51  */
52  Polygon(ArrayRef<Vector2f> points);
53 
54  /**
55  * @brief Constructor from points
56  *
57  * @param first Iterator to the first point
58  * @param last Iterator after the last point
59  */
60  template<typename Iterator>
61  Polygon(Iterator first, Iterator last)
62  : m_points(first, last)
63  {
64 
65  }
66 
67  /**
68  * @brief Add a point to the polygon
69  *
70  * @param point The point to add to the polygon
71  */
72  void addPoint(Vector2f point);
73 
74  /**
75  * @brief Get the number of points of the polygon
76  *
77  * @returns The number of points of the polygon
78  */
79  std::size_t getPointCount() const;
80 
81  /**
82  * @brief Get the i-th point of the polygon
83  *
84  * @param index The index of the point
85  */
86  Vector2f getPoint(std::size_t index) const;
87 
88  /**
89  * @brief Get the center of the polygon
90  *
91  * As the polygon is convex, the center is inside the polygon
92  *
93  * @returns The center of the polygon
94  */
95  Vector2f getCenter() const;
96 
97  /**
98  * @brief Get the farthest point in a direction
99  *
100  * @param direction The direction to search
101  * @returns The farthest point of the polygon in the given direction
102  */
103  Vector2f getSupport(Vector2f direction) const;
104 
105  /**
106  * @brief Get an iterator to the first point
107  *
108  * @returns A pointer to the first point
109  *
110  * @sa end()
111  */
112  const Vector2f *begin() const;
113 
114  /**
115  * @brief Get an iterator past the last point
116  *
117  * @returns A pointer past the last point
118  *
119  * @sa begin()
120  */
121  const Vector2f *end() const;
122 
123  /**
124  * @brief Apply a transformation to the polygon
125  *
126  * @param mat The transformation matrix
127  */
128  void applyTransform(const Matrix3f& mat);
129 
130  private:
131  std::vector<Vector2f> m_points;
132  };
133 
134 #ifndef DOXYGEN_SHOULD_SKIP_THIS
135 }
136 #endif
137 }
138 
139 #endif // GF_POLYGON_H
Polygon(Iterator first, Iterator last)
Constructor from points.
Definition: Polygon.h:61
std::size_t getPointCount() const
Get the number of points of the polygon.
Vector2f getSupport(Vector2f direction) const
Get the farthest point in a direction.
const Vector2f * begin() const
Get an iterator to the first point.
Polygon(ArrayRef< Vector2f > points)
Constructor from an array.
Vector2f getCenter() const
Get the center of the polygon.
void applyTransform(const Matrix3f &mat)
Apply a transformation to the polygon.
void addPoint(Vector2f point)
Add a point to the polygon.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:41
Vector2f getPoint(std::size_t index) const
Get the i-th point of the polygon.
A convex polygon.
Definition: Polygon.h:40
Polygon()=default
Default constructor.
#define GF_API
Definition: Portability.h:35
const Vector2f * end() const
Get an iterator past the last point.