Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Polygon.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 "CoreApi.h"
27#include "Math.h"
28#include "Matrix.h"
29#include "PointSequence.h"
30#include "SerializationFwd.h"
31#include "Span.h"
32#include "Vector.h"
33#include "Winding.h"
34
35namespace gf {
36#ifndef DOXYGEN_SHOULD_SKIP_THIS
37inline namespace v1 {
38#endif
39
40 struct Transform;
41
47 class GF_CORE_API Polygon : public PointSequence {
48 public:
52 Polygon() = default;
53
60 : PointSequence(points)
61 {
62 }
63
70 template<typename Iterator>
71 Polygon(Iterator first, Iterator last)
72 : PointSequence(first, last)
73 {
74 }
75
84
91 Vector2f getSupport(Vector2f direction) const;
92
103 bool isConvex() const;
104
115
122 bool contains(Vector2f point) const;
123
131 float getArea() const;
132
139 Vector2f getPrevPoint(std::size_t i) const;
140
147 Vector2f getNextPoint(std::size_t i) const;
148 };
149
154 inline
155 Serializer& operator|(Serializer& ar, const Polygon& polygon) {
156 return ar | static_cast<const PointSequence&>(polygon);
157 }
158
163 inline
165 return ar | static_cast<PointSequence&>(polygon);
166 }
167
168#ifndef DOXYGEN_SHOULD_SKIP_THIS
169}
170#endif
171}
172
173#endif // GF_POLYGON_H
A deserializer from a binary file.
Definition: Serialization.h:151
Deserializer & operator|(Deserializer &ar, Polygon &polygon)
Deserialize a polygon.
Definition: Polygon.h:164
A sequence of points.
Definition: PointSequence.h:44
A convex polygon.
Definition: Polygon.h:47
Vector2f getPrevPoint(std::size_t i) const
Get the point before the i-th point.
Polygon(Iterator first, Iterator last)
Constructor from points.
Definition: Polygon.h:71
Vector2f getSupport(Vector2f direction, const Transform &transform) const
Get the farthest point in a direction.
bool contains(Vector2f point) const
Test if a point is inside the polygon.
bool isConvex() const
Check if the polygon is convex.
Polygon(Span< const Vector2f > points)
Constructor from an array.
Definition: Polygon.h:59
Winding getWinding() const
Compute the winding of a simple polygon.
float getArea() const
Compute the area of the polygon.
Vector2f getNextPoint(std::size_t i) const
Get the point after the i-th point.
Polygon()=default
Default constructor.
Vector2f getSupport(Vector2f direction) const
Get the farthest point in a direction.
A serializer to a binary file.
Definition: Serialization.h:43
Serializer & operator|(Serializer &ar, const Polygon &polygon)
Serialize a polygon.
Definition: Polygon.h:155
A span.
Definition: Span.h:414
Winding
The direction of a polygon's rotation.
Definition: Winding.h:33
constexpr Vector2f transform(const Matrix3f &mat, Vector2f point)
Apply an affine transformation to a 2D point.
Definition: Transform.h:326
The namespace for gf classes.
A simple transformation (rotation then translation)
Definition: Transform.h:203