Polar Coordinates
Instead of saying "3 right and 4 up", you can say "5 away, in the direction 53° from east". That second description is polar coordinates: a distance and an angle . They make circles, spirals and anything that rotates easy to describe. This lesson converts between polar and Cartesian coordinates, graphs polar curves, and shows polar thinking in radar screens, circular menus and joystick input.
and
A point in polar coordinates is :
- is the distance from the origin (the pole);
- is the angle from the positive -axis, counter-clockwise positive, usually in radians.
From the right triangle with hypotenuse :
Note: Use
atan2(y, x), notatan(y/x). The plain arctangent cannot tell from (both have ) and fails when .atan2looks at the signs of both coordinates and returns the correct angle in . See atan2.
Polar coordinates are not unique: , and are all the same point. A negative means "go backwards", in the direction .
import math
def to_polar(x, y):
return math.hypot(x, y), math.atan2(y, x)
def to_cartesian(r, theta):
return r * math.cos(theta), r * math.sin(theta)
for x, y in [(3, 4), (-1, 1), (-2, -2), (0, -5)]:
r, th = to_polar(x, y)
back = to_cartesian(r, th)
print(f"({x:>2}, {y:>2}) -> r = {r:.4f}, θ = {th:+.4f} rad = {math.degrees(th):+7.2f}° -> ({back[0]:.4f}, {back[1]:.4f})")( 3, 4) -> r = 5.0000, θ = +0.9273 rad = +53.13° -> (3.0000, 4.0000)
(-1, 1) -> r = 1.4142, θ = +2.3562 rad = +135.00° -> (-1.0000, 1.0000)
(-2, -2) -> r = 2.8284, θ = -2.3562 rad = -135.00° -> (-2.0000, -2.0000)
( 0, -5) -> r = 5.0000, θ = -1.5708 rad = -90.00° -> (0.0000, -5.0000)
Polar curves
An equation describes a curve: for each direction , go out a distance . Some curves are much simpler in polar form than in and :
| Polar equation | Curve |
|---|---|
| circle of radius 3 around the origin | |
| straight line through the origin at 45° | |
| circle through the origin, centre | |
| Archimedean spiral (equal gaps between turns) | |
| cardioid (heart shape) | |
| three-petal rose |
To convert , multiply by : , which is , then complete the square: .
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2 * np.pi, 600)
fig, ax = plt.subplots(figsize=(5, 5), subplot_kw={"projection": "polar"})
ax.plot(theta, 1 + np.cos(theta), label="r = 1 + cos θ")
ax.plot(theta, np.abs(np.cos(3 * theta)) * 1.5, label="r = 1.5|cos 3θ|")
ax.plot(theta, theta / np.pi, label="r = θ/π")
ax.legend(loc="lower left", fontsize=8)
print("polar axes with a cardioid, a rose and a spiral")polar axes with a cardioid, a rose and a spiral
In software: radar, menus and sticks
Radar and sensors. A radar, lidar or sonar measures exactly (from the echo time) and (from where the antenna points). Drawing the blips on a screen is the polar → Cartesian conversion, plus a shift to the screen centre and a flip of because screen grows downwards.
Circular (radial) menus. A pie menu with slices places item at angle , and decides which slice the mouse is in by converting the mouse position to polar around the menu centre:
import math
items = ["Attack", "Defend", "Item", "Magic", "Run", "Talk"]
cx, cy, radius = 200, 200, 80 # menu centre on screen, item ring radius
n = len(items)
print("item positions (screen y grows downward):")
for k, name in enumerate(items):
ang = 2 * math.pi * k / n
x, y = cx + radius * math.cos(ang), cy - radius * math.sin(ang)
print(f" {name:<6} at ({x:6.1f}, {y:6.1f})")
def pick(mx, my):
dx, dy = mx - cx, cy - my # flip y back to math orientation
if math.hypot(dx, dy) < 20:
return None # dead zone in the centre
ang = math.atan2(dy, dx) % (2 * math.pi)
k = round(ang / (2 * math.pi / n)) % n
return items[k]
for mouse in [(290, 205), (160, 130), (205, 195), (215, 300)]:
print("mouse", mouse, "->", pick(*mouse))item positions (screen y grows downward):
Attack at ( 280.0, 200.0)
Defend at ( 240.0, 130.7)
Item at ( 160.0, 130.7)
Magic at ( 120.0, 200.0)
Run at ( 160.0, 269.3)
Talk at ( 240.0, 269.3)
mouse (290, 205) -> Attack
mouse (160, 130) -> Item
mouse (205, 195) -> None
mouse (215, 300) -> Talk
Rounding the angle to the nearest multiple of picks the nearest slice; the % (2π) turns atan2's range into . The dead zone ignores tiny movements near the centre, the same trick used for analog sticks, whose reading is converted to polar so that the dead zone is a circle and the direction is an angle.
Other polar habits: rotating something is just adding to , orbit cameras store (distance, angle), and image processing uses polar transforms to detect circles. Calculus in polar coordinates (areas, arc lengths) is in Polar Coordinates and Polar Areas and Lengths.
Exercises
Exercise 1: Polar to Cartesian
Convert to Cartesian coordinates. Give exact values.
Answer
Solution
Exercise 2: Cartesian to polar
Convert to polar with in .
Answer
Solution
Exercise 3: Convert an equation
Write in Cartesian form and describe the curve.
Answer
: a circle with centre and radius 3.
Solution
Exercise 4: Radar blips
A radar at screen position shows a target at range 150 px and bearing 30° (measured counter-clockwise from east). Where is the blip on screen, with screen growing downward?
Answer
About .
Solution
import math
r, th = 150, math.radians(30)
print(round(400 + r * math.cos(th), 1), round(300 - r * math.sin(th), 1))529.9 225.0
Test yourself
What are the Cartesian coordinates of ?
Why use atan2(y, x) instead of atan(y / x)?
- It is faster
- It returns degrees
- It uses both signs to get the correct quadrant and handles
- It always returns a positive angle
Which polar equation is a circle of radius 4 centred at the origin?