cytoscape.js 返回段距离和段权重值

从坐标(PointX,PointY)返回段距离和段权重
segment-distances segment-weights

function getDistWeight(sX, sY, tX, tY, PointX, PointY){
 var W, D;

D = ( PointY - sY + (sX-PointX) * (sY-tY) / (sX-tX) ) / Math.sqrt( 1 + Math.pow((sY-tY) / (sX-tX), 2) );
 W = Math.sqrt( Math.pow(PointY-sY,2) + Math.pow(PointX-sX,2) - Math.pow(D,2) );

var distAB = Math.sqrt(Math.pow(tX-sX, 2) + Math.pow(tY-sY, 2));
 W = W / distAB;

//check whether the point (PointX, PointY) is on right or left of the line src to tgt. for instance : a point C(X, Y) and line (AB). d=(xB-xA)(yC-yA)-(yB-yA)(xC-xA). if d>0, then C is on left of the line. if d<0, it is on right. if d=0, it is on the line.
 var delta1 = (tX-sX)*(PointY-sY)-(tY-sY)*(PointX-sX);
 switch (true) {
 case (delta1 >= 0) :
 delta1 = 1;
 break;
 case (delta1 < 0) :
 delta1 = -1;
 break;
 }
 //check whether the point (PointX, PointY) is "behind" the line src to tgt
 var delta2 = (tX-sX)*(PointX-sX)+(tY-sY)*(PointY-sY);
 switch (true) {
 case (delta2 >= 0) :
 delta2 = 1;
 break;
 case (delta2 < 0) :
 delta2 = -1;
 break;
 }

D = Math.abs(D) * delta1; //ensure that sign of D is same as sign of delta1. Hence we need to take absolute value of D and multiply by delta1
 W = W * delta2;

return {
 ResultDistance: D, 
 ResultWeight: W
 };
}

var point = getDistWeight(10, 5, 25, 15, 9, 6);
console.log(point);

演示

上一篇:LeetCode 780. Reaching Points


下一篇:ABAP自动寻找增强点的程序