【问题标题】:How do you preform a distance calculation and get answer to display in JavaScript?您如何执行距离计算并获得答案以在 JavaScript 中显示?
【发布时间】:2013-03-08 21:10:19
【问题描述】:

我不知道如何使教师给出的公式起作用并显示和回答。他给出的公式是:

distance = Math.sqrt( ( (x2-x1)*(x2-x1) ) + ( (y2-y1)*(y2-y1) ) );

【问题讨论】:

  • 看起来像家庭作业。对吗?
  • 家庭实验室 4
  • 正文中还有一个表格可以输入x1、x2、y1和y2的值。

标签: javascript computer-science


【解决方案1】:

你的公式是错误的。正确的是:sqrt( (x2 - x1)^2 + (y2 - y2)^2 )

// Two points to find the distance between
var a = [564,426];
var b = [56,784];

// subtract the x's and square the result
var xN = Math.pow( b[0] - a[0], 2 );

// subtract the y's and square the result
var yN = Math.pow( b[1] - a[1], 2 );

// Add the two results together then find their square root
var distance = Math.sqrt(xN + yN);

资源:http://www.mathwarehouse.com/algebra/distance_formula/index.php

【讨论】:

    【解决方案2】:

    这是(x2-x1)*(x2-x1),而不是(x2-x1)*(c2-x1)

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多