~ubuntu-branches/ubuntu/maverick/fyre/maverick

« back to all changes in this revision

Viewing changes to src/math-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Haas
  • Date: 2006-10-22 20:33:21 UTC
  • mfrom: (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20061022203321-xlc2dvrxbptdojtf
Tags: 1.0.1-1
* New upstream release
* Patch from Sebastian Dröge included to avoid libcairo dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * math-util.c - Small math utilities shared by other modules
4
4
 *
5
5
 * Fyre - rendering and interactive exploration of chaotic functions
6
 
 * Copyright (C) 2004-2005 David Trowbridge and Micah Dowty
 
6
 * Copyright (C) 2004-2006 David Trowbridge and Micah Dowty
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License
25
25
#include <glib.h>
26
26
#include <math.h>
27
27
 
28
 
 
29
 
float uniform_variate() {
 
28
/* It's much faster to use our own global g_rand, rather than
 
29
 * relying on the g_random_* family of functions. Those functions
 
30
 * are thread-safe, and the locking around that shared g_rand can
 
31
 * take a very significant amount of CPU. We don't need thread-safe
 
32
 * random variates yet.
 
33
 */
 
34
static GRand* global_random = NULL;
 
35
 
 
36
void math_init() {
 
37
    global_random = g_rand_new_with_seed(time(NULL));
 
38
}
 
39
 
 
40
double uniform_variate() {
30
41
    /* A uniform random variate between 0 and 1 */
31
 
    return g_random_double();
32
 
}
33
 
 
34
 
float normal_variate() {
35
 
    /* A unit-normal random variate, implemented with the Box-Muller method */
36
 
    return sqrt(-2*log(g_random_double())) * cos(g_random_double() * (2*M_PI));
 
42
    return g_rand_double(global_random);
 
43
}
 
44
 
 
45
void normal_variate_pair(double *a, double *b) {
 
46
    /* Produce a pair of values with a standard normal distribution,
 
47
     * using the Polar Box-Mueller method.
 
48
     */
 
49
    double x, y, r2, m;
 
50
 
 
51
    do {
 
52
        x = uniform_variate();
 
53
        x += x - 1;
 
54
        y = uniform_variate();
 
55
        y += y - 1;
 
56
 
 
57
        /* Squared radius. The vector must be nonzero,
 
58
         * and it must lie within the unit circle.
 
59
         */
 
60
        r2 = x*x + y*y;
 
61
    } while (r2 > 1 || r2 == 0);
 
62
    
 
63
    /* Perform the Box-Mueller transform on each component */
 
64
    m = sqrt(-2.0 * log(r2) / r2);
 
65
    *a = x * m;
 
66
    *b = y * m;
 
67
}
 
68
 
 
69
int int_variate(int minimum, int maximum) {
 
70
    return g_rand_int_range(global_random, minimum, maximum);
37
71
}
38
72
 
39
73
int find_upper_pow2(int x) {