javascript - Vector: Get closest vectors based on a single vector -
update:
thanks user @thomas able fix script. can find comments in comment section below.
to make easier, here important quotes:
[...] why vectors contain strings instead of numbers?
i accidently kept on using strings in vectors, breaking proper results. make sure not use strings when working numbers.
in addition, make sure results proper sorted:
sort by:
min(||house.min - client||, ||house.max - client||)
or
||(house.max + house.min)/2 - client||
the problem:
i have array 3 houses, each containing 2 x, y, z vectors. 1 of them min point, whereas other max. 2 vectors represent field/area house in.
in addition, have 1 client represented single vector.
now want closest houses client near to. preferably based on min , max vector of house.
values:
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" }, } ]
please note: array above not represent figure below. figure illustration purposes only. in particular case, client 1 close house 2. however. in different use-case, he/she might close house 3.
illustration / figure:
approach 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 = [] (var = 0; < 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 take house.max , house.min in order clients distance 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 " + house.name + " is: " + distance_of_client_to_house) distances.push({ house: house, distance: distance_of_client_to_house }) } // order distances lowest distance distances = distances.sort(function (a, b) { return a.distance > b.distance }); console.log("distances:") console.log(distances)
approach 2: since stackoverflow not support node js modules, did not include approach in first place. that's why headed approach 1 strings instead of 'real' vector objects.
basically, in approach use vec3 node js module offers vector manipulation.
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 = [] (var = 0; < houses.length; i++) { var house = houses[i] var distance_between_house_min_max = house.min.distanceto(house.max); // here take house.max , house.min in order clients distance house var distance_of_client_to_house = client.distanceto(house.max); console.log("distance " + house.name + " is: " + distance_of_client_to_house) distances.push({ house: house, distance: distance_of_client_to_house }) } // order distances lowest distance distances = distances.sort(function (a, b) { return a.distance > b.distance }); console.log("distances:") console.log(distances)
demo: https://tonicdev.com/stevemuster/57c3a52fee25751400967ddd
question: how can correctly guess houses client near to? open suggestions/hints. thank you.
Comments
Post a Comment