Skip to content

Instantly share code, notes, and snippets.

@BrentonPoke
Created August 16, 2022 20:52
Show Gist options
  • Save BrentonPoke/a19249253f52f3526481c730e7f5c6ea to your computer and use it in GitHub Desktop.
Save BrentonPoke/a19249253f52f3526481c730e7f5c6ea to your computer and use it in GitHub Desktop.
Generating GPS coords within radius
#include <iostream>
#include <string>
#include <random>
#include <cmath>
void getLocation(double x0, double y0, int radius) {
std::random_device rd;
std::uniform_int_distribution<int> dist(0,49);
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000.0;
double u = dist(rd);
double v = dist(rd);
double w = radiusInDegrees * sqrt(u);
double t = 2 * M_PI * v;
double x = w * cos(t);
double y = w * sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / cos(M_PI * y0);
std::cout.precision(17);
double foundLongitude = new_x + x0;
double foundLatitude = y + y0;
//std::cout << "Longitude: " ;
std::cout << std::fixed << foundLongitude;
std::cout << ", ";
std::cout << std::fixed << foundLatitude << std::endl;
}
int main()
{
for(int i=0; i < 20; i++)
getLocation(-83.68930999146386, 43.02028045590756, 15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment