按照与给定点的距离升序排序点数组

5

我需要你的帮助! 我有一个已知坐标的点,比如{x:5, y:4},还有一个包含多个点的对象数组:

[{x:2,y:6},{x:14,y:10},{x:7,y:10},{x:11,y:6},{x:6,y:2}]

现在我需要按照与给定点的距离进行升序排序数组,如下所示:
[{x: 6, y: 2}, {x: 2, y: 6}, {x: 7, y: 10}, {x: 11, y: 6}, {x: 14, y: 10}]

我该如何使用 JS 实现这个?谢谢!


MDN 文档中有很好的信息。 - Matt Ellen
以上情况中给定的点是什么? - Maheer Ali
你如何计算距离? - brk
2个回答

10

我认为,那可能有效:

//reference point
const a = {x:5,y:4};
//array of points to sort
const points = [{x:2,y:6},{x:14,y:10},{x:7,y:10},{x:11,y:6},{x:6,y:2}];
//squared distance
const sqDist = (pointa, pointb) => (pointa.x-pointb.x)**2+(pointa.y-pointb.y)**2;
//sorting
const res = points.sort((pointa, pointb) => sqDist(a,pointa)-sqDist(a,pointb));

console.log(res);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}


首先,将 const dist 从比较函数中取出。您不需要为每个比较定义此函数。其次,如果 distanceA > distanceB,那么 distanceA**2 > distanceB**2Math.sqrt() 是一项昂贵的操作,可以在此处省略。 - Thomas
@Thomas:我已经按照数学公式的建议完全完成了它,尽管我同意,为了性能的缘故可以进行某些优化。 - Yevhen Horbunkov

3

这是一个稍微缩短的版本,不使用Math.sqrt,因为它使用了增量的平方和。

const
   array = [{ x: 2, y: 6 }, { x: 14, y: 10 }, { x: 7, y: 10 }, { x: 11, y: 6 }, { x: 6, y: 2 }],
   point = { x: 5, y: 4 };

array.sort((a, b) =>
    (a.x - point.x) ** 2 + (a.y - point.y) ** 2 -
    (b.x - point.x) ** 2 + (b.y - point.y) ** 2
);

console.log(array)
.as-console-wrapper { max-height: 100% !important; top: 0; }


1
Math.abs((a.x - point.x) * (a.y - point.y)) - Math.abs((b.x - point.x) * (b.y - point.y)),因为负数乘积并不意味着负距离。 - Thomas
对于数组 [{ x: 2, y: 2 }, { x: 0, y: 0 }, { x: -2, y: -2 }, { x: -1, y: 1 }, { x: 1, y: -1 }] 和点 { x: 1, y: 1 },它返回了错误的结果。很可能需要将每个 delta 包含在 Math.abs() 中,这将使该方法不是非常简短。 - Yevhen Horbunkov
@U25lYWt5IEJhc3RhcmQg,你是正确的,请参见编辑。 - Nina Scholz
2
@NinaScholz:现在这与我的答案没有任何不同 :) - Yevhen Horbunkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接