Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Curves.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_CURVES_H
22 #define GF_CURVES_H
23 
24 #include <vector>
25 
26 #include "Curve.h"
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  /**
35  * @brief A line
36  *
37  * A line is a straight curve. It is defined by two end points.
38  */
39  class GF_API Line : public Curve {
40  public:
41  /**
42  * @brief Constructor
43  *
44  * @param p0 The first end point
45  * @param p1 The second end point
46  */
47  Line(Vector2f p0, Vector2f p1);
48 
49  virtual std::size_t getPointCount() const override;
50 
51  virtual Vector2f getPoint(std::size_t index) const override;
52 
53  private:
54  Vector2f m_p0;
55  Vector2f m_p1;
56  };
57 
58  /**
59  * @brief A quadratic Bézier curve
60  *
61  * A quadratic Bézier curve is a Bézier curve with two end points and one control point.
62  */
64  public:
65  /**
66  * @brief Constructor
67  *
68  * @param p0 The first end point
69  * @param p1 The control point
70  * @param p2 The second end point
71  * @param pointCount The number of points composing the curve
72  */
73  QuadraticBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, std::size_t pointCount = 20);
74 
75  virtual std::size_t getPointCount() const override;
76 
77  virtual Vector2f getPoint(std::size_t index) const override;
78 
79  private:
80  std::size_t m_pointCount;
81  Vector2f m_p0;
82  Vector2f m_p1;
83  Vector2f m_p2;
84  };
85 
86  /**
87  * @brief A cubic Bézier curve
88  *
89  * A cubic Bézier curve is a Bézier curve with two end points and two control points.
90  */
91  class GF_API CubicBezierCurve : public Curve {
92  public:
93  /**
94  * @brief Constructor
95  *
96  * @param p0 The first end point
97  * @param p1 The first control point
98  * @param p2 The second control point
99  * @param p3 The second end point
100  * @param pointCount The number of points composing the curve
101  */
102  CubicBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount = 30);
103 
104  virtual std::size_t getPointCount() const override;
105 
106  virtual Vector2f getPoint(std::size_t index) const override;
107 
108  private:
109  std::size_t m_pointCount;
110  Vector2f m_p0;
111  Vector2f m_p1;
112  Vector2f m_p2;
113  Vector2f m_p3;
114  };
115 
116  /**
117  * @brief A compound curve
118  *
119  * A compound curve is a curve composed of several continuous curves. It is
120  * sometimes called a path in vector graphics software.
121  */
122  class GF_API CompoundCurve : public Curve {
123  public:
124  /**
125  * @brief Constructor
126  *
127  * @param origin The first point of the curve
128  */
129  CompoundCurve(Vector2f origin = Vector2f(0, 0));
130 
131  /**
132  * @brief Set the first point of the curve
133  *
134  * @param origin The first point of the curve
135  * @return A reference on the current compound curve
136  */
137  CompoundCurve& setOrigin(Vector2f origin);
138 
139  /**
140  * @brief Create a line from the last point to a new point
141  *
142  * @param p1 The new end point
143  */
144  CompoundCurve& lineTo(Vector2f p1);
145 
146  /**
147  * @brief Create a quadratic Bézier curve from the last point to a new point
148  *
149  * @param p1 The control point
150  * @param p2 The new end point
151  * @param pointCount The number of points composing the curve
152  */
153  CompoundCurve& quadraticCurveTo(Vector2f p1, Vector2f p2, std::size_t pointCount = 20);
154 
155  /**
156  * @brief Create a cubic Bézier curve from the last point to a new point
157  *
158  * @param p1 The first control point
159  * @param p2 The second control point
160  * @param p3 The new end point
161  * @param pointCount The number of points composing the curve
162  */
163  CompoundCurve& cubicCurveTo(Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount = 30);
164 
165  /**
166  * @brief Close the curve
167  *
168  * This create a line between the last point and the first point of the curve.
169  */
170  void close();
171 
172  /**
173  * @brief Reset the curve to a new origin
174  *
175  * The curve is open again.
176  *
177  * @param origin The first point of the curve
178  */
179  CompoundCurve& clear(Vector2f origin = Vector2f(0, 0));
180 
181  virtual std::size_t getPointCount() const override;
182 
183  virtual Vector2f getPoint(std::size_t index) const override;
184 
185  private:
186  std::vector<Vector2f> m_points;
187  };
188 
189 #ifndef DOXYGEN_SHOULD_SKIP_THIS
190 }
191 #endif
192 }
193 
194 #endif // GF_CURVES_H
A compound curve.
Definition: Curves.h:122
CompoundCurve & lineTo(Vector2f p1)
Create a line from the last point to a new point.
QuadraticBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, std::size_t pointCount=20)
Constructor.
CubicBezierCurve(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount=30)
Constructor.
void close()
Close the curve.
CompoundCurve & setOrigin(Vector2f origin)
Set the first point of the curve.
A line.
Definition: Curves.h:39
CompoundCurve & quadraticCurveTo(Vector2f p1, Vector2f p2, std::size_t pointCount=20)
Create a quadratic Bézier curve from the last point to a new point.
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:354
A quadratic Bézier curve.
Definition: Curves.h:63
virtual std::size_t getPointCount() const override
Get the total number of points of the curve.
virtual std::size_t getPointCount() const override
Get the total number of points of the curve.
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
A curve is a one dimension object.
Definition: Curve.h:52
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
Definition: Action.h:34
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
A cubic Bézier curve.
Definition: Curves.h:91
CompoundCurve & clear(Vector2f origin=Vector2f(0, 0))
Reset the curve to a new origin.
virtual std::size_t getPointCount() const override
Get the total number of points of the curve.
Line(Vector2f p0, Vector2f p1)
Constructor.
virtual Vector2f getPoint(std::size_t index) const override
Get a point of the curve.
virtual std::size_t getPointCount() const override
Get the total number of points of the curve.
CompoundCurve & cubicCurveTo(Vector2f p1, Vector2f p2, Vector2f p3, std::size_t pointCount=30)
Create a cubic Bézier curve from the last point to a new point.
#define GF_API
Definition: Portability.h:35
CompoundCurve(Vector2f origin=Vector2f(0, 0))
Constructor.