Distance between two points

Asked

Viewed 4,788 times

1

Read the four values corresponding to the axes x and y of any two points in the plan p1(x1,y1) and p2(x2,y2) and calculate the distance between them, showing 4 decimal places after the comma, according to the formula:

inserir a descrição da imagem aqui

The input file contains two lines of data. The first line contains two floating point values: x1 y1 and the second line contains two floating point values x2 y2. Exit Calculate and print the distance value according to the given formula, with 4 boxes after decimal point.

Input Example

1.0 7.0   
5.0 9.0 
saida = 4.4721

entrada   
-2.5 0.4  
12.1 7.3    
saída = 16.1484

My code is that way

import java.util.*;

public class Problema {

    public static void main(String[] args) {

        Scanner entrada = new Scanner(System.in);
        Formatter formato = new Formatter(Locale.ENGLISH);

        String valoresX;
        String valoresY;

        valoresX = entrada.nextLine(); //pega valores da 1º linha no caso -2.5 0.4
        valoresY = entrada.nextLine();//pega valores da 2º linha no caso 12.1 7.3

        String[] eixosX = valoresX.split(" "); // aqui jogo os valores separados de x em cada posição do vetor eixosX  
        double x1 = Double.parseDouble(eixosX[0]); //Converto a string para double
        double x2 = Double.parseDouble(eixosX[1]);//o mesmo aqui

        String[] eixosY = valoresY.split(" "); // mesmos passos acima mas agora para y
        double y1 = Double.parseDouble(eixosY[0]);
        double y2 = Double.parseDouble(eixosY[1]);



        double distancia = Math.sqrt((x2 - x1) * 2 + (y2 - y1) * 2);

        formato.format("%.4f", distancia);

        System.out.println(formato);

        entrada.close();

    }

}

my difficulty is in the example of the second entry where step -2.5 (space) 0.4 in the first line and 12.1(space) 7.3 in the second line and the result returned and Nan, I need the output to be 16.1484.

1 answer

1


If this entrance:

-2.5 0.4  
12.1 7.3

Corresponds to:

x1 y1
x2 y2

Then first you must correct the order in which things are read. You are considering that the first line has X1 and x2, when in fact it has X1 and Y1. The same goes for the second line (has x2 and Y2, but you’re reading as if it were Y1 and Y2), so the code to read the numbers would look like this:

String primeiraLinha = entrada.nextLine(); // pega valores da 1º linha no caso -2.5 0.4
String segundaLinha = entrada.nextLine();// pega valores da 2º linha no caso 12.1 7.3

String[] eixosPrimeiraLinha = primeiraLinha.split(" ");
double x1 = Double.parseDouble(eixosPrimeiraLinha[0]);
double y1 = Double.parseDouble(eixosPrimeiraLinha[1]);// aqui é y1, e não x2

String[] eixosSegundaLinha = segundaLinha.split(" "); 
double x2 = Double.parseDouble(eixosSegundaLinha[0]); // aqui é x2, e não y1
double y2 = Double.parseDouble(eixosSegundaLinha[1]);

Another detail is that while doing (x2 - x1) * 2, you are multiplying (x2 - X1) by 2. To square, use Math.pow(x2 - x1, 2).

So the calculation goes like this:

double distancia = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

With that, the distance value is 16.1484.


Another way to do it is to use nextDouble(), that already returns a double directly. The difference is that the Scanner must have a java.util.Locale set to recognize the point (.) as the decimal separator.

If you don’t use one Locale, he will use the default JVM and separator may not be the . (in my JVM, for example, the locale default is pt_BR, and the decimal separator is the comma).

So the code goes like this:

// usar Locale.US para usar ponto como separador decimal
Scanner entrada = new Scanner(System.in).useLocale(Locale.US);

// ler valores
double x1 = entrada.nextDouble();
double y1 = entrada.nextDouble();
double x2 = entrada.nextDouble();
double y2 = entrada.nextDouble();

// calcular distância
double distancia = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

The difference is that nextDouble() will read the numbers regardless of whether they are on the same line or not. Already the code with nextLine() and split() only works if there are at least two numbers on the same line (if there are more, they are ignored by our code as it only considers the first two positions).

Browser other questions tagged

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