# How to calculate 2D Trilateration Step by Step

I am trying to code Trilateration but I am not familiar with the math behind it. even though there is an explanation on Wikipedia in this link https://en.wikipedia.org/wiki/Trilateration
and a almost better explanation in here https://stackoverflow.com/questions/9747227/2d-trilateration?answertab=active#tab-top I am still confused on how to do it

I have P1, P2 and P3 as my reference points, and X as the point to find
and I want the distance from P1 to X = 8, P2 to X = 10 and P3 to X = 11

P1 (10,10)

P2 (24,24)

P3 (23,19)

so I have to calculate the unit vector in the direction from P1 to P2, following the formula ex = (P2 - P1) / ‖P2 - P1‖ meaning

ex,x = (P2x - P1x) / sqrt((P2x - P1x)^2 + (P2y - P1y)^2)

ex,y = (P2y - P1y) / sqrt((P2x - P1x)^2 + (P2y - P1y)^2)

ex,x = (24 - 10) / sqrt((24 - 10)^2 + (24 - 10)^2) = 14/sqrt(14^2+14^2) = 14/sqrt(392) = 14/20 = 0.7

ex,y = (24 - 10) / sqrt((24 - 10)^2 + (24 - 10)^2) = 14/sqrt(14^2+14^2) = 14/sqrt(392) = 14/20 = 0.7

then calculating the signed magnitude of the x component of the vector from P1 to P3 by i = ex(P3 - P1)

ix = 0.7*(23-10) = 0.7*13 = 9.1

iy = 0.7*(19-10) = 0.7*9 = 6.3

now calculating the the unit vector in the y direction from the formula
ey = (P3 - P1 - i*ex) / ‖P3 - P1 - i*ex‖

**but here how do I continue?**

I am assuming to continue like this

ey,y = (P3y - P1y - iy*ex,y) / sqrt((P3x - P1x - ix*ex,x)^2+(P3y - P1y - iy*ex,y)^2)

ey,x = (P3x - P1x - ix*ex,x) / sqrt((P3x - P1x - ix*ex,x)^2+(P3y - P1y - iy*ex,y)^2)

ey,y = (19 - 10 - 6.3*0.7) / sqrt((23 - 10 - 9.1*0.7)^2+(19 - 10 - 6.3*0.7)^2)

ey,x = (23 - 10 - 9.1*0.7) / sqrt((23 - 10 - 9.1*0.7)^2+(19 - 10 - 6.3*0.7)^2)

ey,y = 0.6

ey,x = 0.8

then the distance between the centers P1 and P2 is ‖P2 - P1‖

d = ‖P2 - P1‖ = sqrt((P2x - P1x)^2 + (P2y - P1y)^2) = 20

then the signed magnitude of the y component from j = ey(P3 - P1)

jxx = 0.6*(23-10) = 0.6*13 = 7.8

jxy = 0.8*(23-10) = 0.8*13 = 10.4

jyx = 0.6*(19-10) = 0.6*9 = 5.4

jyy = 0.8*(19-10) = 0.8*9 = 7.2

then finally from https://stackoverflow.com/questions/9747227/2d-trilateration?answertab=active#tab-top

x = (r12 - r22 + d2) / 2d

y = (r12 - r32 + i2 + j2) / 2j - ix / j

x = (8^2 - 10^2 + d^2) / 2*d = (8^2 - 10^2 + 20^2) / (2*20) = 9.1

y = (8^2 - 11^2 + i^2 + j^2) / (2*j - (ix / j))

**What do I do????**

please help

## Answers and Replies

mathman
Science Advisor
I have no idea what this method involves. However there is a straightforward approach as follows, with X=(x,y)
$1)\ (x-10)^2+(y-10)^2=64$ (distance to P1 squared)
$2)\ (x-24)^2+(y-24)^2=100$ (distance to P2 squared)
$3)\ (x-23)^2+(y-19)^2=121$ (distance to P3 squared)
Combine equations: 1) minus 3) to get 4), 2) minus 3) to get 5). 4) and 5) are linear in x and y. Solve and check to see if they satisfy 1), 2), and 3).

nop it does not work in this way

P1 = x^2-20x+100+y^2-20y+100=64

P1 = x^2-20x+y^2-20y=-136

P2 = x^2-48x+576+y^2-48y+576=100

P2 = x^2-48x+y^2-48y=-1052

P3 = x^2-46x+529+y^2-38y+361=121

P3 = x^2-46x+y^2-38y=-769

P4=P1-P3

(x^2-20x+y^2-20y=-136)-( x^2-46x+y^2-38y=-769)

(x^2-20x+y^2-20y)-( x^2-46x+y^2-38y)=633

P4 = (26x+18y)=633

P5=P2-P3

(x^2-48x+y^2-48y=-1052)-( x^2-46x+y^2-38y=-769)

(x^2-48x+y^2-48y)-( x^2-46x+y^2-38y)=-283

(-2x-10y)=-283

P5 = (2x+10y)=283

Substitution

(26x+18y)=633 y=(633-26x)/18

(2x+10y)=283 (2x+10((633-26x)/18))=283

(2x+((6330-260x)/18))=283 36x+6330-260x=283*18

6330-224x=5094 1236=224x

x= 5.52
y=(633-26(5.52))/18= 27.2

replacing x and y on P1,2,3

x^2-20x+y^2-20y=-136
〖5.52〗^2-20*5.52+〖27.2〗^2-20*27.2=315.91
wrong

x^2-48x+y^2-48y=-1052
〖5.52〗^2-48*5.52+〖27.2〗^2-48*27.2=351.75
wrong

x^2-46x+y^2-38y=-769
〖5.52〗^2-46*5.52+〖27.2〗^2-38*27.2=372.79
wrong

mfb
Mentor
(x^2-20x+y^2-20y=-136)-( x^2-46x+y^2-38y=-769)
That is a really strange way of writing it.

The approach is right, if your result is wrong you did a mistake somewhere in between.

No, the calculation is right, x and y works with p4 and p5 but not with p1,2,3, it will never work I have checked online, this does not count, at the end I need trilateration not this

mfb
Mentor
I checked the equations, your initial equations don't have a solution to start with.
Here are p1 and p2. The problem is easy to track down:
and I want the distance from P1 to X = 8, P2 to X = 10 and P3 to X = 11

P1 (10,10)

P2 (24,24)
P1 and P2 have a distance of 14*sqrt(2) > 18. There is no such point.

OMG, I do not have ecuations, I have coordinates in an x y Cartesian map, p1 is located in 10,10

mfb
Mentor
Yes, and P2 is located at (24,24), that is 19.8 away from P1. There is no point that is 8 away from P1 and 10 away from P2, that would violate the triangle inequality.

mathman
Science Advisor
Using graph paper and a compass, one can easily see if there is a solution. Place each point on the paper and use it a center of a circle with radius equal to the distance to the unknown point. If the three circles have a common point of intersection that is the solution. Otherwise (as in this case) there is none.