我目前正在尝试开发一个三边测量应用程序来使用 3 部手机跟踪信标。我将在 python 中找到的代码转换为 c#,但我无法让它工作。这是我的代码:
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;
}
当我运行这个方法
测试用例 #1:
- 电话A_x&y = (0,0)
- 电话B_x&y = (100,0)
- PhoneC_x&y = (50,100)
- 电话距离 = 0
- 电话距离 = 100
- 电话距离 = 111.803
它返回 (-488.195, -366.147)
测试用例 #2:
- 电话A_x&y = (0,0)
- 电话B_x&y = (100,0)
- PhoneC_x&y = (50,100)
- 电话距离 = 25
- 电话距离 = 25
- 电话距离 = 25
它返回 (50, 37.5)