Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Spherical > Flat > Nested solution in Clear category for Earth Distances by veky
import math, re
def sincos(coor:str) -> (float, float):
"Sine and cosine of a planet coordinate."
d, m, s = map(int, re.split("\D", coor[:-2]))
γ = math.radians(d + m/60 + s/60**2)
if coor.endswith(("E", "S")): γ = -γ
return math.sin(γ), math.cos(γ)
def rect(coors:str) -> (float, float, float):
"Convert planet to Cartesian coordinates (unit sphere)."
(z, k), (y, x) = map(sincos, re.split("[, ]+", coors))
return x*k, y*k, z
def distance(P1:str, P2:str, *, radius:float=6371.) -> float:
"Distance between two points on the planet surface, in km."
scalar_product = sum(map(float.__mul__, rect(P1), rect(P2)))
return radius * math.acos(scalar_product)
Dec. 5, 2014
Comments: