3d trilateration with multiple known points.

Hi all, I am attempting to write a C# trilateration function, but it is beyond my limited math knowledge...

I have a number of known points (currently 8, but this number may change)
I have the x, y, z coordinates for each.

where each array has 3 values, x, y and z.

I know the distance from the unknown point (x)  to each of the known points.

double D1;
double D2;
double D3;
double D4;
double D5;
double D6;
double D7;
double D8;

So given these values, I need to find the xyz coordinates of the unknown point X[3];

What is the most straightforward way to do this? I have found examples in other code languages, but nothing that I understand in c#. And only ever with three points.
Any and all assistance would be much appreciated!

Thanks in advance!
Who is Participating?
d-glitchConnect With a Mentor Commented:
>> My issue is that am getting different results depending on which 3 points I use.

That means that there is an error (or errors) somewhere.  
Do you know why?  How accurately do you know the positions of the base stations?  How did you measure the distances?

>>  I am trialling cycling through groups of 3 and averaging the result. Is this the best way to go about it?

A better technique might be to minimize the RMS error.

You have n base stations A thru N    and   n distances  Da thru Dn.

Once you have an approximate solution X, calculate a new set of distances between this point and your base stations to get a new set of distances  Dax thru Dnx.

The RMS error is    sqrt[  (1 - Dax/Da)²   +   (1 - Dbx/Db)²   +   (1 - Dcx/Dc)²   +   . . .    +   (1 - Dnx/Dn)² ]  

Move your solution point in the x, y, and z directions to minimize the error.
chemicalx001Author Commented:

I have trilateration working with three known points. I converted this example:


to C#, and it seems to be going according to plan.

Now, to add more known points, would it be better to cycle through groups of three and merge the results somehow?
(groups of 4 would suit much better, but I am unsure how to add that function)

Or update the math to cope with eight points?

code as follows:
(note, converted but not really understood)

public static void trilaterate2(double[] P1, double[] P2, double[] P3, double[] L)

                   //define station points and distances
                        double x1 = P1[0];
                        double y1 = P1[1];
                        double z1 = P1[2];

                        double x2 = P2[0];
                        double y2 = P2[1];
                        double z2 = P2[2];

                        double x3 = P3[0];
                        double y3 = P3[1];
                        double z3 = P3[2];

                        double L1 = L[0];
                        double L2 = L[1];
                        double L3 = L[2];

                   //caluculate coords in plane of stations
                        double LB1 = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));
                        double LB2 = Math.Sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2) + (z3 - z2) * (z3 - z2));
                        double LB3 = Math.Sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3) + (z1 - z3) * (z1 - z3));

                    double X = (L1*L1  - L2*L2  + LB1*LB1)/(2*LB1 );
                    double C1 = Math.Sqrt (L1*L1 - X*X);
                    if (L1*L1 - X*X < 0){Console.Write("no solution");}
                    double XB = (LB3*LB3 - LB2*LB2 + LB1*LB1 )/(2*LB1 );
                    if (LB3*LB3 - XB* XB < 0 ){Console.Write("no solution");}
                    double CB=  Math.Sqrt(LB3*LB3 - XB* XB );
                    if (C1*C1+(XB - X)*(XB - X)< 0){Console.Write("no solution");}
                    double D1 = Math.Sqrt(C1*C1+(XB - X)*(XB - X));
                    double Y = (D1*D1 - L3*L3  + CB*CB  )/(2*CB );
                    if (C1*C1 - Y*Y < 0){Console.Write("no solution");}
                    double Z = Math.Sqrt(C1 * C1 - Y * Y);

                   //Now transform X,Y,Z to x,y,z
                    //Find the unit vectors in X,Y,Z
                    double Xx = (x2-x1);
                    double Xy = (y2-y1);
                    double Xz = (z2-z1);
                    double Xl = Math.Sqrt(Xx*Xx+Xy*Xy+Xz*Xz);
                    Xx = Xx / Xl;
                    Xy = Xy / Xl;
                    Xz = Xz / Xl;

                    double t =- ((x1-x3)*(x2-x1)+(y1-y3)*(y2-y1)+(z1-z3)*(z2-z1))/(LB1*LB1);
                    double Yx = (x1+(x2-x1)*t-x3);
                    double Yy = (y1+(y2-y1)*t-y3);
                    double Yz = (z1+(z2-z1)*t-z3);
                    double Yl = Math.Sqrt(Yx*Yx+Yy*Yy+Yz*Yz);
                    Yx =- (Yx/Yl);
                    Yy =- (Yy/Yl);
                    Yz =- (Yz/Yl);

                    double Zx = (Xy * Yz - Xz * Yy);
                    double Zy = (Xz * Yx - Xx * Yz);
                    double Zz = (Xx * Yy - Xy * Yx);
                    //document.write(' Zx='+Zx.toFixed(5)+' Zy='+Zy.toFixed(5)+' Zz='+Zz.toFixed(5)+'<br>')

                    double x = (x1 + X * Xx + Y * Yx + Z * Zx);
                    double y = (y1 + X * Xy + Y * Yy + Z * Zy);
                    double z = (z1 + X * Xz + Y * Yz + Z * Zz);

                    x = (x1 + X * Xx + Y * Yx - Z * Zx);
                    y = (y1 + X * Xy + Y * Yy - Z * Zy);
                    z = (z1 + X * Xz + Y * Yz - Z * Zz);

                    Console.Write(x + " " + y + " " + z);

Open in new window


public static double[] L = new double[3] { 1782, 1795, 1491 };
        public static double[] P1 = new double[3] { 1000, 1000, 0 };
        public static double[] P2 = new double[3] { 1000, 2000, 0 };
        public static double[] P3 = new double[3] { 2000, 1000, 0 };

        static void Main(string[] args)


Open in new window

There is no documentation of your used function anywhere.
Can you share at least its description (I am mostly concerned about point definition)
Also would be nice to share if geosphere is involved.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

David Johnson, CD, MVPOwnerCommented:
trilateration is the intersection of 3 circles, triangles or spheres.  So you need the x,y co-ordinates of the 3 points AND their radius
Makes little sense as 3 points look like dots to me in question...
chemicalx001Author Commented:
Hi Guys, thanks. these are the known points:

 double x1 = P1[0];
                        double y1 = P1[1];
                        double z1 = P1[2];

                        double x2 = P2[0];
                        double y2 = P2[1];
                        double z2 = P2[2];

                        double x3 = P3[0];
                        double y3 = P3[1];
                        double z3 = P3[2];

and these are the distances from the unknown. (radius of circle)

                        double L1 = L[0];
                        double L2 = L[1];
                        double L3 = L[2];

I know i should get my head around the equation, but i am in a hurry to get this project finished! It seems to be working when i run it on 3 known points, the problem is, i have more than 3. I am trialling cycling through groups of 3 and averaging the result. Is this the best way to go about it?

Thanks again.
You must use integers to keep accuracy of numbers. Double will never yield any solution as around 1000 the detail is 1e-12
Robberbaron (robr)Connect With a Mentor Commented:
but to raise David Johnsons reply again, for a triangulation you only need 3 points.  Any more does not help...

unless you expect a there to be an error in the distances and you want the 'average' answer,  which is what GPS does.... the more satellites (eg points and distances), the better the answer.

is that what you are trying to do ?  

a bit better than average probably
chemicalx001Author Commented:
thanks! My issue is that am getting different results depending on which 3 points I use. Not ideal. So I am averaging the results. Which is working, but very slowly! Way too much latency to be of any use really.

So really my problem is with the trilateration not giving reliable results.
So back to my original question....

Does anyone have a C# formula to trilaterate the location of an unknown point from three known points (xyz) and distance from each known point to the unknown point?

Meanwhile, I will look into centroid, thanks.
chemicalx001Author Commented:
Thanks guys! I am reading up on RMS and getting better results.  I also had an error in my code that was causing the lag, I was not clearing the list after i averaged it.  

Thanks again for your help.
Thanks for the points.  And one final note:

You more you know about the source or your errors, the better you can handle it.

In particular, the error function I showed earlier would be appropriate for errors in measuring the distance.

If the distance measurements were perfect, and the errors are in the coordinates of the base stations, you would not want to normalize with distance.

       RMS Error  =  sqrt[  (Da - Dax)²   +   (Db - Dbx)²   +   (Dc - Dcx)²   +   . . .    +   (Dn - Dnx)² ]
earth man2Commented:
You can use Excel to do this using solver functionality
 excel screenshotYou can extend the array of data points...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.