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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:30:56+00:00 2026-06-05T12:30:56+00:00

I have the following problem. I have a large region populated with a random

  • 0

I have the following problem.

I have a large region populated with a random number of circles of different sizes. If a new circle of random radius is inserted in a random location, I’d like to find a nearby position for it so it doesn’t overlap with any of the others. It’s optimal if the circles stay close.

The number of circles and their size are limited, but random. The region will be quite large, (2500×2500, maybe) so an array of pixels as proposed here is out of the question. A person who answered that same question proposed a grid, in which the cells are the size of the circles. That would solve my problem, using cells the size of the largest possible circle, but I would like the circles to remain as close as possible, so it wouldn’t entirely satisfy my needs.

A very basic approach consists of detecting collisions on placement of the new circle, and moving it away from the circle it collides with. After that, check for collisions again and repeat the process. This is obviously not very elegant and it’s prone to infinite loops (more often than you might think).

The goal is to find the closest possible position for the newly inserted circle so it does not overlap with anyone else.

P.D.
A very nice thing, but a different matter, and not my main objective, would be to rearrange as many circles as it’s necessary, instead of relocating just the one, as though they were ‘pushing’ each other. I’d favor distance over number of circles moved. That is, I’d prefer many circles to move a little than one circle to move very far away from its original position.

  • 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-06-05T12:30:57+00:00Added an answer on June 5, 2026 at 12:30 pm

    I would do the following: define the grid of x,y
    and for the grid, compute the minimum distance to all the circles minus radius to the circle.
    On the resulting map, you can select any pixel which is brighter then X which means that you could place a circle with radius X there without overlap.
    Here is the code and an image showing the resulting map.
    If you want to make it faster, you can use a lower resolution version of the map.

    import numpy as np,numpy.random
    import matplotlib.pyplot as plt,matplotlib,scipy.optimize
    
    maxx=2500
    maxy=2500
    maxrad=60 #max radius of the circle
    ncirc=100 # number of circles
    np.random.seed(1)
    
    #define centers of circles
    xc,yc=np.random.uniform(0,maxx,ncirc),np.random.uniform(0,maxy,ncirc)
    rads=np.random.uniform(0,maxrad,ncirc)
    #define circle radii
    
    xgrid,ygrid=np.mgrid[0:maxx,0:maxy]
    im=xgrid*0+np.inf
    
    for i in range(ncirc):
        im = np.minimum(im, ((xgrid - xc[i])**2 + (ygrid - yc[i])**2)**.5 - rads[i])
    # im now stores the minimum radii of the circles which can 
    # be placed at a given position without overlap
    
    #plotting 
    fig=plt.figure(1)
    plt.clf()
    plt.imshow(im.T,extent=(0, maxx, 0, maxy))
    plt.colorbar()
    ax = plt.gca()
    for i in range(ncirc):
         ax.add_patch(matplotlib.patches.Circle((xc[i], yc[i]), rads[i],
              facecolor='none', edgecolor='red'))
    plt.xlim(0, maxx)
    plt.ylim(0, maxy)
    plt.draw()
    

    The map of the minimum radii of a circles which can be placed without overlap

    The way the map looks like the voronoi diagram but it is unclear whether that can be exploited.

    Update: After some thought there is a potentially faster solution which would work for large number of circles.
    First create grid of the area (say 2500×2500). Fill all the pixels which are inside the circles by 1, Everything else with zero. Then you need to convolve this map with the circular kernel with the required radius of the circle which you want to place. The resulting map must have 0 at the pixels which can be used for placing of the pixels. The advantage of this method is that it can work for very large grids and number of circles, and the convolution can be easily done by fft.

    Here is the illustration showing the first mask, and the mask after the convolution with the circular kernel with radius of 128 pixels. All zero pixels in the right mask are possible location of the new circle with the radius of 128.

    mask

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

Sidebar

Related Questions

I have the following problem: We have a large product which is in master
I have a following problem. A large rectangle contains smaller non-intersecting rectangles (The black
My application problem is the following - I have a large structure foo. Because
I face the following problem. I have two large tables with about 80.000 records
Hey guys, i have the following problem: 1 process executes a very large query
I have the following problem. I am trying to integrate a large code written
I have the following problem: I save(xml serialize) my configuration(very large list of object)
I have to following problem. I've large collection of XML files. In each XML
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I

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.