Distance between two points

1

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

Theinputfilecontainstworowsofdata.Thefirstlinecontainstwofloatingpointvalues:x1y1andthesecondlinecontainstwofloatingpointvaluesx2y2.OutputCalculateandprintthedistancevalueaccordingtotheformulaprovided,with4housesafterthedecimalpoint.

InputSample

1.07.05.09.0saida=4.4721entrada-2.50.412.17.3saída=16.1484

Mycodeisthisway

importjava.util.*;publicclassProblema{publicstaticvoidmain(String[]args){Scannerentrada=newScanner(System.in);Formatterformato=newFormatter(Locale.ENGLISH);StringvaloresX;StringvaloresY;valoresX=entrada.nextLine();//pegavaloresda1ºlinhanocaso-2.50.4valoresY=entrada.nextLine();//pegavaloresda2ºlinhanocaso12.17.3String[]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 I pass -2.5 (space) 0.4 on the 1st line and 12.1 (space) 7.3 on the 2nd line and the result returned and NaN, I need the output to be 16.1484.

    
asked by anonymous 28.06.2018 / 01:12

1 answer

1

If this entry:

-2.5 0.4  
12.1 7.3

Corresponds to:

x1 y1
x2 y2

So first you must correct the order in which things are read. You're considering that the first row has x1 and x2, when in fact it has x1 and y1 . The same goes for the second line (you have x2 and y2, but you're reading as if it were y1 and y2), so the code to read the numbers looks 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 by doing (x2 - x1) * 2 , you are multiplying (x2 - x1) by 2. To square, use Math.pow(x2 - x1, 2) .

So, the calculation looks like this:

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

With this, the distance value is 16.1484 .

Another way to do this is to use nextDouble() , which 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 do not use a Locale , it will use the default of the JVM and the separator may not be . (in my JVM, for example, the default locale is pt_BR , and the decimal separator is the comma).

Then the code looks 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. Since the code with nextLine() and split() only works if you have at least two numbers on the same line (if there are more, they are ignored by our code, since it only considers the first two positions).

    
28.06.2018 / 01:56