Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Polyline.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_POLYLINE_H
22 #define GF_POLYLINE_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  * @ingroup core
38  * @brief A polyline
39  *
40  * A polyline is a set of consecutive segments. It is defined by the points
41  * ending the segments. A polyline can be open (chain) or closed (loop).
42  */
43  class GF_API Polyline {
44  public:
45  enum Type {
48  };
49 
50  /**
51  * @brief Default constructor
52  *
53  * @param type The type of polyline (default: chain)
54  */
56  : m_type(type)
57  {
58 
59  }
60 
61  /**
62  * @brief Constructor from an array
63  *
64  * @param points The array of points
65  * @param type The type of polyline (default: chain)
66  */
67  Polyline(ArrayRef<Vector2f> points, Type type = Chain)
68  : m_points(points.begin(), points.end())
69  , m_type(type)
70  {
71 
72  }
73 
74  /**
75  * @brief Constructor from points
76  *
77  * @param first Iterator to the first point
78  * @param last Iterator after the last point
79  * @param type The type of polyline (default: chain)
80  */
81  template<typename Iterator>
82  Polyline(Iterator first, Iterator last, Type type = Chain)
83  : m_points(first, last)
84  , m_type(type)
85  {
86 
87  }
88 
89  /**
90  * @brief Add a point to the polyline
91  *
92  * @param point The point to add to the polyline
93  */
94  void addPoint(Vector2f point);
95 
96  /**
97  * @brief Get the number of points of the polyline
98  *
99  * @returns The number of points of the polyline
100  */
101  std::size_t getPointCount() const;
102 
103  /**
104  * @brief Get the i-th point of the polyline
105  *
106  * @param index The index of the point
107  */
108  Vector2f getPoint(std::size_t index) const;
109 
110  /**
111  * @brief Get an iterator to the first point
112  *
113  * @returns A pointer to the first point
114  *
115  * @sa end()
116  */
117  const Vector2f *begin() const;
118 
119  /**
120  * @brief Get an iterator past the last point
121  *
122  * @returns A pointer past the last point
123  *
124  * @sa begin()
125  */
126  const Vector2f *end() const;
127 
128  /**
129  * @brief Check if there is a point before the i-th point
130  *
131  * @param i The index of the current point
132  * @returns True if there is a point before the current point
133  *
134  * @sa getPrevPoint()
135  */
136  bool hasPrevPoint(std::size_t i) const;
137 
138  /**
139  * @brief Get the point before the i-th point
140  *
141  * @param i The index of the current point
142  * @returns The point before the current point
143  *
144  * @sa hasPrevPoint()
145  */
146  Vector2f getPrevPoint(std::size_t i) const;
147 
148  /**
149  * @brief Check if there is a point after the i-th point
150  *
151  * @param i The index of the current point
152  * @returns True if there is a point after the current point
153  *
154  * @sa getNextPoint()
155  */
156  bool hasNextPoint(std::size_t i) const;
157 
158  /**
159  * @brief Get the point after the i-th point
160  *
161  * @param i The index of the current point
162  * @returns The point after the current point
163  *
164  * @sa hasNextPoint()
165  */
166  Vector2f getNextPoint(std::size_t i) const;
167 
168  /**
169  * @brief Set the type of the polyline
170  *
171  * @param type The new type of the polyline
172  */
173  void setType(Type type) {
174  m_type = type;
175  }
176 
177  /**
178  * @brief Check is the polyline is a loop
179  *
180  * @returns True if the polyline is a loop
181  *
182  * @sa isChain(), setType()
183  */
184  bool isLoop() const {
185  return m_type == Loop;
186  }
187 
188  /**
189  * @brief Check is the polyline is a chain
190  *
191  * @returns True if the polyline is a chain
192  *
193  * @sa isLoop(), setType()
194  */
195  bool isChain() const {
196  return m_type == Chain;
197  }
198 
199  private:
200  std::vector<Vector2f> m_points;
201  Type m_type;
202  };
203 
204 #ifndef DOXYGEN_SHOULD_SKIP_THIS
205 }
206 #endif
207 }
208 
209 #endif // GF_POLYLINE_H
void setType(Type type)
Set the type of the polyline.
Definition: Polyline.h:173
Polyline(ArrayRef< Vector2f > points, Type type=Chain)
Constructor from an array.
Definition: Polyline.h:67
Definition: Polyline.h:47
std::size_t getPointCount() const
Get the number of points of the polyline.
bool hasPrevPoint(std::size_t i) const
Check if there is a point before the i-th point.
Polyline(Iterator first, Iterator last, Type type=Chain)
Constructor from points.
Definition: Polyline.h:82
Definition: Polyline.h:46
A polyline.
Definition: Polyline.h:43
Vector2f getPrevPoint(std::size_t i) const
Get the point before the i-th point.
Vector2f getPoint(std::size_t index) const
Get the i-th point of the polyline.
bool isLoop() const
Check is the polyline is a loop.
Definition: Polyline.h:184
const Vector2f * end() const
Get an iterator past the last point.
Vector2f getNextPoint(std::size_t i) const
Get the point after the i-th point.
Polyline(Type type=Chain)
Default constructor.
Definition: Polyline.h:55
constexpr const T * end() const noexcept
Get an iterator past the last element.
Definition: ArrayRef.h:157
bool isChain() const
Check is the polyline is a chain.
Definition: Polyline.h:195
The namespace for gf classes.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:42
Type
Definition: Polyline.h:45
constexpr const T * begin() const noexcept
Get an iterator to the first element.
Definition: ArrayRef.h:146
void addPoint(Vector2f point)
Add a point to the polyline.
bool hasNextPoint(std::size_t i) const
Check if there is a point after the i-th point.
const Vector2f * begin() const
Get an iterator to the first point.
#define GF_API
Definition: Portability.h:35