Introduce random<T>() and random<T>(min,max). Make doings of

calculate_variation more clear (hopefully).

--HG--
branch : minor
This commit is contained in:
vrld
2012-02-04 21:36:19 +01:00
parent a18632ce2d
commit 80824b5471
2 changed files with 24 additions and 8 deletions
+14
View File
@@ -21,6 +21,8 @@
#ifndef LOVE_MATH_H
#define LOVE_MATH_H
#include <cstdlib> // for rand()
/* Definitions of useful mathematical constants
* M_E - e
* M_LOG2E - log2(e)
@@ -65,6 +67,18 @@ struct vertex
float s, t;
};
template<typename T>
inline T random()
{
return T(rand()) / (T(RAND_MAX)+T(1));
}
template<typename T>
inline T random(T min, T max)
{
return random<T>() * (max - min) + min;
}
} // love
#endif // LOVE_MATH_H
+10 -8
View File
@@ -40,12 +40,14 @@ namespace opengl
}
}
float calculate_variation(float inner, float outer, float var)
// returns a value in the range [-x/2:x/2] around mean, where
// x = margin * scale; so margin and scale determine the width
// of the interval to chose the value from, while mean names
// the expected value.
inline float calculate_variation(float mean, float margin, float scale)
{
float low = inner - (outer/2.0f)*var;
float high = inner + (outer/2.0f)*var;
float r = (rand() / (float(RAND_MAX)+1));
return low*(1-r)+high*r;
// same as random(mean - scale*margin*.5, mean + scale*margin*.5)
return mean + scale * margin * (random<float>() - .5f);
}
@@ -93,7 +95,7 @@ namespace opengl
min = direction - spread/2.0f;
max = direction + spread/2.0f;
pLast->direction = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
pLast->direction = random<float>(min, max);
min = speedMin;
max = speedMax;
@@ -113,8 +115,8 @@ namespace opengl
max = tangentialAccelerationMax;
pLast->tangentialAcceleration = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
pLast->sizeOffset = (rand() / (float(RAND_MAX)+1)) * sizeVariation; // time offset for size change
pLast->sizeIntervalSize = (1.0 - (rand() / (float(RAND_MAX)+1)) * sizeVariation) - pLast->sizeOffset;
pLast->sizeOffset = random<float>() * sizeVariation; // time offset for size change
pLast->sizeIntervalSize = (1.0f - random<float>() * sizeVariation) - pLast->sizeOffset;
pLast->size = sizes[(size_t)(pLast->sizeOffset - .5f) * (sizes.size() - 1)];
min = rotationMin;