Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Polygon.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 #ifndef GF_POLYGON_H
22 #define GF_POLYGON_H
23 
24 #include <vector>
25 
26 #include "ArrayRef.h"
27 #include "Math.h"
28 #include "Matrix.h"
29 #include "Portability.h"
30 #include "SerializationFwd.h"
31 #include "Vector.h"
32 #include "Winding.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  struct Transform;
40 
46  class GF_API Polygon {
47  public:
51  Polygon() = default;
52 
59 
66  template<typename Iterator>
67  Polygon(Iterator first, Iterator last)
68  : m_points(first, last)
69  {
70 
71  }
72 
80  bool isEmpty() const;
81 
87  void addPoint(Vector2f point);
88 
94  std::size_t getPointCount() const;
95 
101  Vector2f getPoint(std::size_t index) const;
102 
110  Vector2f getCenter() const;
111 
119  Vector2f getSupport(Vector2f direction, const Transform& transform) const;
120 
127  Vector2f getSupport(Vector2f direction) const;
128 
136  const Vector2f *begin() const;
137 
145  const Vector2f *end() const;
146 
157  bool isConvex() const;
158 
168  Winding getWinding() const;
169 
170  bool contains(Vector2f point) const;
171 
179  float getArea() const;
180 
186  void applyTransform(const Matrix3f& mat);
187 
193  void simplify(float distance = Epsilon);
194 
195  private:
196  std::vector<Vector2f> m_points;
197  };
198 
203  GF_API Serializer& operator|(Serializer& ar, const Polygon& polygon);
204 
209  GF_API Deserializer& operator|(Deserializer& ar, Polygon& polygon);
210 
211 #ifndef DOXYGEN_SHOULD_SKIP_THIS
212 }
213 #endif
214 }
215 
216 #endif // GF_POLYGON_H
A deserializer from a binary file.
Definition: Serialization.h:153
Polygon(Iterator first, Iterator last)
Constructor from points.
Definition: Polygon.h:67
constexpr Vector2f transform(const Matrix3f &mat, Vector2f point)
Apply an affine transformation to a 2D point.
Definition: Transform.h:323
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:96
A simple transformation (rotation then translation)
Definition: Transform.h:200
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
A constant reference to an array and its size.
Definition: ArrayRef.h:43
A convex polygon.
Definition: Polygon.h:46
Winding
The direction of a polygon&#39;s rotation.
Definition: Winding.h:33