/** * crossPoint: 2条线相交的坐标 * line1: 坐标1的对象 * line2: 坐标2的对象 * pt = (x = 0, y = 0) 时不相交 */function crossPoint(line1, line2) { // let line1 = { line1, line2 结构相同 // pStart: { x: 0, y: 0 }, // pEnd: { x: 2, y: 2 }, // } let pt = new egret.Point(0, 0) // line1's cpmponent let X1 = line1.pEnd.x - line1.pStart.x;//b1 let Y1 = line1.pEnd.y - line1.pStart.y;//a1 // line2's cpmponent let X2 = line2.pEnd.x - line2.pStart.x;//b2 let Y2 = line2.pEnd.y - line2.pStart.y;//a2 // distance of 1,2 let X21 = line2.pStart.x - line1.pStart.x; let Y21 = line2.pStart.y - line1.pStart.y; // determinant let D = Y1 * X2 - Y2 * X1;// a1b2-a2b1 // if (D == 0) return pt; // cross point pt.x = (X1 * X2 * Y21 + Y1 * X2 * line1.pStart.x - Y2 * X1 * line2.pStart.x) / D; // on screen y is down increased ! pt.y = -(Y1 * Y2 * X21 + X1 * Y2 * line1.pStart.y - X2 * Y1 * line2.pStart.y) / D; // segments intersect. if ((Math.abs(pt.x - line1.pStart.x - X1 / 2) <= Math.abs(X1 / 2)) && (Math.abs(pt.y - line1.pStart.y - Y1 / 2) <= Math.abs(Y1 / 2)) && (Math.abs(pt.x - line2.pStart.x - X2 / 2) <= Math.abs(X2 / 2)) && (Math.abs(pt.y - line2.pStart.y - Y2 / 2) <= Math.abs(Y2 / 2))) { return pt; } return pt;}