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