gpt4 book ai didi

javascript - 计算对象中的哪个坐标最接近所选坐标

转载 作者:行者123 更新时间:2023-11-28 03:30:12 25 4
gpt4 key购买 nike

我的数组中有一些带有坐标的对象。

var players = [
{x:100, y:100, pid:1},
{x:-100, y:200, pid:2},
{x:600, y:1200, pid:3}
]

var mousepos = {x:50, y:0}
var 已选择 = null

我想计算这些对象中哪个与 mousepos 的坐标最接近以选择它 pidselected = 最接近 mousepos 的坐标的 pid

是否有人可以给我简单的代码?

最佳答案

老兄,这就是答案。使用距离公式计算点之间的距离。距离最小的点是最近的点。

公式:- Sqrt((x2-x1)2 + (y2-y 1)2)

关于javascript - 计算对象中的哪个坐标最接近所选坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com