TExcelTAD

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 rr and an angle θ\theta. 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.

rr and θ\theta

A point in polar coordinates is (r,θ)(r, \theta):

  • rr is the distance from the origin (the pole);
  • θ\theta is the angle from the positive xx-axis, counter-clockwise positive, usually in radians.

From the right triangle with hypotenuse rr:

polar→Cartesian:x=rcos⁡θy=rsin⁡θCartesian→polar:r=x2+y2θ=atan2⁡(y,x)\begin{aligned} &\text{polar} \to \text{Cartesian:} && x = r\cos\theta && y = r\sin\theta \\ &\text{Cartesian} \to \text{polar:} && r = \sqrt{x^2 + y^2} && \theta = \operatorname{atan2}(y, x) \end{aligned}

Note: Use atan2(y, x), not atan(y/x). The plain arctangent cannot tell (1,1)(1, 1) from (−1,−1)(-1, -1) (both have y/x=1y/x = 1) and fails when x=0x = 0. atan2 looks at the signs of both coordinates and returns the correct angle in (−π,π](-\pi, \pi]. See atan2.

Polar coordinates are not unique: (2,π/6)(2, \pi/6), (2,π/6+2π)(2, \pi/6 + 2\pi) and (−2,7π/6)(-2, 7\pi/6) are all the same point. A negative rr means "go backwards", in the direction θ+π\theta + \pi.

python · try it yourself
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 r=f(θ)r = f(\theta) describes a curve: for each direction θ\theta, go out a distance f(θ)f(\theta). Some curves are much simpler in polar form than in xx and yy:

Polar equation Curve
r=3r = 3 circle of radius 3 around the origin
θ=π/4\theta = \pi/4 straight line through the origin at 45°
r=4cos⁡θr = 4\cos\theta circle through the origin, centre (2,0)(2, 0)
r=θr = \theta Archimedean spiral (equal gaps between turns)
r=1+cos⁡θr = 1 + \cos\theta cardioid (heart shape)
r=cos⁡(3θ)r = \cos(3\theta) three-petal rose

To convert r=4cos⁡θr = 4\cos\theta, multiply by rr: r2=4rcos⁡θr^2 = 4r\cos\theta, which is x2+y2=4xx^2 + y^2 = 4x, then complete the square: (x−2)2+y2=4(x - 2)^2 + y^2 = 4.

python · try it yourself
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 rr (from the echo time) and θ\theta (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 yy because screen yy grows downwards.

Circular (radial) menus. A pie menu with nn slices places item kk at angle 2πk/n2\pi k/n, and decides which slice the mouse is in by converting the mouse position to polar around the menu centre:

python · try it yourself
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 2π/n2\pi/n picks the nearest slice; the % (2π) turns atan2's (−π,π](-\pi, \pi] range into [0,2π)[0, 2\pi). The dead zone ignores tiny movements near the centre, the same trick used for analog sticks, whose (x,y)(x, y) 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 θ\theta, 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 (6,2π/3)(6, 2\pi/3) to Cartesian coordinates. Give exact values.

Answer

(−3,33)≈(−3,5.196)(-3, 3\sqrt{3}) \approx (-3, 5.196)

Solution
x=6cos⁡(2π/3)=6⋅(−1/2)=−3y=6sin⁡(2π/3)=6⋅(3/2)=33\begin{aligned} x &= 6\cos(2\pi/3) = 6 \cdot (-1/2) = -3 \\ y &= 6\sin(2\pi/3) = 6 \cdot (\sqrt{3}/2) = 3\sqrt{3} \end{aligned}

Exercise 2: Cartesian to polar

Convert (−5,−5)(-5, -5) to polar with θ\theta in (−π,π](-\pi, \pi].

Answer

(52,−3π/4)≈(7.071,−2.356)(5\sqrt{2}, -3\pi/4) \approx (7.071, -2.356)

Solution
r=25+25=52The point is in the third quadrant, at 45∘ past the negative x-axis: θ=−3π/4(arctan⁡(y/x)=arctan⁡(1)=π/4 would wrongly put it in the first quadrant.)\begin{aligned} &r = \sqrt{25 + 25} = 5\sqrt{2} \\ &\text{The point is in the third quadrant, at } 45^\circ \text{ past the negative } x\text{-axis: } \theta = -3\pi/4 \\ &\text{(} \arctan(y/x) = \arctan(1) = \pi/4 \text{ would wrongly put it in the first quadrant.)} \end{aligned}

Exercise 3: Convert an equation

Write r=6sin⁡θr = 6\sin\theta in Cartesian form and describe the curve.

Answer

x2+(y−3)2=9x^2 + (y - 3)^2 = 9: a circle with centre (0,3)(0, 3) and radius 3.

Solution
r2=6rsin⁡θ  ⇒  x2+y2=6y  ⇒  x2+y2−6y+9=9  ⇒  x2+(y−3)2=9r^2 = 6r\sin\theta \;\Rightarrow\; x^2 + y^2 = 6y \;\Rightarrow\; x^2 + y^2 - 6y + 9 = 9 \;\Rightarrow\; x^2 + (y - 3)^2 = 9

Exercise 4: Radar blips

A radar at screen position (400,300)(400, 300) shows a target at range 150 px and bearing 30° (measured counter-clockwise from east). Where is the blip on screen, with screen yy growing downward?

Answer

About (529.9,225.0)(529.9, 225.0).

Solution
x=400+150cos⁡30∘=400+129.9=529.9y=300−150sin⁡30∘=300−75=225\begin{aligned} x &= 400 + 150\cos 30^\circ = 400 + 129.9 = 529.9 \\ y &= 300 - 150\sin 30^\circ = 300 - 75 = 225 \end{aligned}
python · try it yourself
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 (r,θ)=(2,π/2)(r, \theta) = (2, \pi/2)?

  1. (2,0)(2, 0)
  2. (0,2)(0, 2)
  3. (0,−2)(0, -2)
  4. (2,2)(\sqrt{2}, \sqrt{2})

Why use atan2(y, x) instead of atan(y / x)?

  1. It is faster
  2. It returns degrees
  3. It uses both signs to get the correct quadrant and handles x=0x = 0
  4. It always returns a positive angle

Which polar equation is a circle of radius 4 centred at the origin?

  1. θ=4\theta = 4
  2. r=4θr = 4\theta
  3. r=4cos⁡θr = 4\cos\theta
  4. r=4r = 4