Skip to content

Instantly share code, notes, and snippets.

@axetroy
Last active April 26, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axetroy/dda1fea8b5a568e943beb6572b935c13 to your computer and use it in GitHub Desktop.
Save axetroy/dda1fea8b5a568e943beb6572b935c13 to your computer and use it in GitHub Desktop.
坐标系中如何最快确定距离最近/远的两个点, 如果选择一条最优路线
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: 在一堆坐标系中, 从一个点, 到达另一个点, 最快的路线
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment