计算两条线段的交点


 //线段平行或者不相交返回false 否则返回交点坐标
      getPolyLinesIntersection(line1, line2) {
//a b 代表线段1的两个端点  c d 代表线段2的两个端点
        let a = {
          x: line1[0][0], y: line1[0][1],
        }, b = {
          x: line1[1][0], y: line1[1][1],
        }, c = {
          x: line2[0][0], y: line2[0][1],
        }, d = {
          x: line2[1][0], y: line2[1][1],
        };
        /** 1 解线性方程组, 求线段交点. **/
          // 如果分母为0 则平行或共线, 不相交
        let denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x) * (c.y - d.y);
        if (denominator === 0) {
          return false;
        }
        // 线段所在直线的交点坐标 (x , y)
        let x = ((b.x - a.x) * (d.x - c.x) * (c.y - a.y)
          + (b.y - a.y) * (d.x - c.x) * a.x
          - (d.y - c.y) * (b.x - a.x) * c.x) / denominator;
        let y = -((b.y - a.y) * (d.y - c.y) * (c.x - a.x)
          + (b.x - a.x) * (d.y - c.y) * a.y
          - (d.x - c.x) * (b.y - a.y) * c.y) / denominator;
        /** 2 判断交点是否在两条线段上 **/
        if (
          // 交点在线段1上
          (x - a.x) * (x - b.x) <= 0 && (y - a.y) * (y - b.y) <= 0
          // 且交点也在线段2上
          && (x - c.x) * (x - d.x) <= 0 && (y - c.y) * (y - d.y) <= 0
        ) {
          // 返回交点p
          return {
            x: x,
            y: y
          }
        }
        //否则不相交
        return false
      },

你可能感兴趣的:(计算两条线段的交点)