I'm currently trying to develop a trilateration application to track beacons using 3 phones. I converted code I found in python over to c# but I'm having trouble getting it to work. This is my code:
public double[] getPosition(double phoneADistance, double phoneBDistance, double phoneCDistance)
{
//meterToFeet is just a conversion method which takes the distance parameter and multiplies it by 3.28.
double PhoneADist = meterToFeet(phoneADistance);
double PhoneBDist = meterToFeet(phoneBDistance);
double PhoneCDist = meterToFeet(phoneCDistance);
//The phone's x and y coordinates are pre-set
Vector<double> P1 = new DenseVector(new[] { PhoneA_x, PhoneA_y });
Vector<double> P2 = new DenseVector(new[] { PhoneB_x, PhoneB_y });
Vector<double> P3 = new DenseVector(new[] { PhoneC_x, PhoneC_y });
var ex = (P2 - P1) / (P2 - P1).L2Norm();
var i = ex.DotProduct(P3 - P1);
var ey = (P3 - P1 - i * ex) / (P3 - P1 - i * ex).L2Norm();
var d = (P2 - P1).L2Norm();
var j = ey.DotProduct(P3 - P1);
var x = (Math.Pow(PhoneADist, 2) - Math.Pow(PhoneBDist, 2) + Math.Pow(d, 2)) / (2 * d);
var y = ((Math.Pow(PhoneADist, 2) - Math.Pow(PhoneCDist, 2) + Math.Pow(i, 2) + Math.Pow(j, 2)) / (2 * j)) - ((i / j) * x);
double[] answer = new double[] { x, y };
Console.Write(x + " " + y);
return answer;
}
When I run this method
Test case #1:
- PhoneA_x&y = (0,0)
- PhoneB_x&y = (100,0)
- PhoneC_x&y = (50,100)
- phoneADistance = 0
- phoneBDistance = 100
- phoneCDistance = 111.803
it returns (-488.195, -366.147)
Test Case #2:
- PhoneA_x&y = (0,0)
- PhoneB_x&y = (100,0)
- PhoneC_x&y = (50,100)
- phoneADistance = 25
- phoneBDistance = 25
- phoneCDistance = 25
it returns (50, 37.5)