-
Notifications
You must be signed in to change notification settings - Fork 1
/
Generic.cpp
322 lines (257 loc) · 7.78 KB
/
Generic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//////////////////////////////////////////////////////////////////////////////
//
// Hx
// --
//
// Plinth
//
// Copyright(c) 2015 M.J.Silk
//
// This software is provided 'as-is', without any express or implied
// warranty.In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions :
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
// M.J.Silk
// MJSilk2@gmail.com
//
//////////////////////////////////////////////////////////////////////////////
#include "Generic.hpp"
namespace hx
{
namespace Sfml
{
// SFML from Hx
inline sf::Vector2f vector2(const hx::Vector2d& vector)
{
return{ static_cast<float>(vector.x), static_cast<float>(vector.y) };
}
inline sf::Vector2i vector2(const hx::Vector2i& vector)
{
return{ vector.x, vector.y };
}
inline sf::Vector2u vector2(const hx::Vector2u& vector)
{
return{ vector.x, vector.y };
}
inline sf::Vector3f vector3(const hx::Vector3d& vector)
{
return{ static_cast<float>(vector.x), static_cast<float>(vector.y), static_cast<float>(vector.z) };
}
inline sf::Vector3i vector3(const hx::Vector3i& vector)
{
return{ vector.x, vector.y, vector.z };
}
inline sf::Vector3i vector3(const hx::Vector3u& vector)
{
return{ static_cast<int>(vector.x), static_cast<int>(vector.y), static_cast<int>(vector.z) };
}
// Hx from SFML
inline hx::Vector2d vector2(const sf::Vector2f& vector)
{
return{ vector.x, vector.y };
}
inline hx::Vector2i vector2(const sf::Vector2i& vector)
{
return{ vector.x, vector.y };
}
inline hx::Vector2u vector2(const sf::Vector2u& vector)
{
return{ vector.x, vector.y };
}
inline hx::Vector3d vector3(const sf::Vector3f& vector)
{
return{ vector.x, vector.y, vector.z };
}
inline hx::Vector3i vector3(const sf::Vector3i& vector)
{
return{ vector.x, vector.y, vector.z };
}
sf::Color colorFromValue(long int value)
{
return colorFromRgb(hx::Color::rgbFromValue(value));
}
sf::Color colorFromRgb(const hx::Color::Rgb& rgb)
{
return sf::Color(hx::Tween::linear(0, 255, rgb.r), hx::Tween::linear(0, 255, rgb.g), hx::Tween::linear(0, 255, rgb.b));
}
sf::Color colorBlend(const sf::Color& colorA, const sf::Color& colorB, double alpha)
{
return sf::Color(hx::Tween::linear(colorA.r, colorB.r, alpha), hx::Tween::linear(colorA.g, colorB.g, alpha), hx::Tween::linear(colorA.b, colorB.b, alpha), hx::Tween::linear(colorA.a, colorB.a, alpha));
}
sf::Color colorBlend(const sf::Color& colorA, const sf::Color& colorB, float alpha)
{
return colorBlend(colorA, colorB, static_cast<double>(alpha));
}
float dotProduct(const sf::Vector2f& a, const sf::Vector2f& b)
{
return a.x * b.x + a.y * b.y;
}
float lengthSquared(const sf::Vector2f& vector)
{
return vector.x * vector.x + vector.y * vector.y;
}
float length(const sf::Vector2f& vector)
{
return sqrt(lengthSquared(vector));
}
float lengthSquared(const sf::Vector3f& vector)
{
return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
}
float length(const sf::Vector3f& vector)
{
return sqrt(lengthSquared(vector));
}
sf::Vector3f crossProduct(const sf::Vector3f& a, const sf::Vector3f& b)
{
return
{
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
/*
float crossProductPoints(const sf::Vector2f& a, const sf::Vector2f& b)
{
return{ a.x * b.y - b.x * a.y };
}
*/
bool CCW(const std::vector<sf::Vector2f>& points)
{
if (points.size() != 3)
return false;
return (points[2].y - points[0].y) * (points[1].x - points[0].x) > (points[1].y - points[0].y) * (points[2].x - points[0].x);
}
// vector of (2) lines. line is a vector of (2) vector2fs (the points of the line).
// i.e. { { line1.x, line.y }, { line2.x, line2.y } }
bool doLinesIntersect(const std::vector<std::vector<sf::Vector2f>>& lines)
{
if (lines.size() != 2)
return false;
if ((lines[0].size() != 2) || (lines[1].size() != 2))
return false;
return ( CCW({ lines[0][0], lines[1][0], lines[1][1] }) !=
CCW({ lines[0][1], lines[1][0], lines[1][1] })) &&
( CCW({ lines[0][0], lines[0][1], lines[1][0] }) !=
CCW({ lines[0][0], lines[0][1], lines[1][1] }));
}
bool isPointInsidePolygon(const sf::Vector2f& point, const std::vector<sf::Vector2f>& polygonVertices)
{
return isPointInsidePolygon(point, polygonVertices, boundingBox(polygonVertices));
}
bool isPointInsidePolygon(const sf::Vector2f& point, const std::vector<sf::Vector2f>& polygonVertices, const sf::FloatRect& boundingBox)
{
if (!boundingBox.contains(point))
return false;
bool isInside{ false };
sf::Vector2f pointOutsideBoundingBox{ boundingBox.left - 1, boundingBox.top - 1 };
for (unsigned int v{ 0 }, w{ polygonVertices.size() - 1 }; v < polygonVertices.size(); w = v++)
{
if (doLinesIntersect({ { polygonVertices[v], polygonVertices[w] }, { pointOutsideBoundingBox, point } }))
isInside = !isInside;
}
return isInside;
}
bool doClosedPolylinesIntersect(const std::vector<sf::Vector2f>& a, const std::vector<sf::Vector2f>& b)
{
for (unsigned int l{ 0 }, m{ a.size() - 1 }; l < a.size(); m = l++)
{
for (unsigned int v{ 0 }, w{ b.size() - 1 }; v < b.size(); w = v++)
{
if (doLinesIntersect({ { a[l], a[m] }, { b[v], b[w] } }))
return true;
}
}
return false;
}
sf::FloatRect boundingBox(const std::vector<sf::Vector2f>& vertices)
{
sf::FloatRect box;
if (vertices.size() > 0)
{
box.left = vertices[0].x;
box.top = vertices[0].y;
}
// store bottom right corner otherwise moving the top or left of the box would require also adjusting the width and height to compensate
sf::Vector2f bottomRight{ vertices[0] };
for (auto& vertex : vertices)
{
if (vertex.x < box.left)
box.left = vertex.x;
else if (vertex.x > bottomRight.x)
bottomRight.x = vertex.x;
if (vertex.y < box.top)
box.top = vertex.y;
else if (vertex.y > bottomRight.y)
bottomRight.y = vertex.y;
}
box.width = bottomRight.x - box.left;
box.height = bottomRight.y - box.top;
return box;
}
void changeAlpha(sf::Color& color, unsigned int alpha)
{
color.a = hx::clamp(alpha, 0u, 255u);
}
void changeAlpha(sf::Color& color, float alpha)
{
changeAlpha(color, static_cast<double>(alpha));
}
void changeAlpha(sf::Color& color, double alpha)
{
changeAlpha(color, hx::Tween::linear(0u, 255u, alpha));
}
sf::Color colorFromColorAndAlpha(sf::Color color, unsigned int alpha)
{
color.a = hx::clamp(alpha, 0u, 255u);
return color;
}
sf::Color colorFromColorAndAlpha(sf::Color color, float alpha)
{
return colorFromColorAndAlpha(color, hx::Tween::linear(0u, 255u, alpha));
}
sf::Color colorFromColorAndAlpha(sf::Color color, double alpha)
{
return colorFromColorAndAlpha(color, hx::Tween::linear(0u, 255u, alpha));
}
sf::Vector2f roundVector(const sf::Vector2f& vector)
{
return{ round(vector.x), round(vector.y) };
}
sf::Vector3f roundVector(const sf::Vector3f& vector)
{
return{ round(vector.x), round(vector.y), round(vector.z) };
}
sf::Vector2f floorVector(const sf::Vector2f& vector)
{
return{ floor(vector.x), floor(vector.y) };
}
sf::Vector3f floorVector(const sf::Vector3f& vector)
{
return{ floor(vector.x), floor(vector.y), floor(vector.z) };
}
sf::Vector2f ceilVector(const sf::Vector2f& vector)
{
return{ ceil(vector.x), ceil(vector.y) };
}
sf::Vector3f ceilVector(const sf::Vector3f& vector)
{
return{ ceil(vector.x), ceil(vector.y), ceil(vector.z) };
}
} // namespace Sfml
} // namespace hx