Reputation: 41
I have 3 latitudes and longitude:
x : 1.000000000 1.000000000
y : 2.000000000 2.000000000
z : 3.000000000 3.000000000
I need to calculate the smallest distance between Z and the line formed by X -> Y, PLEASE HELP
PHP code that solve my problem until now:
function calc ($a, $ay, $b, $by,$c, $cy) {
$a = array($a, $ay, 0);// i use 0 altitude always
$b = array($b, $by, 0);
$c = array($c, $cy, 0);
$ab = array(
(($a[1] * $b[2]) - ($b[1] * $a[2])),
(($a[2] * $b[0]) - ($b[2] * $a[0])),
(($a[0] * $b[1]) - ($b[0] * $a[1]))
);
$normal = pow(pow($ab[0],2)+pow($ab[1],2)+pow($ab[2],2),0.5);
$d = array(
($ab[0]/$normal),
($ab[1]/$normal),
($ab[2]/$normal)
);
$e_ = (($d[0] * $c[0]) + ($d[1] * $c[1]) + ($d[2] * $c[2]));
$e = acos($e_);
$res = pi()/2 - $e;
$res = $res * 6378.1;
return $res;
}
Upvotes: 1
Views: 82
Reputation: 7824
we need to do some spherical geometry here. If we were just working in the plane the question would be easy https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line things are trickier on a sphere.
First what do we mean by the line formed by X -> Y. We probably mean a great circle https://en.wikipedia.org/wiki/Great_circle for example the equator.
Assume for a moment that the points X, Y were on the equator, then the distance between Z and the equator would be proportional to the latitude of Z.
One way to solve the problem would be to rotate our points so that X and Y lie on the equator and then find the latitude of Z. A procedure for doing this is (set up axis so x-axis is out through 0,0, the y-axis is due east and the z-axis due north)
We now have both X3 and Y3 on the equator and can find the latitude of Z3. The actual distance will be radius-of-earth * latitude-of-Z3-in-radians
Curiously @abiessu comment is not actually correct the great circle through 1N 1E and 2N 2E does not quite run through 3N 3E. I make the distance 14cm.
An easier way to calculate this is to find the cross product of X and Y, W=X ^ Y. This vector is the normal to the plane through X, Y and the center of the sphere. Now find the dot product W . Z this is angle-between-W-and-Z * len(W) * len(Z). So divide the dot product by the two lengths to give a and find pi/2-a the angle of inclination of Z above the plane. Multiply this by the radius to get the distance.
Taking the example of points at 1N 1E take points on the unit sphere
a 1.0N 1.0E (0.999695, 0.017450, 0.017452 )
b 2.0N 2.0E (0.998782, 0.034878, 0.034899 )
c 3.0N 3.0E (0.997261, 0.052264, 0.052336 )
a^b (0.000000, -0.017458, 0.017439 )
d=unit(a^b) (0.000011 , -0.707484, 0.706730 )
d . c 0.000022543731833669922
e = acos(d . c) = 1.570773783063061 in radians
pi/2 - e = 0.000022543731835522607
0.14378617602014673km distance on earth with radius 6378.1km
Upvotes: 2