Inbuilt function to find distance
An answer to this question on Stack Overflow.
Question
Is there any inbuilt function in c++ or c libraries that can be used to find distance between two points in 2-D space
PS: I know how to implement it myself.
Answer
Not so much.
2D distance is a mathematical function, and, if we look at the mathematical functions available to us in C/C++, we find that they operate on numbers.
But that's non-specific: what we actually find is that the functions have different names (in C) or are overloaded (in C++) to operate on different types (int, float, double, &c.) of numbers. Either this, or casting is performed.
Fortunately, there are limited types of numbers so it makes sense to have general libraries to do this kind of thing.
Now, could we construct a 2D distance function in the same way as we construct mathematical functions? You'll immediately see it's more difficult, as there are many ways to represent the points. For instance, cartesian vs. radial, x-y vs. i-j, double vs. float vs. int. Our general 2D distance would need to cover all these possibilities.
Most libraries which have a 2D distance function will have accompanying point structures to reduce the number of possibilities.
However, there is at least one data structure which is implemented that can store a point and be used to find 2D distance using standard libraries: complex numbers!
// norm example
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
complex<double> mycomplex (3.0,4.0);
cout << "The norm of " << mycomplex << " is " << norm(mycomplex) << endl;
return 0;
}
But this assumes you're talking about Euclidean distance. You could also be speaking about the Manhatten distance or more exotic metrics. Rather than try to account for all the possibilities I have mentioned, the language designers have opted not to implement this function. (Or any of the many, many other functions such as this which one might reasonably ask this question about).
EDIT:
Or you can subtract the points and use the hypot function from the C99 standard. See here.