cytoscape.js return segment distance and segment weight value weight

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_33871182/article/details/102753857

Return segment distance and segment weights from the coordinates (PointX, Pointy)
segment-segment-weights Distances

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);

Show

Guess you like

Origin blog.csdn.net/qq_33871182/article/details/102753857