Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance approximation?

I am doing pathfinding on a 2D grid.

I need to calculate distance as one of my heuristics.

Furthermore I need to return the closest spot if the full path is not found.

Calculating the exact distance to a double precision seems like unnecessary overhead. Is there any fast approximation I can use, which will still be accurate enough to meet my needs? (within rounding accuracy of 1)

By the way, path lengths are typically only around 5-30 nodes, so using a more accurate function at the end wouldn't be worth it.

like image 964
user1012037 Avatar asked Oct 27 '11 08:10

user1012037


People also ask

What is the formula to figure out distance?

Learn how to find the distance between two points by using the distance formula, which is an application of the Pythagorean theorem. We can rewrite the Pythagorean theorem as d=√((x_2-x_1)²+(y_2-y_1)²) to find the distance between any two points.

Is Manhattan a distance?

Manhattan distance is a distance metric between two points in a N dimensional vector space. It is the sum of the lengths of the projections of the line segment between the points onto the coordinate axes. In simple terms, it is the sum of absolute difference between the measures in all dimensions of two points.


2 Answers

I need to return the closest spot if the full path is not found.

In this case you could skip the square root operation in the distance calculation, i.e. compare squared distances using just dy * dy + dx * dx.

This works since a2 < b2 if and only if a < b for two arbitrary distances a and b.

In a 2D grid this would be implemented purely with integers.

If you need non-integer values, I'd probably go with doubles until that proves to be a bottleneck.

like image 167
aioobe Avatar answered Nov 02 '22 05:11

aioobe


If it is a 2D grid you could consider using the Manhattan distance. This would allow you to work in grid units all the time and avoid the square root. As aioobe suggests, this is probably micro-optimizing.

like image 43
Jeff Foster Avatar answered Nov 02 '22 04:11

Jeff Foster