Reputation: 31
enter image description hereCoordinates of three points are (-2.5466649, -1.2534076, 0.0001741), (-2.6229969, 1.6419994, 0.0000651) and (-2.6972299, 2.8495214, 0.0003421). Would like to find a point at distance of 1.01, 3.91 and 5.12, respectively.
import numpy as np
x1=-2.5466649
y1=-1.2534076
z1= 0.0001741
x2=-2.6229969
y2= 1.6419994
z2= 0.0000651
x3=-2.6972299
y3= 2.8495214
z3= 0.0003421
#distance
l1=1.01
l2=3.91
l3=5.12
#coefficient
a1=2*(x2-x1)
a2=2*(x3-x2)
a3=2*(x1-x3)
b1=2*(y2-y1)
b2=2*(y3-y2)
b3=2*(y1-y3)
c1=2*(z2-z1)
c2=2*(z3-z2)
c3=2*(z1-z3)
d1 = l1**2 - l2**2 - x1**2 - y1**2 - z1**2 + x2**2 + y2**2 + z2**2
d2 = l2**2 - l3**2 - x2**2 - y2**2 - z2**2 + x3**2 + y3**2 + z3**2
d3 = l3**2 - l1**2 - x3**2 - y3**2 - z3**2 + x1**2 + y1**2 + z1**2
a = np.array([[a1, b1, c1], [a2, b2, c2], [a3, b3, c3]])
b = np.array([d1, d2, d3])
x = np.linalg.solve(a, b)
print x # [-5.00886518e+02 -1.78735572e+01 -6.55360000e+04]
Upvotes: 2
Views: 758
Reputation: 161
I think from your graphical depiction, you are referring to trilateration. More info on that here
Also I was able to find this which has some code in one of its answers (link to answer) given by Andrew. Sharing the same code here for your question.
import numpy
from numpy import sqrt, dot, cross
from numpy.linalg import norm
# Find the intersection of three spheres
# P1,P2,P3 are the centers, r1,r2,r3 are the radii
# Implementaton based on Wikipedia Trilateration article.
def trilaterate(P1,P2,P3,r1,r2,r3):
temp1 = P2-P1
e_x = temp1/norm(temp1)
temp2 = P3-P1
i = dot(e_x,temp2)
temp3 = temp2 - i*e_x
e_y = temp3/norm(temp3)
e_z = cross(e_x,e_y)
d = norm(P2-P1)
j = dot(e_y,temp2)
x = (r1*r1 - r2*r2 + d*d) / (2*d)
y = (r1*r1 - r3*r3 -2*i*x + i*i + j*j) / (2*j)
temp4 = r1*r1 - x*x - y*y
if temp4<0:
raise Exception("The three spheres do not intersect!");
z = sqrt(temp4)
p_12_a = P1 + x*e_x + y*e_y + z*e_z
p_12_b = P1 + x*e_x + y*e_y - z*e_z
return p_12_a,p_12_b
P1=numpy.array([-2.5466649, -1.2534076, 0.0001741])
P2=numpy.array([-2.6229969, 1.6419994, 0.0000651])
P3=numpy.array([-2.6972299, 2.8495214, 0.0003421])
r1=1.01
r2=3.91
r3=5.12
print(trilaterate(P1,P2,P3,r1,r2,r3))
Though since this code gives this error
raise Exception("The three spheres do not intersect!");
Exception: The three spheres do not intersect!
I think there is an issue with your distances which would lead to there being no common point of intersection.
Edit : Editing the code as there was no imports
Upvotes: 3