How to calculate the Euclidean distance

Asked

Viewed 11,601 times

6

I want to calculate the Euclidean distance by the following formula:

formula euclidiana

So I tried, making this code:

#define SLEEP_1 1000
void HeaderClass::DistanciaEuclidianaEntrePontos() {

    int x1, x2, y1, y2, distancia;

    std::cout << "Coordenadas ponto 1 (x): ";
    std::cin >> x1;

    std::cout << "Coordenadas ponto 1 (y): ";
    std::cin >> y1;

    Sleep(SLEEP_1);

    std::cout << "Coordenadas ponto 2 (x): ";
    std::cin >> x2;

    std::cout << "Coordenadas ponto 2 (y): ";
    std::cin >> y2;

    Sleep(SLEEP_1);

    distancia = sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2));

    std::cout << "Distancia Euclidiana: " << distancia << std::endl;
}

but still I can’t get what I want...

for example:

(4-7) ² + (2-5) ² = 12

but the program says :

(4-7) ² + (2-5) ² = -214(...)

There will be some more way to do it?

  • What you’re getting this way, and what you hoped to get?

  • I don’t know, give me a huge number, for example : (4-7) ² + (2-5) ² = 12 but in the program it says : - 214...

  • 1

    I gave an answer that should solve your problem, but I would like to ask: is int even if you want it, it wouldn’t be double? Thus the distance can be truncated, if it does not give an integer value.

  • Not because the double would conflict with the " 2 " (²)

  • @André I don’t understand... You can calculate the square of a double yeah, no problem...

  • In C/C++ the operator does not mean potentiation but OR Exclusive (Bitwise exclusive OR). Use the Pow function.

Show 1 more comment

3 answers

8


Your problem is on that line:

distancia = sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2));

In C++, the operator ^ is not the exponentiation, but the "or exclusive" (xor). You are making the xor of the difference between the numbers and 2, which is equivalent to changing the 2nd bit of the number and only. In C++ there is no exponentiation operator, that I know of, better simply multiply the difference by itself:

distancia = sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
  • 2

    @Bacco hehe is, sometimes these details go unnoticed... P P.S. O pow it would be nice if the sub-expressions had side effects, and could not be used twice (which is not the case here).

  • See how it works in IDEONE. http://ideone.com/rlmEy3 In fact, if you use the float you get 4 and some broken ones (which makes sense, because 4 * 4 = 16, almost 18)

  • For, in IDEONE gives 4.24264 >.> due to the fact of using float

  • yeah, I noticed

  • 1

    +1, although I do not like much of the solution to use (x2 - x1) * (x2 - x1) for the sake of code readability and for questions related to rounding for very small differences; hypot(x1-x2,y1-y2) is preferable to this as regards rounding.

6

I would do so, taking advantage of most of your code, using hypot(x,y) (only from C++11) which returns the square root of the quadratic sum of x and y:

#include <iostream>
#include <cmath>
using namespace std;

int main(){

    double x1, x2, y1, y2, distancia;

    cout << "Coordenadas ponto 1 (x): ";
    cin >> x1;

    cout << "Coordenadas ponto 1 (y): ";
    cin >> y1;

    cout << "Coordenadas ponto 2 (x): ";
    cin >> x2;

    cout << "Coordenadas ponto 2 (y): ";
    cin >> y2;

    distancia = hypot(x1-y1,x2-y2);

    cout << "Distancia Euclidiana: " << distancia << endl;

    return 0;

}

Note also that both the coordinates of the points and the distance cannot be integers (unless it has a discretized space). An interesting suggestion would be to write valid code for a space with an n-dimensional Euclidean metric or for spaces with different metrics.

  • 2

    +1 I did not know this hypot, to use in practice I agree that would be the best. But since in this case it is obviously an exercise, I imagine that the explicit calculation is even the most desired (either by multiplying the factor by itself, or by using the power function).

  • 3

    @mgibsonbr C++11 introduces some new functions in the `cmath , in particular the inverse hyperbolic functions, the cube root, error and gamma functions, plus some rounding, etc. I know it is not in everyone’s interest, but they are very welcome. In relation to your answer, I believe that it was very educational because it pointed out the differences between operators. By way of confession: I would prefer there to be, in fact, an operator for exponentiation in C++.

  • it is easy to remember the name of the function, in the end the result is even the hypotenuse.

1

What you can also do is create a function that does the exponentiation and uses it to compute the square of the number.

Example:

int exp (int num){
    return num * num;
}
distancia = sqrt((exp(x2 - x1)) + (exp(y2 - y1)));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.