2

I am calculating the position of a user using triangulation.

If I use array values it outputs NaN NaN, but if I hardcode the values it works fine as expected and outputs

Grabbing the values from an array:

    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]);

Hardcoding the values:

    var aX = 2;
    var aY = 4;
    var bX = 5.5;
    var bY = 13;
    var cX = 11.5;
    var cY = 2;

Here is the rest of the code:

    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);

Can someone explain this to me please? I am so confused.

smj2393
  • 1,929
  • 1
  • 23
  • 50
  • How is this a duplicate??? I just didn't realise my stupid mistake. – smj2393 Dec 19 '14 at 11:47
  • 1
    whilst the actually error might be a duplicate (i.e. the solution is the same) the question is different. do questions get closed because the answer is duplicated?? that doesnt sound right – atmd Dec 19 '14 at 11:51
  • 1
    Exactly... If that was the case most of the problems on Stackoverflow would be duplicates! – smj2393 Dec 19 '14 at 12:12

2 Answers2

3

A slight error in how you are accessing the arrays. you'll need

parseInt(beaconCoordinates[0][0]);

rather then

parseInt(beaconCoordinates[0,0]);

atmd
  • 7,430
  • 2
  • 33
  • 64
  • Ahhhhh, thank you. Such a bad mistake! Its always the stupid mistakes that baffle you the most :( – smj2393 Dec 19 '14 at 11:44
  • 1
    yep, and it's one that you can look at for hours and still miss it, sometimes another pair of eye pick up the smallest of things – atmd Dec 19 '14 at 11:49
0

The issue is that you are only getting the top level array, you cannot access a value arr[0,0] instead you need to get one value at a time: 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);

You should also pass a radix parameter into parseInt if you are using it...

Why do we need to use radix?

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

Community
  • 1
  • 1
Simon Staton
  • 4,345
  • 4
  • 27
  • 49