I am trying to establish a Java method to calculate the x and y coordinate based off of three given coordinates and distances. I used the following post:Determining The Coordinates Of A Point Based On Its Known Difference From Three Other Points for guidance. I just can't get this to work properly and don't really understand the math. With my given inputs I should be outputting (1,4), but instead output a bunch of different results depending on what I make 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);
    }

}

Class w/ Method:

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

    }

}
有帮助吗?

解决方案

I guess that there is no problem with your program. The problem is that the four points that you chose have the same Y (the distance vectors are colinar). Therefore, M that is the determinant that is used by the cramer method to solve the linear system is always zero. So two divisions by zero arise in your program.

In this case, the solution is much simpler:

(x-xi)^2+(y-yi)^2=di^2

But y-yi=0. Threfore, x-xi=di.

So, I can write

x-x1=d1

x-x2=d2

x-x3=d3

Therefore, using any of these equations you get x=4 and Y is the same of the other points.

[PS: I think it is not a problem, but in the evaluation of Mx and My instead of dividing by 2 I would divide by 2.0 - just to be sure that integer division is not happening]

I hope I helped.

Daniel

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top