Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:31:33+00:00 2026-05-23T06:31:33+00:00

I am trying to come up with a method for generating X amount of

  • 0

I am trying to come up with a method for generating X amount of random points in a given area (in my case a square). The one thing that makes this such an issue is that each point has to be at least Y units away from all other points.

What springs to mind at first is (in c#) to check the distance between the new point and all existing points:

while(points.Count < pointsToGenerate)
{
     Point newPoint = NewPoint();
     bool addPoint = true;
     foreach(Point p in points)
     {
          if((p - newPoint).Length() < minDistance)
          {
              addPoint = false;
              break;
          }
     }

     if(addPoint)
     {
          points.Add(newPoint);
     }
}

Now this would certainly work however if no valid points were ever found this would become a never ending loop. So throw in a magical number Z there as a limit of tries?

if(loopCount > 100)
{
     break;
}

Now this has some obvious problems. If the points are randomly generated, then loopCount can go above Z even if there are places left to place the point. It not only can, but it will happen!

What I could do is to create a list of available point per each pass and then select a random one of those. This would work flawlessly except for one thing: Performance. I’m not in need of super performance in my application, but the area is 1000^2. A lot of points to check per pass even if I limit myself to integers!

So, what I can come up with might not suffice, thus I would love some assistance on this. Is there a better way to generate X points in an area A with the minimum distance between points Y?

Thanks!

EDIT: With better, I mean generally better where a balance of performance vs perfection is achieved. A bit vague, I know. I’m not entierly sure how much overhead I can have for generating those points yet so I’m basically after anything more elegant than my own method.

~Robert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:31:34+00:00Added an answer on May 23, 2026 at 6:31 am

    To understand your problem : are you looking for the optimal answer (i.e : homework), or for a very good algorithm that’s better than creating random points?

    In the first case, I’m afraid it’s a very complicated problem if you’ve got no prior information on the area A. And I believe that it’ll be hard to find an algorithm that’s faster than exploring every single case.

    However, if you’ve got some prior information on A then things might be simpler. For example, if it’s convex, you can exploit the fact that the optimal pavement if your space is infinite is hexagonal. It means that you have to put your points (in X) at the extremities of the triangles

    triangle

    So :

    • compute the convex hull (O(n*log(n)))
    • compute the diameter (the two furthest points of your set)
    • begin by adding to X one of the points (of the diameter)
    • then add points that correspond to the hexagonal pavement, favoring the ones on the convex hull

    This algorithm is not optimal (not unless you define a very good “favoring the ones on the convex hull”…)


    Edit : Mr E’s comment reminded me that the optimal pavement thing derives from circle packing. Kudos to him for the precision!


    However, I do have another algorithm that looks very nice, and is perhaps even optimal! It does not require any conditions on A, and is a bit expensive, but not too much. Yeah, I know, it contradicts what I said, but who cares! Having a good algorithm is nice enough.

    Let’s name B the set of the available points so far. And C the points that form the extremities of B. At the beginning, B=A, and if A is a square, then C consists of 4 points (the corners). You simply have to recursively :

    • compute the two furthest points of B. You only need the points in C for this
    • add to X one of the points (of the diameter) at random
    • remove from B the points that are now unavailable. You only need to update C for this.

    I know that if you work in a 1000×1000 grid, C begins with 4 points, but after adding one point to X, it means that C grows to 1570 points (roughly (pi/2)1000).
    You have to notice that you never put in memory B, which is big (O(n^2) if A can be put in a n
    n grid) Only C, and I believe that at any point, the size of C is O(n) , which remains much better than O(n^2). And computing the diameter remains O(size(C)) = O(n)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to come up with a method, that given a day of
I am trying to come up with a method of extracting 4 characters that
I am trying to come up with a design for a method that takes
I am trying to come up with a method factory that looks inside config
I am trying to come up with a method that takes an integer and
I am trying to come up with a C# method that will be able
I'm trying to come up with a javascript/jquery method that will allow me to
I'm trying to come up with a method which will measure and return the
I'm trying to come up with a Java regex that will match a filename
I am trying to come up with such a solution that the user is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.