An algorithm for finding all points on a two-dimensional grid at some distance from another point

I have a point on a 2D grid (x, y), and I need to find all the points located at a distance n from this point. The way I measure the distance is to use the formula for the distance between two points. Does anyone know how to do this?

Edit: just for reference, what I'm trying to do is write some kind of AI path search that will be at some distance from the target in a system using grid-based locations. I am currently using A * path finder, but I'm not sure if it matters or matters, as I am new to this.

+5
source share
5 answers

Here is what I will do:

  • , , D, x y. , , D. , . .

  • . , D * sqrt (2)/2 x y, , , D. , .

  • , D. . , D = sqrt (Δx 2 + Δy 2), D 2= Δx 2 + Δy 2.
    , .


, :

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
+2

. , : , .

O (N ^ 2). , , . , R-Tree, .

0

, n, , sin/cos?

:

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

This will print each dot n in 360 degree increments.

0
source

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;
}
0
source

All Articles