C#: How to generate random locations nearby map's center in GMap.NET?

1k views Asked by At

Is there any way to generate random locations nearby map's center within a specific radius in my Form? I can obtain the center of the map with the following line:

        var center = gMapControl2.Position;

I did some research and came across to this post but it's in Java.

2

There are 2 answers

0
Ehsan.Saradar On

Assume that center is (x0,y0) and we are looking for a random location (x,y) with maximum distance maxDist from the center. We know that

(x-x0)*(x-x0) + (y-y0)+(y-y0) <= maxDist *maxDist 

So first we find a random value for x in the appropriate distance then find a random value for y:

    int x = random.Next(-1* maxDist, maxDist);
    int maxY =(int) Math.Floor(Math.Sqrt(maxDist * maxDist - x * x));
    int y = random.Next(-1*maxY, maxY);
    y += y0;
    x += x0;
0
SezMe On

Generate a random number for R between RMax and RMin. Generate a different number for theta between 0 and 360. Now use basic trigonometry to convert to (x,y).

This approach seems most intuitive to me because the problem as stated is fundamentally radially symmetric. It also gives you (R, Theta) for any other computations you may want to do.