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 6111931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:13+00:00 2026-05-23T14:41:13+00:00

What is the most efficient way to randomly fill a space with as many

  • 0

What is the most efficient way to randomly fill a space with as many non-overlapping shapes? In my specific case, I’m filling a circle with circles. I’m randomly placing circles until either a certain percentage of the outer circle is filled OR a certain number of placements have failed (i.e. were placed in a position that overlapped an existing circle). This is pretty slow, and often leaves empty spaces unless I allow a huge number of failures.

So, is there some other type of filling algorithm I can use to quickly fill as much space as possible, but still look random?

  • 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-23T14:41:14+00:00Added an answer on May 23, 2026 at 2:41 pm

    Issue you are running into

    You are running into the Coupon collector’s problem because you are using a technique of Rejection sampling.

    You are also making strong assumptions about what a “random filling” is. Your algorithm will leave large gaps between circles; is this what you mean by “random”? Nevertheless it is a perfectly valid definition, and I approve of it.


    Solution

    To adapt your current “random filling” to avoid the rejection sampling coupon-collector’s issue, merely divide the space you are filling into a grid. For example if your circles are of radius 1, divide the larger circle into a grid of 1/sqrt(2)-width blocks. When it becomes “impossible” to fill a gridbox, ignore that gridbox when you pick new points. Problem solved!


    Possible dangers

    You have to be careful how you code this however! Possible dangers:

    • If you do something like if (random point in invalid grid){ generateAnotherPoint() } then you ignore the benefit / core idea of this optimization.
    • If you do something like pickARandomValidGridbox() then you will slightly reduce the probability of making circles near the edge of the larger circle (though this may be fine if you’re doing this for a graphics art project and not for a scientific or mathematical project); however if you make the grid size 1/sqrt(2) times the radius of the circle, you will not run into this problem because it will be impossible to draw blocks at the edge of the large circle, and thus you can ignore all gridboxes at the edge.

    Implementation

    Thus the generalization of your method to avoid the coupon-collector’s problem is as follows:

    Inputs: large circle coordinates/radius(R), small circle radius(r)
    Output: set of coordinates of all the small circles
    Algorithm:
      divide your LargeCircle into a grid of r/sqrt(2)
    
      ValidBoxes = {set of all gridboxes that lie entirely within LargeCircle}
    
      SmallCircles = {empty set}
    
      until ValidBoxes is empty:
        pick a random gridbox Box from ValidBoxes
        pick a random point inside Box to be center of small circle C
    
        check neighboring gridboxes for other circles which may overlap*
        if there is no overlap:
          add C to SmallCircles
          remove the box from ValidBoxes  # possible because grid is small
        else if there is an overlap:
          increase the Box.failcount
          if Box.failcount > MAX_PERGRIDBOX_FAIL_COUNT:
            remove the box from ValidBoxes
    
      return SmallCircles
    

    (*) This step is also an important optimization, which I can only assume you do not already have. Without it, your doesThisCircleOverlapAnother(…) function is incredibly inefficient at O(N) per query, which will make filling in circles nearly impossible for large ratios R>>r.

    This is the exact generalization of your algorithm to avoid the slowness, while still retaining the elegant randomness of it.


    Generalization to larger irregular features

    edit: Since you’ve commented that this is for a game and you are interested in irregular shapes, you can generalize this as follows. For any small irregular shape, enclose it in a circle that represent how far you want it to be from things. Your grid can be the size of the smallest terrain feature. Larger features can encompass 1×2 or 2×2 or 3×2 or 3×3 etc. contiguous blocks. Note that many games with features that span large distances (mountains) and small distances (torches) often require grids which are recursively split (i.e. some blocks are split into further 2×2 or 2x2x2 subblocks), generating a tree structure. This structure with extensive bookkeeping will allow you to randomly place the contiguous blocks, however it requires a lot of coding. What you can do however is use the circle-grid algorithm to place the larger features first (when there’s lot of space to work with on the map and you can just check adjacent gridboxes for a collection without running into the coupon-collector’s problem), then place the smaller features. If you can place your features in this order, this requires almost no extra coding besides checking neighboring gridboxes for collisions when you place a 1×2/3×3/etc. group.

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

Sidebar

Related Questions

What is the most efficient way to determine how many comments a particular blog
What's the most efficient way to lower case every element of a List or
What would be the most efficient way to select a non previously selected pair
What's the most efficient way to concatenate strings?
What's the most efficient way to resize large images in PHP? I'm currently using
What's the most efficient way to convert the output of this function from a
Whats the most efficient way of selecting total number of records from a large
What would be the most efficient way of recording to a log (.txt) from
What is the most efficient way to get the default constructor (i.e. instance constructor
What is the most efficient way of turning the list of values of a

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.