2014-12-19 32 views
2

我正在計算使用三角測量的用戶的位置。爲什麼從數組中獲取NaN(不是數字)錯誤?

如果我使用數組值它輸出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); 

有人可以向我解釋這個嗎?我感到很困惑。

+0

這怎麼是一個重複的???我只是沒有意識到我愚蠢的錯誤。 – smj2393 2014-12-19 11:47:39

+1

,而實際的錯誤可能是重複的(即解決方案是相同的),問題是不同的。問題是否因爲答案重複而關閉?那聽起來不錯 – atmd 2014-12-19 11:51:54

+1

正確...如果是這種情況,Stackoverflow上的大多數問題都將重複! – smj2393 2014-12-19 12:12:03

回答

3

訪問數組的方式存在一個小錯誤。你需要

parseInt(beaconCoordinates[0][0]); 

而不是

parseInt(beaconCoordinates[0,0]);

+0

Ahhhhh,謝謝。這樣的錯誤!它總是愚蠢的錯誤,最難倒你:( – smj2393 2014-12-19 11:44:33

+1

是的,這是一個,你可以看幾個小時,仍然錯過它,有時另一雙眼睛撿起最小的東西 – atmd 2014-12-19 11:49:38

0

的問題是,你只得到了最高級別陣列,你不能訪問值ARR [0,0],而不是你需要在一個時間得到的一個值:ARR [0] [0]

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函數,如果你正在使用它......

Why do we need to use radix?

http://www.w3schools.com/jsref/jsref_parseint.asp

+0

是的,謝謝。錯誤! – smj2393 2014-12-19 11:46:01

相關問題