欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS 计算两点间的直线距离

程序员文章站 2022-07-14 13:57:21
...

要完成此效果需要两个步骤

第一步:把如下代码加入到<head>区域中

 

 

 

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function distance(form) {
var x1 = eval(form.x1.value);
var y1 = eval(form.y1.value);
var x2 = eval(form.x2.value);
var y2 = eval(form.y2.value);
var xdiff = x2 - x1;
var ydiff = y2 - y1;
form.answer.value = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5);
}
//  End -->
</script>


第二步:把如下代码加入到<body>区域中

<center>
<form>
<table border=3 cellspacing=2 cellpadding=5>
<tr>
        <td colspan=4 align=center height="18">求两点的直线距离</td>
</tr>
<tr>
        <td colspan=2 align=center>点 A 座标</td>
        <td colspan=2 align=center>点 B 座标</td>
</tr>
<tr>
        <td align=center>X 轴</td>
        <td align=center>Y 轴</td>
        <td align=center>X 轴</td>
        <td align=center>Y 轴</td>
</tr>
<tr>
<td align=center><input type=text name=x1 size=5></td>
<td align=center><input type=text name=y1 size=5></td>
<td align=center><input type=text name=x2 size=5></td>
<td align=center><input type=text name=y2 size=5></td>
</tr>
<tr>
<td colspan=4 align=center>
          <input type=button value="得到结果" onClick="distance(this.form)">
        </td>
</tr>
<tr>
<td colspan=4 align=center><input type=text name=answer size=20></td>
</tr>
</table>
</form>
</center>
<a href="http://www.alixixi.com/Dev/HTML/jsrun/">欢迎访问阿里西西网页特效集</a>