user1543957
user1543957

Reputation: 1828

Inbuilt function to find distance

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.

Upvotes: 10

Views: 9033

Answers (6)

Konrad Rudolph
Konrad Rudolph

Reputation: 545508

Well, you can use arithmetic on complex numbers:

using point_t = std::complex<int>;

double distance(point_t a, point_t b) {
    return std::abs(b - a);
}

I realise that this doesn’t quite fulfil your requirement of not writing your own function but the actual distance logic is implemented in the std::norm function. It just returns the square of the distance.

Upvotes: 7

Meyer
Meyer

Reputation: 1702

With C++11, the hypot function has been added to the standard library. It computes sqrt(x^2 + y^2), and provides some protection against overflows. It is a convenient way to find the Euclidean distance between two points:

#include <cmath>
#include <iostream>

struct Point{double x; double y;};

int main()
{
  Point a{0.0, 0.0};
  Point b{3.0, 4.0};

  double distance = std::hypot(a.x-b.x, a.y-b.y);

  std::cout << distance << std::endl;
}

Upvotes: 5

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

Well I think it's easy to find:

The linear distance between two points in a 3D Space.

d = sqrt( ( x2 - x1 )^2 + ( y2 - y1 )^2 + ( z2 - z1 )^2 )

The Manhattan distance is different, very used in 2D games:

d = | ( x2 - x1 ) | + | ( y2 - y1 ) |

typedef struct {
    float x, y, z;
} point_t;

typedef struct {
    int x, y;
} point2d_t;

double distanceFinder( point_t a, point_t b )
{
    return sqrt( pow( a.x-b.x, 2.0 ) + pow( a.y-b.y, 2.0 ) + pow( a.z-b.z, 2.0 ) );
}

int manhattanFinder( point2d_t a, point2d_t b)
{
   /* Considering the points have integer coordinates and is a 2D game */
   return abs( a.x - b.x ) + abs( a.y - b.y );
}

Upvotes: 1

Richard
Richard

Reputation: 61239

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.

Upvotes: 1

Martin Vidner
Martin Vidner

Reputation: 2337

Boost.Geometry claims to have functions for Cartesian and non-Cartesian distance.

Upvotes: 2

emartel
emartel

Reputation: 7773

No, as a 2D vector is not a type part of the language.

Depending on your needs, there are many math / game / simulation libraries that can be used that implement 2D coordinate objects and will provide you with functions to find the distance between such points.

Upvotes: 1

Related Questions