How to select random points within grid meeting min/max distance requirements from each other?

I have a 2D grid map that is relatively large. I'd like to pick some semi-random points all over it (say N points). The rule I want to ensure here, though, is that any selected points are at least M distance from every other selected point but no more than L distance from at least one other point.

Is there a fairly elegant way of doing this without having every new point's distance calculated for every existing point (being less computationally expensive as well)?

I imagine the rules could be met by something like partitioning the map out into rectangular segments and just kind of flood-fill randomly from one inner segment outwards, but I'm having a little trouble visualizing that where my L, M, and N values are randomly chosen (L is of course greater than or equal to M, but not entirely sure what the rules will be for M or N or even the size of the map).

The reason for this whole thing is a procedural waypoint system that I can then build a 2D dungeon crawler type map by using a path finding system to create tiles from waypoint to waypoint essentially.

If this is too vague or any other issues, just let me know and I'll happily try to explain further.

Edit: To give some hard numbers as an example, say the full grid is 2,000 by 1,000 total possible points. Assume you want 5,000 total waypoints spread fairly randomly across the map. Each of the 4,000 points need to be (euclidean distance) greater than 20 from every other point and less than 30 away from at least one other point.

• Ok so you goal is to generate all the points so they are at a specific distance from one another? Mar 13 '15 at 21:49
• I'm guessing poisson discs will be useful for this. You can use a quad tree or sorted range data structure to help find the closest point to your new proposed point. Jason Davies has a demo of an efficient algorithm but it only handles M and not your L condition. Still might be a good starting point. Mar 14 '15 at 15:42
• I'm not sure understand correctly: Do A) have a grid and want to select points in it that match the criteria. Or B) create a grid with just points that match the criteria? Jul 20 '15 at 7:59

Every point must be greater then 20 and less then 30 away from each other.

Red    is the Point.
Purple is the radius 20 from point. If any point is placed here it means it is inside the radius of another point.
Green  is radius 30 from point.
White  is out side. No points should be placed here because it means that point will be 30 units away from any other point. Next simply add a point randomly in a green space and fill in its neighbors. Each point is now greater then 20 and less then 30 from every other point.

Im guessing you will store your grid of points as a object that contains the state of the square.

There is also this method maybe this is what you were looking for in the first place :D

• You've illustrated exactly what I'm going for, but I still don't know quite how to achieve it. I could do a filled circle algorithm working outwards from a radius of 0 to 20 and then 20 to 30, ignore points already seen, and in the 20 to 30 range add them to a "green" list to choose my next point from, but that wouldn't scale very well if I wanted a map with say inner radius 30 and outer radius 400 as an example. Sorry if I didn't explain well, but I'm hoping to have this scale very well so I could set semi-random numbers for my map size, point count, minimum radius, and maximum radius. Mar 14 '15 at 0:31
• I cant seem to find better then O(N^2). How about a pseudo random algorithm? Will that be good enough? There will be some predictability. Mar 14 '15 at 10:02

Your point selection rules can be satisfied by a Poisson-Disk sampling distribution & can be solved in O(n) with Bridson's algorithm. Basically, the algorithm divides the output region into a grid of cells sized relative to the minimum allowable distance, such that only one point can appear in each cell. Then, when you consider adding a new point, you only need to check a disk shaped collection of neighboring cells as opposed to the entire list of points. With respect to your problem, the cell & neighborhood size will account for your M parameter, the disk radii will account for your M parameter. Your N parameter is handled by terminating when enough points have been generated.

Mike Bostock has a nice animation showing the algorithm in progress.

The standard implementation is only concerned with a fixed minimal distance between points. amitp mentioned Herman Tulleken's Poisson Disk sampling article; it includes an adaptation to for varying the minimum distance across the resulting image/map which may make it better suited for your problem. It also includes source code.

Here's an idea of how it might go. Keep in mind the more points you create the less random their placing will be.

For(int i=0; i<3999; i++){
{"ft_lang_label":"__label__en","ft_lang_prob":0.9517271,"math_prob":0.9406819,"size":1501,"snap":"2022-05-2022-21","text_gpt3_token_len":329,"char_repetition_ratio":0.11155645,"word_repetition_ratio":0.014705882,"special_character_ratio":0.22251832,"punctuation_ratio":0.07540984,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9567602,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-28T08:41:31Z\",\"WARC-Record-ID\":\"<urn:uuid:69736829-f72b-4d22-9faa-91bcaa8d55fd>\",\"Content-Length\":\"150531\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f33ff03f-ca92-4c6a-a3de-ad88bfe52717>\",\"WARC-Concurrent-To\":\"<urn:uuid:8e330c94-999d-49a8-a38c-43cffe7f1f1e>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://gamedev.stackexchange.com/questions/96586/how-to-select-random-points-within-grid-meeting-min-max-distance-requirements-fr\",\"WARC-Payload-Digest\":\"sha1:35RNHZODLB4MWEEGZR6AHVZJV7ETBZMR\",\"WARC-Block-Digest\":\"sha1:6VZKNQANY2HEVATKS6MWYAKTBS3POKAO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320305423.58_warc_CC-MAIN-20220128074016-20220128104016-00607.warc.gz\"}"}