Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly generating coordinates inside a bounded region [closed]

I have a list of longitudes and latitudes which forms boundary for a geographical area. I would like to generate some random co-ordinates inside this geographical area . Could you suggest some approaches I can take in any language?

like image 857
van Avatar asked Jan 18 '14 14:01

van


1 Answers

Like any problem, there are many ways to solve it, the first thing came into my mind is

  1. Let's call this "geographic area" a polygon.
  2. Find the bounding box of the polygon (easy, just find maxX maxY minX minY).
  3. Generate random coordinate inside the bounding box x=rand()%(maxX-minX)+minX (and same for Y)
  4. Test that the coordinate is inside the polygon, there are many solutions to this problem and they are implemented in any given language so you don't have to implement it by yourself. Here is an implementation in C/C++ (it is easy to change it to any other language) : Point in Polygon Algorithm

http://en.wikipedia.org/wiki/Point_in_polygon

Edit : As Jan Dvorak suggested, it might be problematic to use it technique on huge areas, i believe that if your polygon is close to the equator and his size is less the 100km, it will work just fine.

Also you will run into problems if you are near the 180° line because right next to it is the -180°.

like image 158
OopsUser Avatar answered Sep 28 '22 19:09

OopsUser