Walmink
Walmink

Reputation: 177

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)

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.

Upvotes: 7

Views: 1746

Answers (3)

Dag Ågren
Dag Ågren

Reputation: 1148

Here is a solution that will work for varying radiuses, and can be simplified if all radiuses are equal, as in your case. We first transform the problem slightly. Instead of fitting a circle among other circles, we extend the radiuses of all other circles by the radius of our circle to place, and instead try to place a point outside of these extended circles. This is equivalent to the original problem. We proceed as follows:

  1. First a special case. If the point is outside of all circles, we have a trivial solution.
  2. Find all the circles the point is inside. Calculate the closest point on their circumference (just move out from the original point along the radius).
  3. Find all the intersection points between pairs of circles.
  4. Combine the sets of points from steps 2 and 3, and filter these by finding the ones that are not covered by any other circle.
  5. Pick the closest point from the remaining set. Done!

This seems to be O(n^3), so not terribly fast, but should be doable if your set is not too huge.

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

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.

Upvotes: 4

RamonBoza
RamonBoza

Reputation: 9038

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

Upvotes: 2

Related Questions