1 /** 2 * 获取两条相交线的交点坐标信息 3 * @param {any} 线1 4 * @param {any} 线2 5 * @param {any} 回调函数 6 */ 7 intersect: function (geometry_r1, geometry_r2, func) { 8 var path1 = geometry_r1.paths[0]; 9 var path2 = geometry_r2.paths[0]; 10 for (var i = 0; i < path1.length - 1; i++) { 11 for (var j = 0; j < path2.length - 1; j++) { 12 var a1 = path1[i][1] - path1[i + 1][1]; 13 var b1 = path1[i + 1][0] - path1[i][0]; 14 var c1 = a1 * path1[i + 1][0] + b1 * path1[i + 1][1]; 15 //转换成一般式: Ax+By = C 16 var a2 = path2[j][1] - path2[j + 1][1]; 17 var b2 = path2[j + 1][0] - path2[j][0]; 18 var c2 = a2 * path2[j + 1][0] + b2 * path2[j + 1][1]; 19 // 计算交点 20 var d = a1 * b2 - a2 * b1; 21 // 当d==0时,两线平行 22 if (d == 0) { 23 return false; 24 } else { 25 var x = (b2 * c1 - b1 * c2) / d; 26 var y = (a1 * c2 - a2 * c1) / d; 27 // 检测交点是否在两条线段上 28 if ((isInBetween(path1[i + 1][0], x, path1[i][0]) || isInBetween(path1[i + 1][1], y, path1[i][1])) && 29 (isInBetween(path2[j + 1][0], x, path2[j][0]) || isInBetween(path2[j + 1][1], y, path2[j][1]))) { 30 require(["esri/geometry/Point"], function (Point) { 31 var pt = new Point(x, y, map2dBase.map.spatialReference); 32 func(pt); 33 }); 34 } 35 } 36 } 37 //如果b在a和c之间,返回true 38 //当a==b或者b==c时排除结果,返回false 39 function isInBetween(a, b, c) { 40 // 如果b几乎等于a或c,返回false.为了避免浮点运行时两值几乎相等,但存在相差0.00000...0001的这种情况出现使用下面方式进行避免 41 if ((Math.abs(a - b) > 0.000001 || Math.abs(b - c) > 0.000001) && ((b > a && b < c) || (b < a && b > c)) || (a == b || b == c)) { 42 return true; 43 } else { 44 return false; 45 } 46 } 47 } 48 }