你的写作方式
lineDistance
它需要两个参数,形式为
{ x: Number, y: Number }
,所以有两个点对象,每个对象都有一个
x
以及
y
协调。因为在你的例子中你只传递了两个数字,所以它不会起作用。
请参见下面的工作示例:
function lineDistance( point1, point2 ){
let xs = point2.x - point1.x;
xs = xs * xs;
let ys = point2.y - point1.y;
ys = ys * ys;
return Math.sqrt( xs + ys );
}
const pos = lineDistance({x : 206.54, y: 300.76}, {x : 204.03, y: 505.37});
document.querySelector(".distance").textContent = pos;
<p class="distance"></p>