Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cartesian2polar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from math import acos, sqrt, pi

# Read values for x and y
x = float(input("x = "))
y = float(input("y = "))

# Convert polar coordinates to cartesian
r = sqrt(x**2 + y**2)
θ = acos(x/r)
θ *= 1 if y >= 0 else -1
Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The conversion from cartesian to polar coordinates is not correct when x is negative. The acos function returns a value between 0 and pi, so when x is negative, the angle should be 2*pi - acos(x/r) instead of -acos(x/r).

Suggested change
r = sqrt(x**2 + y**2)
θ = acos(x/r)
θ *= 1 if y >= 0 else -1
r = sqrt(x**2 + y**2)
if x >= 0:
θ = acos(x/r)
else:
θ = 2*pi - acos(x/r)


# Print results
print("r = " + str(r))
print("θ = " + str(θ))
Comment on lines +4 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: It's a good practice to encapsulate the conversion logic into a function. This will make the code more modular, reusable, and testable.

Suggested change
x = float(input("x = "))
y = float(input("y = "))
# Convert polar coordinates to cartesian
r = sqrt(x**2 + y**2)
θ = acos(x/r)
θ *= 1 if y >= 0 else -1
# Print results
print("r = " + str(r))
print("θ = " + str(θ))
def cartesian_to_polar(x, y):
r = sqrt(x**2 + y**2)
if x >= 0:
θ = acos(x/r)
else:
θ = 2*pi - acos(x/r)
return r, θ
x = float(input("x = "))
y = float(input("y = "))
r, θ = cartesian_to_polar(x, y)
print("r = " + str(r))
print("θ = " + str(θ))

13 changes: 13 additions & 0 deletions polar2cartesian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from math import cos, sin

# Read values for r and θ
r = float(input("r = "))
θ = float(input("θ = "))

# Convert polar coordinates to cartesian
x = r*cos(θ)
y = r*sin(θ)

# Print results
print("x = " + str(x))
print("y = " + str(y))
Comment on lines +4 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: It's a good practice to encapsulate the conversion logic into a function. This will make the code more modular, reusable, and testable.

Suggested change
r = float(input("r = "))
θ = float(input("θ = "))
# Convert polar coordinates to cartesian
x = r*cos(θ)
y = r*sin(θ)
# Print results
print("x = " + str(x))
print("y = " + str(y))
def polar_to_cartesian(r, θ):
x = r*cos(θ)
y = r*sin(θ)
return x, y
r = float(input("r = "))
θ = float(input("θ = "))
x, y = polar_to_cartesian(r, θ)
print("x = " + str(x))
print("y = " + str(y))