基于凸多边形离散点排序的研究

发布时间 2023-10-11 20:36:59作者: 漫思
  OrderBy() {
    var vertices1 = _.cloneDeep(this.polygon);
    var xArray = vertices1.map((item) => item.x);

    var yArray = vertices1.map((item) => item.y);

    const [minX, maxX, minY, maxY] = [_.min(xArray), _.max(xArray), _.min(yArray), _.max(yArray)];

    //拿到原点坐标
    const centerPoint = new Point(
      ((minX || 0) + (maxX || 0)) * 0.5,
      ((minY || 0) + (maxY || 0)) * 0.5,
    );
    // console.log('centerPoint');
    // console.log(centerPoint)
    // console.log('centerPoint');
    var res: PointWidthArc[] = vertices1.map((item) => {
      var oax = item.x - centerPoint.x;
      var oay = item.y - centerPoint.y;
      var arc = Math.atan2(oay, oax);
      return new PointWidthArc(item.x, item.y, arc);
    });

    res.sort((a, b) => {
      return a.Arc - b.Arc;
    });

    return res;
  }

js代码奉上