Advertisement
arxeiss

Line cross

Oct 9th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var crossLines = function(px1,px2,py1,py2,x3,x4,y3,y4){
  2.             var x1 = Math.floor(Math.min(px1, px2)),x2 = Math.floor(Math.max(px1, px2)),y1 = Math.floor(Math.min(py1, py2)),y2 = Math.floor(Math.max(py1, py2));
  3.             x3 = Math.floor(x3);x4 = Math.floor(x4);y3 = Math.floor(y3);y4 = Math.floor(y4);            
  4.             var dDet = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  5.             if (dDet == 0)
  6.               return false;
  7.             var uLDet = (x1 * y2 - y1 * x2);
  8.             var dLDet = (x3 * y4 - y3 * x4);
  9.             var Px = Math.floor((uLDet * (x3 - x4) - (x1 - x2) * dLDet) / dDet);
  10.             var Py =  Math.floor((uLDet * (y3 - y4) - (y1 - y2) * dLDet) / dDet);
  11.             if (Px >= x1 && Px <= x2 && Py >= y1 && Py <= y2 && Px >= x3 && Px <= x4 && Py >= y3 && Py <= y4)
  12.               return true;
  13.             return false;
  14.           };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement