How do I calculate Euclidean distance

Asked

Viewed 3,097 times

-1

I have a class Ponto:

public class Ponto {
    public int x;
    public int y;

    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
}

I have to create a "distance" method that takes another instance per parameter and calculates the Euclidean distance between the encapsulated point and the point passed by argument.

How do you calculate Euclidean distance:

Be the points A=(Xa, Ya) B=(Xb, Yb)

then the distance is

d² = (Xa-Xb)² + (Ya-Yb)²

Isolating:

d = root ( (Xa-Xb)² + (Ya-Yb)² )

Problem: how do I calculate this in the algorithm?

  • 5

    you are struggling at some point in specific to your code? you already have the formula, already have everything, just implement

1 answer

1

You can do it like this:

  public int distancia(Ponto p){
      //algoritmo que calcula distancia entre (this.x, this.y)
      //e (p.getX(), p.getY()) passados como argumento Ponto p.
  } 

Browser other questions tagged

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