00001
00002
00003 #ifndef MATHUTILS_H
00004 #define MATHUTILS_H
00005
00006
00007 #include "Vector3f.h"
00008
00009 #include <math.h>
00010
00011
00012 namespace pge {
00013 namespace mathutils {
00014
00015
00016
00017
00018
00019 static float absf(float f) {
00020 if(f < 0.0f) {
00021 return -f;
00022 } else {
00023 return f;
00024 }
00025 }
00026
00027
00028
00029
00030
00031 static int absi(int i) {
00032 if(i < 0) {
00033 return -i;
00034 } else {
00035 return i;
00036 }
00037 }
00038
00039
00040
00041
00042
00043 static float mapValueToInterval(float minInterval, float maxInterval, float destMin, float destMax,
00044 float value) {
00045
00046
00047 if (value < minInterval) {
00048 value = minInterval;
00049 }
00050 if (value > maxInterval) {
00051 value = maxInterval;
00052 }
00053
00054
00055 float percent = ((value - minInterval) / (maxInterval - minInterval)) * 100.0f;
00056
00057
00058 float result = destMin + ((percent / 100.0f) * (destMax - destMin));
00059
00060 return result;
00061 }
00062
00063
00064
00065
00066
00067 static float random() {
00068 int r = rand();
00069 float result;
00070
00071 result = mapValueToInterval(0.0f, (float)RAND_MAX, 0.0f, 1.0f, (float)r);
00072 return result;
00073 }
00074
00075
00076
00077
00078
00079 static float random(float min, float max) {
00080 int r = rand();
00081 float result;
00082
00083 result = mapValueToInterval(0.0f, (float)RAND_MAX, min, max, (float)r);
00084 return result;
00085 }
00086
00087
00088
00089
00090
00091 static float clampToZeroOne(float value) {
00092 if (value < 0.0f) {
00093 return 0.0f;
00094 }
00095 if (value > 1.0f) {
00096 return 1.0f;
00097 }
00098 return value;
00099 }
00100
00101
00102
00103
00104
00105 static bool isValueInTolerance(float value0, float value1, float tolerance) {
00106 if (absf(value0 - value1) <= tolerance) {
00107 return true;
00108 }
00109 return false;
00110 }
00111
00112
00113
00114
00115
00116 static double degreeToRadian(double degree) {
00117 return degree * PIOVER180;
00118 }
00119
00120
00121
00122
00123
00124 static double radianToDegree(double angle) {
00125 return angle * (180.0 / PI);
00126 }
00127 };
00128 };
00129
00130 #endif