title: Trilateration Method Android Java
body: <p>I have a little problem... Does anyone knows about how can i implement this?</p>
  
  <p>I need a method in java like this:</p>
  
  <pre><code>public /*COORDINATE OBJECT*/ getTrilaterationPointBetween(
  /*COORDINATE OBJECT*/ coordinate1, double distance1,
  /*COORDINATE OBJECT*/ coordinate2, double distance2,
  /*COORDINATE OBJECT*/ coordinate3, double distance3){
  
      //MAGIC
  
      return /*COORDINATE OBJECT*/;
  }
  </code></pre>
  
  <p>In practice this method will receive 3 coordinates and the distance between "myLocation" with one of them...</p>
  
  <p>I need the algorithm of the Trilateration!!</p>
  
  <p>I did some searches and made some tests and the results are driving me into the middle of the pacific!!</p>
  
  <p>About the OBJECT, is really simple, have 2 variables representing Latitude and Longitude.</p>
  
  <p>I have tried this method:</p>
  
  <pre><code>static double[] MyTrilateration(Ponto ponto1, double dist1,
                                      Ponto ponto2, double dist2,
                                      Ponto ponto3, double dist3) {
  
          double[] tmpWAP1 = new double[3];
          double[] tmpWAP2 = new double[3];
          double[] tmpWAP3 = new double[3];
  
          double tmpLat2, tmpLong2, tmpLat3, tmpLong3;
          double tmpSlide, deg;
          double MyLat, MyLong;
  
          double[] MyLocation = new double[2];
  
          tmpLat2  = ponto2.getX() - ponto1.getX();
          tmpLong2 = ponto2.getY() - ponto1.getY();
          tmpLat3  = ponto3.getX() - ponto1.getX();
          tmpLong3 = ponto3.getY() - ponto1.getY();
  
          tmpSlide = Math.sqrt(Math.pow(tmpLat2,2)+Math.pow(tmpLong2,2));
  
          deg = (180/Math.PI)*Math.acos( Math.abs(tmpLat2)/Math.abs(tmpSlide));
  
          if( (tmpLat2&gt;0 &amp;&amp; tmpLong2&gt;0) ) {
              deg = 360 - deg;
          }
          else if( (tmpLat2&lt;0 &amp;&amp; tmpLong2&gt;0) ) {
              deg = 180 + deg;
          }
          else if( (tmpLat2&lt;0 &amp;&amp; tmpLong2&lt;0)){
              deg = 180 - deg;
          }
          else if( (tmpLat2&gt;0 &amp;&amp; tmpLong2&lt;0)) {
              deg = deg;
          }
  
          tmpWAP1[0] = 0.0;
          tmpWAP1[1] = 0.0;
          tmpWAP1[2] = dist1;
          tmpWAP2 = myRotation(tmpLat2, tmpLong2, dist2, deg);
          tmpWAP3 = myRotation(tmpLat3, tmpLong3, dist3, deg);
  
  
          MyLat = (Math.pow(tmpWAP1[2],2)-Math.pow(tmpWAP2[2],2)+Math.pow(tmpWAP2[0],2))/(2*tmpWAP2[0]);
  
          MyLong = (Math.pow(tmpWAP1[2],2)-Math.pow(tmpWAP3[2],2)-Math.pow(MyLat,2)
                  +Math.pow(MyLat-tmpWAP3[0],2)+Math.pow(tmpWAP3[1], 2))/(2*tmpWAP3[1]);
  
          MyLocation = myRotation(MyLat, MyLong, 0, -deg);
  
          MyLocation[0] = MyLocation[0] + ponto1.getX();
          MyLocation[1] = MyLocation[1] + ponto1.getY();
  
          return MyLocation;
      }
  </code></pre>
  
  <p>and this method:</p>
  
  <pre><code>public String getCoordinateWith(
              Ponto a, Ponto b, Ponto c,
              Float dA,
              Float dB,
              Float dC) {
          Float W, Z, x, y, y2;
          W = dA*dA - dB*dB - a.getX()*a.getX() - a.getY()*a.getY() + b.getX()*b.getX() + b.getY()*b.getY();
          Z = dB*dB - dC*dC - b.getX()*b.getX() - b.getY()*b.getY() + c.getX()*c.getX() + c.getY()*c.getY();
  
          x = (W*(c.getY()-b.getY()) - Z*(b.getY()-a.getY())) / (2 * ((b.getX()-a.getX())*(c.getY()-b.getY()) - (c.getX()-b.getX())*(b.getY()-a.getY())));
          y = (W - 2*x*(b.getX()-a.getX())) / (2*(b.getY()-a.getY()));
          //y2 is a second measure of y to mitigate errors
          y2 = (Z - 2*x*(c.getX()-b.getX())) / (2*(c.getY()-b.getY()));
          y = (y + y2) / 2;
          return "Position: " + x + " , " + y;
      }
  </code></pre>
  
  <p>Thank you!</p>