I'd like to have a function that accepts a geo location (Latitude, Longitude) and generates random sets of coordinates around it but also takes these parameters as a part of the calculation:
Example of how the generation would be:
What's a good approach to achieve this?
Yes you can. Just get to the place you want to make a pin, and then clear your search, and then click the map (try clicking twice if nothing happens). It will put a gray dot. Then you can click the longitude and latitude coordinates in the little pop up box at the bottom.
In python, there's an inbuilt method, “random. uniform()” which performs this task with ease and uses just one word. This method is defined in the “random” module. It Returns the generated floating-point random number between the lower limit and upper limit.
Enter Coordinates on Android Open Google Maps, enter the coordinates into the Search box at the top, and hit the Search key on the keyboard. You'll see a pin appear on the map for the spot. Swipe up from the bottom to get directions or view details for the location.
Generate random coordinates around a location
function generateRandomPoint($centre, $radius) { $radius_earth = 3959; //miles //Pick random distance within $distance; $distance = lcg_value()*$radius; //Convert degrees to radians. $centre_rads = array_map( 'deg2rad', $centre ); //First suppose our point is the north pole. //Find a random point $distance miles away $lat_rads = (pi()/2) - $distance/$radius_earth; $lng_rads = lcg_value()*2*pi(); //($lat_rads,$lng_rads) is a point on the circle which is //$distance miles from the north pole. Convert to Cartesian $x1 = cos( $lat_rads ) * sin( $lng_rads ); $y1 = cos( $lat_rads ) * cos( $lng_rads ); $z1 = sin( $lat_rads ); //Rotate that sphere so that the north pole is now at $centre. //Rotate in x axis by $rot = (pi()/2) - $centre_rads[0]; $rot = (pi()/2) - $centre_rads[0]; $x2 = $x1; $y2 = $y1 * cos( $rot ) + $z1 * sin( $rot ); $z2 = -$y1 * sin( $rot ) + $z1 * cos( $rot ); //Rotate in z axis by $rot = $centre_rads[1] $rot = $centre_rads[1]; $x3 = $x2 * cos( $rot ) + $y2 * sin( $rot ); $y3 = -$x2 * sin( $rot ) + $y2 * cos( $rot ); $z3 = $z2; //Finally convert this point to polar co-ords $lng_rads = atan2( $x3, $y3 ); $lat_rads = asin( $z3 ); return array_map( 'rad2deg', array( $lat_rads, $lng_rads ) ); }
Usage
generateRandomPoint(array(3.1528, 101.7038), 4);
A brute force method should be good enough.
for each point to generate "n" find a random angle get the x and y from the angle * a random radius up to max radius for each point already generated "p" calculate the distance between "n" and "p" if "n" satisfies the min distance add new point "n"
In PHP, generating a new point is easy
$angle = deg2rad(mt_rand(0, 359)); $pointRadius = mt_rand(0, $radius); $point = array( 'x' => sin($angle) * $pointRadius, 'y' => cos($angle) * $pointRadius );
Then calculating the distance between two points
$distance = sqrt(pow($n['x'] - $p['x'], 2) + pow($n['y'] - $p['y'], 2));
** Edit **
For the sake of clarifying what others have said, and after doing some further research (I'm not a mathematician, but the comments did make me wonder), here the most simple definition of a gaussian distribution :
If you were in 1 dimension, then $pointRadius = $x * mt_rand(0, $radius); would be OK since there is no distinction between $radius and $x when $x has a gaussian distribution.
In 2 or more dimensions, however, if the coordinates ($x,$y,...) have gaussian distributions then the radius $radius does not have a gaussian distribution.
In fact the distribution of $radius^2 in 2 dimensions [or k dimensions] is what is called the "chi-squared distribution with 2 [or k] degrees of freedom", provided the ($x,$y,...) are independent and have zero means and equal variances.
Therefore, to have a normal distribution, you'd have to change the line of the generated radius to
$pointRadius = sqrt(mt_rand(0, $radius*$radius));
as others have suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With