坐标系中如何最快确定距离最近/远的两个点, 如果选择一条最优路线

point.js
复制

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  distance(point) {
    const diffX = Math.abs(point.x - this.x);
    const diffY = Math.abs(point.y - this.y);
    return Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
  }
}
const coordinates = [
  new Point(46, 88),
  new Point(13, 68),
  new Point(18, 43),
  new Point(64, 21),
  new Point(22, 96),
  new Point(86, 76),
  new Point(92, 13),
  new Point(46, 53),
  new Point(33, 42)
];
const matrixDistance = coordinates.map(point1 =>
  coordinates.map(point2 => point1.distance(point2))
);
const MaxDistance = Math.max(...matrixDistance.map(arr => Math.max(...arr)));
const MinDistance = Math.min(
  ...matrixDistance.map(arr =>
    Math.min(...arr.filter(distance => distance !== 0))
  )
);

console.log(matrixDistance);    // 距离矩阵
console.log(MinDistance);       // 最近的距离
console.log(MaxDistance);       // 最远的距离

// TODO: 在一堆坐标系中, 从一个点, 到达另一个点, 最快的路线

大牛们的评论:朕有话说

还没有人评论哦,赶紧抢沙发!