Angel 3.2
A 2D Game Prototyping Engine
MathUtil.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../AI/BoundingShapes.h"
33 #include "../Infrastructure/Vector2.h"
34 #include "../Infrastructure/VecStructs.h"
35 
37 
45 class MathUtil
46 {
47 public:
51  static const float E;
52 
56  static const float Log10E;
57 
61  static const float Log2E;
62 
66  static const float Pi;
67 
71  static const float PiOver2;
72 
76  static const float PiOver4;
77 
81  static const float TwoPi;
82 
87  static const float MaxFloat;
88 
93  static const float MinFloat;
94 
98  static const float Epsilon;
99 
107  template<typename T>
108  static T Abs(T val )
109  {
110  return val >= 0 ? val : -val;
111  }
112 
121  template<typename T>
122  static T Max(T value1, T value2)
123  {
124  return value1 > value2 ? value1 : value2;
125  }
126 
135  template<typename T>
136  static T Min(T value1, T value2)
137  {
138  return value1 < value2 ? value1 : value2;
139  }
140 
149  template<typename T>
150  static T Distance(T value1, T value2)
151  {
152  return Abs(value1 - value2);
153  }
154 
167  template<typename T>
168  static T Lerp(T value1, T value2, float amount)
169  {
170  return T(value1 + ((T)(value2 - value1) * amount ));
171  }
172 
186  template<typename T>
187  static T SmoothStep(T value1, T value2, float amount)
188  {
189  float num = Clamp(amount, 0.0f, 1.0f);
190  return Lerp(value1, value2, (num * num) * (3.0f - (2.0f * num)));
191  }
192 
201  static int Clamp(int value, int min, int max)
202  {
203  return Max(min, Min(max, value));
204  }
205 
214  static float Clamp(float value, float min, float max)
215  {
216  return Max(min, Min(max, value));
217  }
218 
227  static double Clamp(double value, double min, double max)
228  {
229  return Max(min, Min(max, value));
230  }
231 
238  static float ToDegrees(float radians);
239 
246  static float ToRadians(float degrees);
247 
255  static Vector2 VectorFromAngle(float angle_in_degrees);
256 
263  static float AngleFromVector(const Vector2& v1);
264 
272  static float AngleFromVectors(const Vector2& v1, const Vector2& v2);
273 
281  static int RoundToInt(double x);
282 
289  static int RandomInt(int maximum);
290 
298  static int RandomIntInRange(int min, int max);
299 
308  static int RandomIntWithError(int target, int error);
309 
316  static float RandomFloat(float maximum=1.0f);
317 
325  static float RandomFloatInRange(float min, float max);
326 
335  static float RandomFloatWithError(float target, float error);
336 
342  static bool RandomBool();
343 
349  static Vector2 RandomVector();
350 
358  static Vector2 RandomVector(const Vector2& maxValues);
359 
368  static Vector2 RandomVector(const Vector2& minValues, const Vector2& maxValues);
369 
384  static Vector2List RandomPointField(int numPoints, const Vector2& minValue, const Vector2& maxValue, float minDistance=0.5f);
385 
402  static bool FuzzyEquals(float value1, float value2, float epsilon=Epsilon);
403 
413  static bool FuzzyEquals(const Vector2& v1, const Vector2& v2, float epsilon=Epsilon);
414 
424  static Vector2 ScreenToWorld(int x, int y);
425 
434  static Vector2 ScreenToWorld(const Vec2i& screenCoordinates);
435 
445  static Vector2 WorldToScreen(float x, float y);
446 
455  static Vector2 WorldToScreen(const Vector2& worldCoordinates);
456 
462  static Vector2 GetWorldDimensions();
463 
470  static float PixelsToWorldUnits(float pixels);
471 
479  static float WorldUnitsToPixels(float worldUnits);
480 
481  enum AABBSplittingAxis
482  {
483  AA_X,
484  AA_Y
485  };
486 
493  static AABBSplittingAxis GetMajorAxis(const BoundingBox& source);
494 
505  static void SplitBoundingBox(const BoundingBox& source, AABBSplittingAxis axis, BoundingBox& LHS, BoundingBox& RHS);
506 
515  static float DeltaAngle(float A1, float A2);
516 
524  static float VectorDeltaAngle(const Vector2& v1, const Vector2& v2);
525 };