-
Notifications
You must be signed in to change notification settings - Fork 457
Coordinate systems conversion #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Print results | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print("r = " + str(r)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print("θ = " + str(θ)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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).