Skip to content
DeepForgeby svx
All problems
MediumCalculusca-262

Geodesic Distance on a Sphere

Problem

The geodesic or great-circle distance between two points on a sphere of radius R with unit position vectors u and v is R * arccos(u dot v).

Given the two unit vectors as lists and the radius, return the distance. Clamp the dot product to the interval [-1, 1] to guard against rounding.

Hint

The central angle is the arccosine of the dot product of unit vectors.

Test cases

case 1
in: [[1,0,0],[0,1,0],1]
expected: 1.5707963267948966
case 2
in: [[1,0,0],[1,0,0],5]
expected: 0
case 3
in: [[1,0,0],[-1,0,0],2]
expected: 6.283185307179586
case 4
in: [[0.6,0.8,0],[1,0,0],2]
expected: 1.8545904360032246
Starter codePython
def geodesic_sphere_distance(u, v, radius):
    # Your code here
    pass

4 test cases · run your code against them in the editor