kingBook

导航

点与线的距离及垂足点

dist(10,0,0,0,10,10);

function dist(x:Number,y:Number,x1:Number,y1:Number,x2:Number,y2:Number):void{
    var se:Number=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);//线段两点距离平方
    var p:Number=((x-x1)*(x2-x1)+(y-y1)*(y2-y1));//向量点乘=|a|*|b|*cosA
    var r:Number=p/se; //r即点到线段的投影长度与线段长度比
    var outx:Number=x1+r*(x2-x1);//垂足x
    var outy:Number=y1+r*(y2-y1);//垂足y
    var des =(x-outx)*(x-outx)+(y-outy)*(y-outy);//与垂足距离
    trace(outx);
    trace(outy);
    trace(Math.round(Math.sqrt(des)));
}
/*output:
 5
 5
 7
*/

 http://blog.csdn.net/guoziqing506/article/details/52575611

\[
\overrightarrow{AB}
\]

 

posted on 2017-07-28 15:12  kingBook  阅读(1190)  评论(0编辑  收藏  举报