Created
February 12, 2013 22:31
-
-
Save rinfz/4774086 to your computer and use it in GitHub Desktop.
Attempted implementation of generating a unit vector from a given offset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double *randomVector(double v[2], int x, int y){ | |
/* generates two coordinates on a unit circle which are used | |
* for the 4 vectors from each grid point. | |
*/ | |
srand(time(NULL)); | |
double r; | |
int cont = 1; | |
/* http://burtleburtle.net/bob/rand/unitvec.html */ | |
do { | |
double m = (double)rand()/(double)(RAND_MAX-1); | |
double n = (double)rand()/(double)(RAND_MAX-1); | |
v[0] = 2*m-1; | |
v[1] = 2*n-1; | |
r = (v[0]*v[0])+(v[1]*v[1]); | |
} while (r > 1.0 || r == 0); | |
double l = sqrt(-2*log(r)/r); | |
// v[0]: x, v[1]: y | |
v[0] = v[0]*l+x; | |
v[1] = v[1]*l+y; | |
printf("v0: %f, v1: %f\n", v[0], v[1]); | |
return v; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment