title: Trilateration Formula (Programming)
body: <p>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:</p>
  
  <pre><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&lt;double&gt; P1 = new DenseVector(new[] { PhoneA_x, PhoneA_y });
          Vector&lt;double&gt; P2 = new DenseVector(new[] { PhoneB_x, PhoneB_y });
          Vector&lt;double&gt; 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;
      }
  </code></pre>
  
  <p>When I run this method</p>
  
  <p><em>Test case #1:</em> </p>
  
  <ul>
  <li>PhoneA_x&amp;y = (0,0) </li>
  <li>PhoneB_x&amp;y = (100,0)</li>
  <li>PhoneC_x&amp;y = (50,100)</li>
  <li>phoneADistance = 0</li>
  <li>phoneBDistance = 100</li>
  <li>phoneCDistance = 111.803</li>
  </ul>
  
  <p>it returns (-488.195, -366.147)</p>
  
  <p><em>Test Case #2:</em></p>
  
  <ul>
  <li>PhoneA_x&amp;y = (0,0) </li>
  <li>PhoneB_x&amp;y = (100,0)</li>
  <li>PhoneC_x&amp;y = (50,100)</li>
  <li>phoneADistance = 25 </li>
  <li>phoneBDistance = 25 </li>
  <li>phoneCDistance = 25</li>
  </ul>
  
  <p>it returns (50, 37.5)</p>