我正在尝试建立一个Java方法,根据三个给定的坐标和距离计算x和y坐标。我使用了以下帖子:Determining The Coordinates Of A Point Based On Its Known Difference From Three Other Points作为指导。我只是不能让它正常工作,并不真正理解数学。根据我给出的输入,我应该输出(1,4),而是根据我制作的d1,d2,d3输出一堆不同的结果。
public class Driver {
public static void main (String[] args)
{
double x1 = 1;
double y1 = 1;
double x2= 2;
double y2 = 1;
double x3= 3;
double y3 = 1;
double d1 = 3;
double d2 = 2;
double d3 = 1;
Main control = new Main();
control.GET_POINT(x1,y1,x2,y2,x3,y3,d1,d2,d3);
}
}
类w /方法:
public class Main {
public void GET_POINT(double x1, double y1,double x2,double y2,double x3,double y3, double r1, double r2, double r3){
double A = x1 - x2;
double B = y1 - y2;
double D = x1 - x3;
double E = y1 - y3;
double T = (r1*r1 - x1*x1 - y1*y1);
double C = (r2*r2 - x2*x2 - y2*y2) - T;
double F = (r3*r3 - x3*x3 - y3*y3) - T;
// Cramer's Rule
double Mx = (C*E - B*F) /2;
double My = (A*F - D*C) /2;
double M = A*E - D*B;
double x = Mx/M;
double y = My/M;
System.out.println(x);
System.out.println("and ");
System.out.println( y);
}
}
答案 0 :(得分:2)
我猜您的程序没有问题。问题是你选择的四个点具有相同的Y(距离矢量是colinar)。因此,作为Cramer方法用于求解线性系统的行列式的M总是为零。因此,您的计划中会出现两个除以零的部门。
在这种情况下,解决方案更简单:
(X-11)^ 2 +(Y基)^ 2 =二^ 2
但是y-yi = 0。因此,x-xi = di。
所以,我可以写
X-X1 = D1
的x X2 = D2
X-X3 = D3
因此,使用这些方程式中的任何一个,得到x = 4,Y与其他点相同。
[PS:我认为这不是问题,但在评估Mx和My而不是除以2时我会除以2.0 - 只是为了确保整数除法不会发生]
我希望我能帮忙。
丹尼尔