Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For my school assignment I need to create a function to find Euclidean distance using Cartesian points that are found using a different function.

I got the error
polar_to_cartesian() missing 1 required positional argument: 'R'


Any help is appreciated I feel so dumb and confused

What I have tried:

def polar_to_cartesian(latlon:tuple, R: float):
    """
    Description: Takes in a tuple 'latlon' (latitude and longitude, in radians) and the float 'R' (nonnegative radius of the sphere), returning the cartesian coordinates of the points.
    
    >>> polar_to_cartesian((99, 34), 300)
    (-10.137244625211233, 6.320561508619837, -299.7620502559061)
    
    >>> polar_to_cartesian((22, 56), 4000)
    (-3412.746736059186, 2086.122284215118, -35.4052371616155)
    
    """
    import math
    
    (lat, lon) = latlon
    
    x = R * math.cos(lat) * math.cos(lon)
    y = R * math.cos(lat) * math.sin(lon)
    z = R * math.sin(lat)
    
    return (x, y, z)

# Problem 5.
def distance_Euclidean(points:list, R: float):
    """
    Description: Takes in a list 'points' (2 latitudes, 2 longitudes, all in radians) and the float 'R' (nonnegative radius of the sphere), returning the Euclidean distance between the two points.
    
    >>> distance_Euclidean([(10, 60),(70, 80)], 2000)
    63.245553203367585
    
    >>> distance_Euclidean([(40, 700),(600, 70)], 1000)
    842.9116205154606
    
    """
    import math
    
    [(lat1, lon1), (lat2, lon2)] = points
    
    lat1 = lat1 * (math.pi/180)
    lon1 = lon1 * (math.pi/180)
    lat2 = lat2 * (math.pi/180)
    lon2 = lon2 * (math.pi/180)
    
    (x1, y1, z1) = polar_to_cartesian((points[0], R))
    (x2, y2, z2) = polar_to_cartesian((points[1], R))
    #return math.sqrt((lon2 - lon1)**2 + (lat2 - lat1)**2)
    
    return math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + + (z1 - z2)**2)
Posted
Updated 18-Sep-21 19:59pm

1 solution

Check your brackets:
Python
(x1, y1, z1) = polar_to_cartesian((points[0], R))
(x2, y2, z2) = polar_to_cartesian((points[1], R))
The second set encloses both parametres, making them a single tuple instead of two parameters:
Python
(x1, y1, z1) = polar_to_cartesian(points[0], R)
(x2, y2, z2) = polar_to_cartesian(points[1], R)
Should fix it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900