Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Geometry.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_GEOMETRY_H
22 #define GF_GEOMETRY_H
23 
24 #include <vector>
25 
26 #include "ArrayRef.h"
27 #include "Heightmap.h"
28 #include "Portability.h"
29 #include "Random.h"
30 #include "Vector.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  /**
38  * @ingroup core
39  * @brief State for the Bresenham's line algorithm
40  *
41  * @sa gf::generateLine()
42  * @sa [Bresenham's line algorithm - Wikipedia](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)
43  */
44  class Bresenham {
45  public:
46  /**
47  * @brief Constructor
48  *
49  * @param p0 The first point of the line
50  * @param p1 The last point of the line
51  */
52  Bresenham(Vector2i p0, Vector2i p1);
53 
54  /**
55  * @brief Generate a new point on the line
56  *
57  * @param res The resulting point if one was generated
58  * @returns True if the algorithm is finished
59  */
60  bool step(Vector2i& res);
61 
62  private:
63  Vector2i m_p0;
64  Vector2i m_p1;
65  Vector2i m_delta;
66  Vector2i m_step;
67  int m_error;
68  };
69 
70  /**
71  * @ingroup core
72  * @brief Generate a line between two positions
73  *
74  * This function uses [Bresenham's line algorithm](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm).
75  *
76  * @param p0 The first point of the line
77  * @param p1 The last point of the line
78  * @return A series of point beginning at the first point and ending just before the last point
79  *
80  * @sa gf::Bresenham
81  */
83 
84  /**
85  * @ingroup core
86  * @brief 1D midpoint displacement
87  *
88  * @param p0 The first end point
89  * @param p1 The second end point
90  * @param random A random engine
91  * @param iterations The number of iterations
92  * @param direction The direction to make a displacement
93  * @param initialFactor The initial factor to apply to the displacement
94  * @param reductionFactor The factor to apply at each iteration
95  */
97 
98  /**
99  * @ingroup core
100  * @brief 1D midpoint displacement
101  *
102  * The direction is perpendicular to the segment @f$ [P_0 P_1] @f$
103  *
104  * @param p0 The first end point
105  * @param p1 The second end point
106  * @param random A random engine
107  * @param iterations The number of iterations
108  * @param initialFactor The initial factor to apply to the displacement
109  * @param reductionFactor The factor to apply at each iteration
110  */
112 
113 
114  /**
115  * @ingroup core
116  * @brief 2D midpoint displacement
117  *
118  * The size can be anything. If the size is not a power of two plus one,
119  * then a greater heightmap is generated and a submap of the right size
120  * is returned (in the middle of the generated map).
121  *
122  * The function takes initial values. If there are less than three values,
123  * only the first one is used to initialize the four corners. If there are
124  * more than four, only the first four are used to initialize the four
125  * corners in that order: north-west, north-east, south-east, south-west.
126  * If no values are given, the four corners are initialized to @f$ 0.0 @f$.
127  *
128  * @param size The size of the map
129  * @param random A random engine
130  * @param initialValues The initial values of the four corners
131  *
132  * @sa gf::diamondSquare2D()
133  */
134  GF_API Heightmap midpointDisplacement2D(Vector2i size, Random& random, ArrayRef<double> initialValues = nullptr);
135 
136 
137  /**
138  * @ingroup core
139  * @brief 2D diamond square
140  *
141  * The size can be anything. If the size is not a power of two plus one,
142  * then a greater heightmap is generated and a submap of the right size
143  * is returned (in the middle of the generated map).
144  *
145  * The function takes initial values. If there are less than three values,
146  * only the first one is used to initialize the four corners. If there are
147  * more than four, only the first four are used to initialize the four
148  * corners in that order: north-west, north-east, south-east, south-west.
149  * If no values are given, the four corners are initialized to @f$ 0.0 @f$.
150  *
151  * @param size The size of the map
152  * @param random A random engine
153  * @param initialValues The initial values of the four corners
154  *
155  * @sa gf::midpointDisplacement2D()
156  */
157  GF_API Heightmap diamondSquare2D(Vector2i size, Random& random, ArrayRef<double> initialValues = nullptr);
158 
159 
160 #ifndef DOXYGEN_SHOULD_SKIP_THIS
161 }
162 #endif
163 }
164 
165 #endif // GF_GEOMETRY_H
A random engine.
Definition: Random.h:43
std::vector< Vector2f > midpointDisplacement1D(Vector2f p0, Vector2f p1, Random &random, unsigned iterations, Vector2f direction, float initialFactor=1.0f, float reductionFactor=0.5f)
1D midpoint displacement
Heightmap diamondSquare2D(Vector2i size, Random &random, ArrayRef< double > initialValues=nullptr)
2D diamond square
A heightmap.
Definition: Heightmap.h:44
std::vector< Vector2f > midpointDisplacement1D(Vector2f p0, Vector2f p1, Random &random, unsigned iterations, float initialFactor=1.0f, float reductionFactor=0.5f)
1D midpoint displacement
std::vector< Vector2i > generateLine(Vector2i p0, Vector2i p1)
Generate a line between two positions.
The namespace for gf classes.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:42
State for the Bresenham&#39;s line algorithm.
Definition: Geometry.h:44
Bresenham(Vector2i p0, Vector2i p1)
Constructor.
bool step(Vector2i &res)
Generate a new point on the line.
#define GF_API
Definition: Portability.h:35
Heightmap midpointDisplacement2D(Vector2i size, Random &random, ArrayRef< double > initialValues=nullptr)
2D midpoint displacement