Answers for "Generate Random Numbers a Distance Apart From Each Other"
http://answers.unity.com/questions/1208509/generate-random-numbers-a-distance-apart-from-each.html
The latest answers for the question "Generate Random Numbers a Distance Apart From Each Other"Answer by Bunny83
http://answers.unity.com/answers/1208744/view.html
I don't quite get the point of that strange number generation. Your actual range that you generate is
-869 to 0 and 150 to 720. So you have a gap between 0 and 150 and your galaxy isn't centered around 0,0
If you wanted to concentrate the values around the center you would have to do something like:
int num = Random.Range(-870, 870);
if (num > 0)
num -= 150;
else
num += 150;
This will give you a range of -720 to 720 with double the desity between -150 and 150. Keep in mind that you will generate a rectange here and not a circle. So the high density area in the center will also be a rectangle. You might want to generate the position based on a random angle around the center and a random distance.
float angle = Random.Range(0,360);
float dist = Random.Range(0f,870f);
if (dist > 150)
dist -= 150;
Vector3 p = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward * dist;
This will generate points in a circular fashion and the "bulge" around the center is also circular.
About your actual problem there are several ways to solve this problem.
- You could simply generate more points than you need and when done you simply iterate over all points and check the distance between the current and every other point. If a distance is too close you would simply remove one of the points. This has a constant complexity of O(n²).
- The other way would be todo the same as in the first approach, but if two points are too close you simply move them apart, each half the overlapping distance. The problem here is that after one iteration through all points the result is not guaranteed to be correct as the spreading of two points can cause new problems with other points. In the high density area this could cause a lot of problems and you would need many iterations until all points have a valid position. If there are way too many points in a certain area it could take thousands of iterations where each iteration has a complexity of O(n²).
The first approach is the easiest one to implement however the resulting amount of points can not be determined before the process. If you need a certain amount you could generate 2 or 3 times the points you need, then delete all point that overlap. If the resulting amout is larger than the wanted amount you can simply randomly delete points from the array until the amount is correct. However if there are less points than the wanted amount you would need to generate more points and restart the process.
The second one is the most natural approach and more like a physics engine solving problem. As long as there are overlaps you apply correction forces / movement until there are no overlaps. This approach has a set amount of points which does not change but it's runtime can explode if the values are choosed too large. Also this approach can cause to "push" your points outside your initial radius.Mon, 27 Jun 2016 11:16:49 GMTBunny83