How to take data from an object and do mathematical operation with another object in C++?

Asked

Viewed 211 times

1

I have to calculate the distance between two points in C++ (Object Oriented)

Here’s the code I got:

Ponto p1(2,-3);
Ponto p2(4,5);

class Ponto
{
public:
    Ponto(int x1, int y1) : x(x1), y(y1) {}

    CalculaDist ();

private:
    int x;
    int y;
};

How to do this?

3 answers

7

If you have two points a = [x1, y1], and b = [x2, y2], when entering the point c = [x1, y2], you will have a rectangle triangle with the right angle at the vertex c. This triangle has as one of the catetos, the side ac, measuring |y2-y1|. The other cateto is the side bc, measuring |x2-x1|. Calculating the size of these two headings is easy because they are aligned relative to one of the axes (one on x and the other on y), and therefore their length is the difference of the positions relative to the other axis.

With this, the distance between the points a and b can be calculated with Pythagoras' theorem because the ab is the hypotenuse of this right triangle.

Here’s what your code looks like:

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

class Ponto {
public:
    Ponto(int x1, int y1) : x(x1), y(y1) {}

    double calcular_distancia(Ponto &outro) {
        int a = x - outro.x;
        int b = y - outro.y;
        return sqrt(a * a + b * b);
    }

    int inline get_x() {
        return x;
    }

    int inline get_y() {
        return y;
    }
private:
    int x;
    int y;
};

int main() {
    Ponto p1(2, -3);
    Ponto p2(4, 5);
    double distancia = p1.calcular_distancia(p2);
    cout << distancia;
}

See here working on ideone.

5

To calculate the distance between two points in a Cartesian plane the following formula is applied:

formula

Based on the example of your question, follow a possible solution to your problem:

#include <iostream>
#include <cmath>

class Ponto
{
    public:

        Ponto( int x, int y ) : x(x), y(y) { }

        virtual ~Ponto( void ) { }

        double CalculaDist( const Ponto& p )
        {
            int x1 = p.x - this->x;
            int y1 = p.y - this->y;

            double d = std::pow(x1, 2) + std::pow(y1, 2);
            return std::sqrt(d);
        }

    private:

        int x;
        int y;
};


int main(void)
{
    Ponto p1( 2, -3 );
    Ponto p2( 4, 5 );

    std::cout << "Distancia: " << p1.CalculaDist( p2 ) << std::endl;

    return 0;
}

Exit:

$ ./distancia
Distancia: 8.24621

5

As I suspect that the concept of distance itself was not very clear, I leave here my contribution, just as a complement to the already quite good answers from @Victorstafusa and @Lacobus.

So I present a diagram that I have elaborated of the calculation that you are trying to make and its representations in the Cartesian plan.

gráfico

Note that the calculation for the red line, the distance, follows Pythagoras' theorem, as @Victorstafusa has already indicated.

This is formally presented as:

inserir a descrição da imagem aqui

For example:

  • h - hypotenuse corresponding to the distance, the red line
  • a - the heading corresponding to the blue line
  • b - cathode corresponding to the yellow line

Browser other questions tagged

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