JavaScript algorithm question: distance between two points

JavaScript algorithm question: distance between two points

Given two points P1 and P2, where the coordinates of P1 are (x1, y1), and the coordinates of P2 are (x2, y2), please calculate the distance between the two points.

input format

Input a total of two lines, each line contains two double-precision floating-point numbers xi, yi, representing the coordinates of one of the points.

All input values ​​retain one decimal place.

output format

Print your results to four decimal places.

data range

−109≤xi,yi≤109

Input sample:

1.0 7.0
5.0 9.0

Sample output:

4.4721
let buf = "";

process.stdin.on("readable", function() {
    let chunk = process.stdin.read();
    if (chunk) buf += chunk.toString();
});


process.stdin.on("end", function() {
    let lines = buf.split('\n');
    let [x1, y1] = lines[0].split(' ').map(x => {return parseFloat(x)});
    let [x2, y2] = lines[1].split(' ').map(x => {return parseFloat(x)});

    let dx = x1 - x2;
    let dy = y1 - y2;

    console.log(Math.sqrt(dx * dx + dy * dy).toFixed(4));
});

Guess you like

Origin blog.csdn.net/qq_42465670/article/details/130489900