我正在使用三角剖分来计算用户的位置。
如果我使用数组值,它会输出NaN NaN,但是如果我硬编码这些值,它就会像预期的那样正常工作并输出。
从数组中获取值:
var beaconCoordinates = [[10,20], [200,300], [50,500]];
//get values from array
var aX = parseInt(beaconCoordinates[0,0]);
var aY = parseInt(beaconCoordinates[0,1]);
var bX = parseInt(beaconCoordinates[1,0]);
var bY = parseInt(beaconCoordinates[1,1]);
var cX = parseInt(beaconCoordinates[2,0]);
var cY = parseInt(beaconCoordinates[2,1]);对值进行硬编码:
var aX = 2;
var aY = 4;
var bX = 5.5;
var bY = 13;
var cX = 11.5;
var cY = 2;下面是代码的其余部分:
var dA = 5.7;
var dB = 6.8;
var dC = 6.4;
//trilateration / triangulation formula
var S = parseInt((Math.pow(cX, 2.) - Math.pow(bX, 2.) + Math.pow(cY, 2.) - Math.pow(bY, 2.) + Math.pow(dB, 2.) - Math.pow(dC, 2.)) / 2.0);
var T = parseInt((Math.pow(aX, 2.) - Math.pow(bX, 2.) + Math.pow(aY, 2.) - Math.pow(bY, 2.) + Math.pow(dB, 2.) - Math.pow(dA, 2.)) / 2.0);
var y = ((T * (bX - cX)) - (S * (bX - aX))) / (((aY - bY) * (bX - cX)) - ((cY - bY) * (bX - aX)));
var x = ((y * (aY - bY)) - T) / (bX - aX);
//x and y position of user
console.log(x,y);有人能给我解释一下吗?我真的很困惑。
发布于 2014-12-19 11:42:25
访问数组的方式稍有错误。你需要
parseInt(beaconCoordinates[0][0]);而不是那样
parseInt(beaconCoordinates[0,0]);
发布于 2014-12-19 11:43:05
问题是,您只获取顶级数组,不能访问arr 0、0值,而需要一次获得一个值:arr。
http://jsfiddle.net/ayqmLp2n/
var beaconCoordinates = [[10,20], [200,300], [50,500]];
//get values from array
var aX = parseInt(beaconCoordinates[0][0]);
var aY = parseInt(beaconCoordinates[0][1]);
var bX = parseInt(beaconCoordinates[1][0]);
var bY = parseInt(beaconCoordinates[1][1]);
var cX = parseInt(beaconCoordinates[2][0]);
var cY = parseInt(beaconCoordinates[2][1]);
console.log(cY);如果使用的话,还应该将基参数传递到parseInt .
parseint.asp
https://stackoverflow.com/questions/27565359
复制相似问题