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!

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

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.

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

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?

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.

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?

>> 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.

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

0

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.

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

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

(http://www.experts-exchange.com/Other/Math_Science/Q_21253179.html)

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)

Open in new window

called:

Open in new window