<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>点与线的距离及垂足点</title>
</head>
<script language=javascript>
function dist(x, y, startx, starty, endx, endy)
{
var se = (startx-endx)*(startx-endx)+(starty-endy)*(starty-endy);//线段两点距离平方
var p = ((x-startx)*(endx-startx)+(y-starty)*(endy-starty)); //向量点乘=|a|*|b|*cosA
var r = p/se; //r即点到线段的投影长度与线段长度比
var outx=startx+r*(endx-startx);
var outy=starty+r*(endy-starty);
var des =(x-outx)*(x-outx)+(y-outy)*(y-outy);
alert(outx);
alert(outy);
alert(Math.round(Math.sqrt(des)));
}
</script>
<body>
<input type=button value='dist' οnclick="dist()">
<textarea id='textjs' rows="20" cols="120">
dist(5,20,1,6,20,15);
</textarea>
<br>
<input type="button" value="run javacript" οnclick="javascript:eval(textjs.value);">
</body>
<html>
点与线的距离及垂足点
最新推荐文章于 2023-01-29 14:45:53 发布