1

# How to convert cartesian coordinates to polar coordinates

jgonzalez9918
2086
1. First you need to get the input coordinate from the user and check that it is a number so, after you get the input as an STR convert it to a FLOAT type in this process you can check if it is a number as it follows:
``````whileTrue:
try:
x = float(input("Enter the X axis coordonate =>"))
print(type(x))
break
except ValueError:

whileTrue:
try:
y = float(input("Enter the Y axis coordonate =>"))
print(type(y))
break
except ValueError:

cartesian = [x, y]

print('The input coordinate is:')
print('=>', cartesian)

``````

note that the function WHILE allows you to enter the code into a cycle that checks the input until it is a variable that can be converted to a float, in this way you can work with negative numbers.

1. Make the mathematical conversion using the Pitagoras theorem and trigonometric functions.

First, you have to import the math package, it’ll allow you to use the trigonometric functions, be aware that these functions work with radiants values, in this case, the code is written to print the angle in grades.

``````import math

size = (x**2 + y**2)**0.5
angle = math.atan(y / x) * 180 / math.pi
polar = [size, angle]

``````
1. After the conversion is done it is necessary to analyze the angle to show taking into account the quadrant where is located the cartesian coordinate.
``````if x > 0and y > 0:
print('The coordonate isin the first quarter')
result = f'{size}, {angle}°'
print(result)
elif x < 0and y > 0:
print('The coordonate isin the second quarter')
result = f'{size}, {180-angle*-1}°'
print(result)
elif x < 0and y < 0:
print('The coordonate isin the third quarter')
result = f'{size}, {180+angle}°'
print(result)
elif x > 0and y < 0:
print('The coordonate isin the forth quarter')
result = f'{size}, {360-angle*-1}°'
print(result)

``````

thanks to those who take the time to see my post… keep learning

Escribe tu comentario
+ 2