Created
May 9, 2013 04:16
-
-
Save justin/5545529 to your computer and use it in GitHub Desktop.
Generate a shit load of random geocoordinates around a given distance.
This file contains hidden or 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
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI)) | |
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) | |
#define ARC4RANDOM_MAX 0x100000000 | |
double startingLatitude = DEGREES_TO_RADIANS(39.753638); | |
double startingLongitude = DEGREES_TO_RADIANS(-105.007375); | |
double earthRadius = 3960.056052; | |
double maxdist = 50.0f; | |
maxdist = maxdist / earthRadius; | |
for (int i = 0 ; i<100 ; i++) | |
{ | |
double random1 = ((double)arc4random() / ARC4RANDOM_MAX); | |
double random2 = ((double)arc4random() / ARC4RANDOM_MAX); | |
double randomDistance = acos(random1 *(cos(maxdist) - 1) + 1); | |
double brg = 2 * M_PI * random2; | |
double lat = asin(sin(startingLatitude) * cos(randomDistance) + cos(startingLatitude) * sin(randomDistance) * cos(brg)); | |
double lon = startingLongitude + atan2(sin(brg) * sin(randomDistance) * cos(startingLatitude), cos(randomDistance) - sin(startingLatitude) * sin(lat)); | |
if (lon < (M_PI * - 1)) | |
{ | |
lon = lon + 2 * M_PI; | |
} | |
if (lon > M_PI) | |
{ | |
lon = lon - 2 * M_PI; | |
} | |
CLLocationCoordinate2D coord; | |
coord.latitude = RADIANS_TO_DEGREES(lat); | |
coord.longitude = RADIANS_TO_DEGREES(lon); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have used this multiple times! Thanks for sharing!