6

I have some point on a 2D grid (x, y) and I need to find all points that are n distance away from that point. The way I'm measuring distance is by using the distance formula between the two points. Anyone know how to do this?

Edit: Just for reference, what I'm trying to do is to write some AI path finding that will maintain some distance away from a target in a system that uses grid based locations. Currently I'm using A* path finding, but I'm not sure if that matters or makes a difference since I'm kind of new to this stuff.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Justin G
  • 776
  • 1
  • 10
  • 25

5 Answers5

2

Here's what I would do:

  1. First filter out all points that are further than D on either x or y. These are certainly outside the circle of radius D. This is a much simpler computation, and it can quickly eliminate a lot of work. This is a outer bounding-box optimization.

  2. You can also use an inner bounding-box optimization. If the points are closer than D * sqrt(2)/2 on either x or y, then they're certainly within the circle of radius D. This is also cheaper than calculating the distance formula.

  3. Then you have a smaller number of candidate points that may be within the circle of radius D. For these, use the distance formula. Remember that if D = sqrt(Δx2+Δy2), then D2 = Δx2+Δy2.
    So you can skip the cost of calculating square root.


So in pseudocode, you could do the following:

for each point
begin
    if test 1 indicates the point is outside the outer bounding box, 
    then skip this point

    if test 2 indicates the point is inside the inner bounding box, 
    then keep this point

    if test 3 indicates the point is inside the radius of the circle, 
    then keep this point
end
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • You can further optimize the algorithm by using spatial indexes. This will be true if the number of points is really high. If the number of points is not that big, the cost of constructing the spatial index may not be worth. – Alceu Costa Dec 31 '11 at 20:29
  • The grid isn't very large. I think the current planned size of the world is only going to be around 256x256. – Justin G Dec 31 '11 at 20:41
  • OK, so first I would filter the points that have both their X and Y distance within the distance, then filter those points using the inner bounding-box check? Not sure how to use the second part. – Justin G Dec 31 '11 at 21:55
  • Sweet, thanks for that :) I need to do some simple testing to make sure I'm getting the values I want but I think this might be exactly what I was looking for. – Justin G Dec 31 '11 at 23:47
0

Java implementation:

public static Set<Point> findNearbyPoints(Set<Point> pts, Point centerPt, double radius) {
    Set<Point> nearbyPtsSet = new HashSet<Point>();
    double innerBound = radius * (Math.sqrt(2.0) / 2.0);
    double radiusSq = radius * radius;
    for (Point pt : pts) {
        double xDist = Math.abs(centerPt.x - pt.x);
        double yDist = Math.abs(centerPt.y - pt.y);
        if (xDist > radius || yDist > radius)
            continue;
        if (xDist > innerBound || yDist > innerBound)
            continue;
        if (distSq(centerPt, pt) < radiusSq)
            nearbyPtsSet.add(pt);
    }
    return nearbyPtsSet;
}
zc22
  • 161
  • 1
  • 8
0

This problem is known as range query. The brute force solution is just as you described: computed the distance of all points from the reference point and return those whose distance is less than the desired range value.

The brute force algorithm is O(N^2). There are, however, more efficient algorithms that employ spatial indexes to reduce algorithm complexity and the number of distance calculations. For example, you can use a R-Tree to index your points.

Alceu Costa
  • 9,733
  • 19
  • 65
  • 83
0

Its called nearest neighbor search. More at http://en.wikipedia.org/wiki/Nearest_neighbor_search

There are open libraries for that. I have used one written for C and recommend it: http://www.cs.umd.edu/~mount/ANN/. ANN stands for Approximate Nearest Neighbor, however, you can turn the approximation off and find the exact nearest neighbors.

Tarandeep Gill
  • 1,506
  • 18
  • 34
  • Interesting stuff, but I think it's far too heavy for what I'm currently doing. I'll keep it in mind for later stuff though. – Justin G Dec 31 '11 at 23:50
0

This wouldn't use the distance formula, but if you're looking for points exactly n distance away, perhaps you could use sin/cos?

In pseudocode:

for degrees in range(360):
    x = cos(degrees) * n
    y = sin(degrees) * n
    print x, y

That would print every point n away in 360 degree increments.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224