Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding nearest free position for a circle for any point [x,y] in a 2D space with circles

I am making a game in which the user player places circles on the screen. It is important that the circles never overlap, so I need to figure out the nearest possible free spot from the cursor. I have found circle packing algorithms, but they do not seem a fit for my problem. I have also solved a similar problem in the past for boxes (here), but with circles, I cannot seem to figure it out.

I figured out how I can find the nearest free position when it intersects with one circle, or even when two are involved. However, I cannot find a robust algorithm that can deal with complex cases that have any number of circles in any arrangement.

Precise description of problem: I have a 2D space with any number of non-intersecting circles, all with identical radii (though that may not matter). I want to find a position for the next circle that will make it not intersect with any other circle, and which center [x,y] is nearest to a specified location [x,y].

Suggestions of any kind appreciated (references, approaches, or (Java) libraries).

p.s. Bonus points if the solution includes making sure the circle stays within a specific bounding box (i.e. display).

My final solution: (based on David Wallace's suggestions)

  • Calculate the minimal distance between the centers of two circles (in my case, all circles are the same size, so always 2*radius)
  • Make a list of all circles that are closer to the mouse position than the minimum distance
  • If 0 overlaps: all good!
  • If 1 overlap: move the new circle's center to the minimum distance from the compared circle's center, along the vector that runs from compared circle's center to mouse position
  • If 2 overlap: find out where the two overlapping circles intersect. Place the new circle on the intersection closest to the mouse position. If this position still overlaps with any circle, move to the other intersection. If that one doesn't work, leave the new circle were it is.
  • If 3 overlap: same as in 2 overlap, just take the two circles closest to the new circle.

Note that this does not work perfectly, but good enough in my case, where a user is dragging the new circle on the screen. It works in most cases and in those it doesn't, usually when there are many circles very close together, the new circle simply stays in the last position (which was valid). The user can then decide to drag it a fit further and be more precise in where he wants the new circle to go.

like image 537
Walmink Avatar asked Oct 15 '13 07:10

Walmink


2 Answers

This isn't a complete answer, but you may be able to make it into one.

Suppose you've already placed circles of radii r1, r2, r3 ... rn with centres C1, C2, C3 ... Cn, and you're looking to place a new circle of radius rz, the new circle's centre will have to be outside all of a set of "enlarged" circles, centred at C1, C2, C3 ... Cn; with radii (r1+rz), (r2+rz), (r3+rz) ... (rn+rz). So if the cursor is at point P, then there are some cases to consider.

(1) If P is not in any of the enlarged circles, then the problem is solved.

(2) If P is in just one of the enlarged circles, then move outwards along a radius of that circle, until you either reach a point that's outside all of the enlarged circles, or until you reach another enlarged circle. The former case reduces to scenario (1); the latter reduces to scenario (2). Pick an arbitrary direction if P happens to be the centre of the circle.

(3) If P is in several of the circles, then find the directions from P to each centre of a circle that it's in. Find the pair of directions that have the widest interval between them, and bisect that angle, to work out which direction to head along. For example, if the directions to the centres of the circles are 30deg, 120deg and 330deg, then bisect the angle between 120deg and 330deg - then head in a direction of 225deg. Head in that direction until you reach the edge of a circle, then recalculate. Keep doing this until you get back to scenario (2).

The thing that I can't work out is what to do if you get stuck in scenario (3). Maybe only allow a certain number of steps, then exit. After all, it's possible that there's no suitable place to put the circle.

like image 107
Dawood ibn Kareem Avatar answered Oct 13 '22 01:10

Dawood ibn Kareem


To calculate the distance between a point and a circle is with the center, considering your Circle class is like this one:

public class Circle{
    int x;
    int y;
    int radius;
}

public interface CircleHelper{
    public int distanceBetweenCircleAndPoint(Circle c, Point p);
    public int distanceBetweenTwoCircles(Circle c1, Circle c2);
}

First of all, I would think about using Quadtrees and check if there is any quad without surrounding circles

A quadtree

The quadtree deep can be selected considering the radius of the circles.

so if you have a point in one of the quads, you would look to its surrounding quads to check if there is any circle there and move from the point in the direction of empty quads.

I hope you understand my approach

like image 41
RamonBoza Avatar answered Oct 13 '22 00:10

RamonBoza