Search code examples
mathequationequation-solving

Triangulating coordinates with an equation


Ok, I know this sounds really daft to be asking here, but it is programming related.

I'm working on a game, and I'm thinking of implementing a system that allows users to triangulate their 3D coordinates to locate something (eg for a task). I also want to be able to let the user make the coordinates of the points they are using for triangulation have user-determined coordinates (so the location's coordinate is relative, probably by setting up a beacon or something).

I have a method in place for calculating the distance between the points, so essentially I can calculate the lengths of the sides of the triangle/pyramid as well as all but the coordinate I am after. It has been a long time since I have done any trigonometry and I am rusty with the sin, cos and tan functions, I have a feeling they are required but have no clue how to implement them.

Can anyone give me a demonstration as to how I would go about doing this in a mathematical/programatical way?

extra info: My function returns the exact distance between the two points, so say you set two points to 0,0,0 and 4,4,0 respectively, and those points are set to scale(the game world is divided into a very large 3d grid, with each 'block' area being represented by a 3d coordinate) then it would give back a value at around 5.6.

The key point about it varying is that the user can set the points, so say they set a point to read 0,0,0, the actual location could be something like 52, 85, 93. However, providing they then count the blocks and set their other points correctly (eg, set a point 4,4,0 at the real point 56, 89, 93) then the final result will return the relative position (eg the object they are trying to locate is at real point 152, 185, 93, it will return the relative value 100,100,0). I need to be able to calculate it knowing every point but the one it's trying to locate, as well as the distances between all points.

Also, please don't ask why I can't just calculate it by using the real coordinates, I'm hoping to show the equation up on screen as it calculates the result.7

Example: Here is a diagram Example Diagram

Imagine these are points in my game on a flat plain. I want to know the point f. I know the values of points d and e, and the sides A,B and C.

Using only the data I know, I need to find out how to do this.

Answered Edit:

After many days of working on this, Sean Kenny has provided me with his time, patience and intellect, and thus I have now got a working implementation of a triangulation method.

I hope to place the different language equivalents of the code as I test them so that future coders may use this code and not have the same problem I have had.


Solution

  • I spent a bit of time working on a solution but I think the implementer, i.e you, should know what it's doing, so any errors encountered can be tackled later on. As such, I'll give my answer in the form of strong hints.

    First off, we have a vector from d to e which we can work out: if we consider the coordinates as position vectors rather than absolute coordinates, how can we determine what the vector pointing from d to e is? Think about how you would determine the displacement you had moved if you only knew where you started and where you ended up? Displacement is a straight line, point A to B, no deviation, not: I had to walk around that house so I walked further. A straight line. If you started at the point (0,0) it would be easy.

    Secondly, the cosine rule. Do you know what it is? If not, read up on it. How can we rearrange the form given in the link to find the angle d between vectors DE and DF? Remember you need the angle, not a function of the angle (cos is a function remember).

    Next we can use a vector 'trick' called the scalar product. Notice there is a cos function in there. Now, you may be thinking, we've just found the angle, why are we doing it again?

    Define DQ = [1,0]. DQ is a vector of length 1, a unit vector, along the x-axis. Which other vector do we know? Do we know of two position vectors?

    Once we have two vectors (I hope you worked out the other one) we can use the scalar product to find the angle; again, just the angle, not a function of it.

    Now, hopefully, we have 2 angles. Could we take one from the other to get yet another angle to our desired coordinate DF? The choice of using a unit vector earlier was not arbitrary.

    The scalar product, after some cancelling, gives us this : cos(theta) = x / r Where x is the x ordinate for F and r is the length of side A.

    The end result being:

    theta = arccos( xe / B ) - arccos( ( (A^2) + (B^2) - (C^2) ) / ( 2*A*B ) ) 
    

    Where theta is the angle formed between a unit vector along the line y = 0 where the origin is at point d.

    With this information we can find the x and y coordinates of point f relative to d. How? Again, with the scalar product. The rest is fairly easy, so I'll give it to you.

    x = r.cos(theta)
    
    y = r.sin(theta)
    

    From basic trigonometry.

    I wouldn't advise trying to code this into one value.

    Instead, try this:

    //pseudo code
    dx = 0
    dy = 0  //initialise coordinates somehow
    ex = ex
    ey = ey
    A = A
    B = B
    C = C
    
    cosd = ex / B
    cosfi = ((A^2) + (B^2) - (C^2)) / ( 2*A*B)
    d = acos(cosd)      //acos is a method in java.math 
    fi = acos(cosfi)   //you will have to find an equivalent in your chosen language
                      //look for a method of inverse cos
    theta = fi - d
    
    x = A cos(theta)
    y = A sin(theta)
    

    Initialise all variables as those which can take decimals. e.g float or double in Java.

    Imgur

    The green along the x-axis represents the x ordinate of f, and the purple the y ordinate.
    The blue angle is the one we are trying to find because, hopefully you can see, we can then use simple trig to work out x and y, given that we know the length of the hypotenuse. This yellow line up to 1 is the unit vector for which scalar products are taken, this runs along the x-axis.

    We need to find the black and red angles so we can deduce the blue angle by simple subtraction.

    Hope this helps. Extensions can be made to 3D, all the vector functions work basically the same for 3D.