Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inbuilt function to find distance

Tags:

c++

c

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.

like image 546
user1543957 Avatar asked Nov 14 '12 15:11

user1543957


People also ask

What is dist () in Python?

dist() method in Python is used to the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension. This method is new in Python version 3.8. Syntax: math.dist(p, q)

What is distance function C++?

The distance() function in C++ helps find the distance between two iterators. In other words, we can use this function to calculate the number of elements present between the two iterators. This function is available in the <iterator> header file.

What is a function of distance?

What is a Distance Function? A distance function measures the distance between two points in a set. For example, you might want to find the distance between two points on a line. A distance function can also apply to other metric spaces. A metric space is a collection of objects.


1 Answers

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.

like image 161
Konrad Rudolph Avatar answered Oct 12 '22 16:10

Konrad Rudolph