更新
感谢用户@Thomas,我能够修复我的脚本。 您可以在下面的评论部分找到他的评论。
为了更容易,这里是最重要的引用:
[...]为什么你的向量包含字符串而不是数字?
我意外地继续在我的向量中使用字符串,从而破坏了正确的结果。 使用数字时,请确保不要使用任何字符串。
此外,请确保您的结果排序正确:
排序依据:
min(|| house.min - client ||,|| house.max - client ||)
或
||(house.max + house.min)/ 2 - client ||
问题:
我有一个有3个房子的阵列,每个房子包含两个X,Y,Z向量。其中一个是最小点,而另一个是最大值。这两个向量代表房子所在的区域/区域。
此外,我有一个客户端,由一个向量表示。
现在我希望得到客户所在的最近的房子。优选地基于房屋的最小和最大矢量。
值:
var houses = [
{
"max": { x: "-7839.9688", y: "-7469.0312", z: "215.537" },
"name": "House 1",
"min": { x: "-7112.0312", y: "-7932.9692", z: "72.0312" }
}, {
"max": { x: "-6380.0312", y: "-9586.1963", z: "372.1475" },
"name": "House 3",
"min": { x: "-6764.2441", y: "-9189.0312", z: "96.3953" },
}, {
"max": { x: "-7747.1074", y: "-8659.9023", z: "422.2812" },
"name": "House 2",
"min": { x: "-6827.3853", y: "-9668.9688", z: "72.0312" },
}
]
请注意:上面的数组不代表下图。该图仅用于说明目的。在这种特殊情况下, 客户1离房子2很近。但是。在一个不同的用例中,他/她 可能离房子3很近。
插图/图:
方法1:
var houses = [
{
"max": { x: "-7839.9688", y: "-7469.0312", z: "215.537" },
"name": "House 1",
"min": { x: "-7112.0312", y: "-7932.9692", z: "72.0312" }
},
{
"max": { x: "-6380.0312", y: "-9586.1963", z: "372.1475" },
"name": "House 3",
"min": { x: "-6764.2441", y: "-9189.0312", z: "96.3953" },
},
{
"max": { x: "-7747.1074", y: "-8659.9023", z: "422.2812" },
"name": "House 2",
"min": { x: "-6827.3853", y: "-9668.9688", z: "72.0312" },
}
]
var client = {}
client.x = '-7514.48'
client.y = '-9443.54'
client.z = '-183.969'
var distances = []
for (var i = 0; i < houses.length; i++) {
var house = houses[i]
var distance_between_house_min_max = Math.sqrt(((house.min.x - house.max.x)*(house.min.x - house.max.x)) + ((house.min.y - house.max.y)*(house.min.y - house.max.y)) + ((house.min.z - house.max.z)*(house.min.z - house.max.z)));
// Here I would like to take house.max and house.min together in order to get the clients distance to a the house
var distance_of_client_to_house = Math.sqrt(((client.x - house.max.x)*(client.x - house.max.x)) + ((client.y - house.max.y)*(client.y - house.max.y)) + ((client.z - house.max.z)*(client.z - house.max.z)));
console.log("Distance to " + house.name + " is: " + distance_of_client_to_house)
distances.push({ house: house, distance: distance_of_client_to_house })
}
// Order distances by lowest distance
distances = distances.sort(function (a, b) {
return a.distance > b.distance
});
console.log("Distances:")
console.log(distances)
&#13;
方法2: 由于Stackoverflow不支持节点JS模块,因此我首先没有包含这种方法。这就是为什么我用字符串代替方法1而不是真正的&#39;矢量对象。
基本上,在这种方法中,我们使用vec3节点JS模块来提供向量操作。
var vector = require("vec3").Vec3
var houses = [
{
"max": new vector(-7839.9688, -7469.0312, 215.537),
"name": "House 1",
"min": new vector(-7112.0312, -7932.9692, 72.0312)
},
{
"max": new vector(-6380.0312, -9586.1963, 372.1475),
"name": "House 3",
"min": new vector(-6764.2441, -9189.0312, 96.3953),
},
{
"max": new vector(-7747.1074, -8659.9023, 422.2812),
"name": "House 2",
"min": new vector(-6827.3853, -9668.9688, 72.0312),
}
]
var client = new vector(-7514.48, -9443.54, -183.969)
var distances = []
for (var i = 0; i < houses.length; i++) {
var house = houses[i]
var distance_between_house_min_max = house.min.distanceTo(house.max);
// Here I would like to take house.max and house.min together in order to get the clients distance to a the house
var distance_of_client_to_house = client.distanceTo(house.max);
console.log("Distance to " + house.name + " is: " + distance_of_client_to_house)
distances.push({ house: house, distance: distance_of_client_to_house })
}
// Order distances by lowest distance
distances = distances.sort(function (a, b) {
return a.distance > b.distance
});
console.log("Distances:")
console.log(distances)
演示: https://tonicdev.com/stevemuster/57c3a52fee25751400967ddd
问题:我如何才能正确猜出客户所在的房屋?我愿意接受你的所有建议/提示。谢谢。