TExcelTAD

Circles

A circle is the set of all points at the same distance, the radius, from a centre point. That one-line definition gives the formulas for circumference and area, the equation of a circle in coordinates, and the cheapest collision test in game programming.

Parts of a circle

Part Meaning
radius rr centre to any point on the circle
diameter d=2rd = 2r a chord through the centre
chord a segment joining two points on the circle
arc a piece of the circle between two points
sector a "pizza slice" bounded by two radii and an arc
segment the region between a chord and its arc
tangent a line touching the circle at exactly one point
secant a line crossing the circle at two points
central angle an angle with its vertex at the centre
inscribed angle an angle with its vertex on the circle

Measuring a circle

circumferenceC=2πr=πdareaA=πr2arc lengths=(θ/360∘)⋅2πr(θ=central angle in degrees)sector areaA=(θ/360∘)⋅πr2chord lengthc=2r⋅sin⁡(θ/2)\begin{aligned} \text{circumference} &\quad C = 2\pi r = \pi d \\ \text{area} &\quad A = \pi r^2 \\ \text{arc length} &\quad s = (\theta / 360^\circ) \cdot 2\pi r && (\theta = \text{central angle in degrees}) \\ \text{sector area} &\quad A = (\theta / 360^\circ) \cdot \pi r^2 \\ \text{chord length} &\quad c = 2r \cdot \sin(\theta / 2) \end{aligned}

π≈3.14159\pi \approx 3.14159 is the ratio of any circle's circumference to its diameter. An arc or sector is just the fraction θ/360∘\theta/360^\circ of the whole circle. In radians these become s=rθs = r\theta and A=12r2θA = \frac{1}{2}r^2\theta, which is one reason radians are preferred later (Angles and Radians).

python · try it yourself
import math

r, theta = 6, 50                      # radius 6, central angle 50°
frac = theta / 360
print("circumference:", round(2 * math.pi * r, 4))
print("area:         ", round(math.pi * r**2, 4))
print("arc length:   ", round(frac * 2 * math.pi * r, 4), "= 5π/3 ≈", round(5 * math.pi / 3, 4))
print("sector area:  ", round(frac * math.pi * r**2, 4), "= 5π ≈", round(5 * math.pi, 4))
print("chord length: ", round(2 * r * math.sin(math.radians(theta / 2)), 4))
circumference: 37.6991
area:          113.0973
arc length:    5.236 = 5π/3 ≈ 5.236
sector area:   15.708 = 5π ≈ 15.708
chord length:  5.0714

The chord (5.07) is a bit shorter than its arc (5.24), as it must be: a straight line is the shortest path.

Angle facts

  • Inscribed angle theorem: an inscribed angle is half the central angle that cuts off the same arc. So all inscribed angles on the same arc are equal.
  • Thales' theorem: an angle inscribed in a semicircle is a right angle (the central angle is 180°, half of it is 90°).
  • Tangent-radius: a tangent is perpendicular to the radius at the point of contact.
  • Two tangents drawn from an outside point have equal length.

Thales' theorem gets a full two-column proof in Geometric Proofs.

The equation of a circle

A point (x,y)(x, y) is on the circle with centre (h,k)(h, k) and radius rr exactly when its distance from the centre is rr. Squaring the distance formula gives:

(x−h)2+(y−k)2=r2(x - h)^2 + (y - k)^2 = r^2

Expanded, it looks like x2+y2+Dx+Ey+F=0x^2 + y^2 + Dx + Ey + F = 0. To recover centre and radius, complete the square (Completing the Square): the centre is (−D/2,−E/2)(-D/2, -E/2) and r2=D2/4+E2/4−Fr^2 = D^2/4 + E^2/4 - F.

python · try it yourself
import math

D, E, F = -6, 4, -12                  # x² + y² − 6x + 4y − 12 = 0
h, k = -D / 2, -E / 2
r = math.sqrt(D**2 / 4 + E**2 / 4 - F)
print("centre:", (h, k), " radius:", r)

for x, y in [(6, 2), (8, -2), (0, 0), (9, 1)]:
    d2 = (x - h)**2 + (y - k)**2
    where = "on" if d2 == r * r else "inside" if d2 < r * r else "outside"
    print((x, y), "distance² =", d2, "→", where)
centre: (3.0, -2.0)  radius: 5.0
(6, 2) distance² = 25.0 → on
(8, -2) distance² = 25.0 → on
(0, 0) distance² = 13.0 → inside
(9, 1) distance² = 45.0 → outside

If D2/4+E2/4−FD^2/4 + E^2/4 - F comes out negative, the equation describes no points at all.

In software: bounding circles and collisions

Two circles overlap when the distance between centres is less than the sum of the radii. Comparing squared distances avoids the square root, which is why game code looks like this:

python · try it yourself
def circles_hit(c1, r1, c2, r2):
    dx, dy = c2[0] - c1[0], c2[1] - c1[1]
    return dx * dx + dy * dy < (r1 + r2) ** 2

def bounding_circle(points):
    xs, ys = [p[0] for p in points], [p[1] for p in points]
    cx, cy = (min(xs) + max(xs)) / 2, (min(ys) + max(ys)) / 2        # centre of the bounding box
    r = max(((x - cx) ** 2 + (y - cy) ** 2) ** 0.5 for x, y in points)
    return (cx, cy), r

sprite = [(0, 0), (4, 0), (4, 2), (1, 3)]
centre, radius = bounding_circle(sprite)
print("bounding circle:", centre, radius)
print("hits bullet at (4, 3), r 0.5?", circles_hit(centre, radius, (4, 3), 0.5))
print("hits bullet at (7, 3), r 0.5?", circles_hit(centre, radius, (7, 3), 0.5))
bounding circle: (2.0, 1.5) 2.5
hits bullet at (4, 3), r 0.5? True
hits bullet at (7, 3), r 0.5? False

A bounding circle is a rotation-proof broad-phase filter: unlike a box, it stays valid however the sprite turns. This simple one is centred on the box centre, so it is not always the smallest possible circle; the smallest is found by Welzl's algorithm. See Circles and Spheres.

Exercises

Exercise 1: From area to circumference

A circle has area 49π cm249\pi\ \text{cm}^2. Find its radius and circumference.

Answer

r=7r = 7 cm, C=14π≈43.98C = 14\pi \approx 43.98 cm.

Solution
πr2=49π⇒r2=49⇒r=7C=2π⋅7=14π≈43.98\begin{aligned} &\pi r^2 = 49\pi \Rightarrow r^2 = 49 \Rightarrow r = 7 \\ &C = 2\pi \cdot 7 = 14\pi \approx 43.98 \end{aligned}

Exercise 2: Arc and sector

In a circle of radius 9, find the arc length and sector area for a central angle of 120°.

Answer

Arc 6π≈18.856\pi \approx 18.85, sector 27π≈84.8227\pi \approx 84.82.

Solution
fraction=120/360=1/3arc=(1/3)⋅2π⋅9=6πsector=(1/3)⋅π⋅81=27π\begin{aligned} \text{fraction} &= 120 / 360 = 1/3 \\ \text{arc} &= (1/3) \cdot 2\pi \cdot 9 = 6\pi \\ \text{sector} &= (1/3) \cdot \pi \cdot 81 = 27\pi \end{aligned}

Exercise 3: Inscribed angle

An arc has a central angle of 140°. What is any inscribed angle that cuts off the same arc?

Answer

70°

Solution

The inscribed angle is half the central angle: 140∘/2=70∘140^\circ / 2 = 70^\circ.

Exercise 4: Centre and radius

Find the centre and radius of x2+y2+8x−2y+8=0x^2 + y^2 + 8x - 2y + 8 = 0.

Answer

Centre (−4,1)(-4, 1), radius 3.

Solution
(x2+8x+16)+(y2−2y+1)=−8+16+1(x+4)2+(y−1)2=9centre (−4,1),r=9=3\begin{aligned} &(x^2 + 8x + 16) + (y^2 - 2y + 1) = -8 + 16 + 1 \\ &(x + 4)^2 + (y - 1)^2 = 9 \\ &\text{centre } (-4, 1), \quad r = \sqrt{9} = 3 \end{aligned}

Exercise 5: Exactly touching

Circle A has centre (1,1)(1, 1) and radius 2; circle B has centre (5,4)(5, 4) and radius 3. What does circles_hit return, and why?

Answer

False: the circles touch at exactly one point but do not overlap.

Solution
distance2=(5−1)2+(4−1)2=16+9=25,distance=5sum of radii=2+3=525<25 is false→touching, not overlapping\begin{aligned} &\text{distance}^2 = (5 - 1)^2 + (4 - 1)^2 = 16 + 9 = 25, \qquad \text{distance} = 5 \\ &\text{sum of radii} = 2 + 3 = 5 \\ &25 < 25 \text{ is false} \to \text{touching, not overlapping} \end{aligned}

Use <= instead if touching should count as a hit.

python · try it yourself
dx, dy = 5 - 1, 4 - 1
print(dx * dx + dy * dy, (2 + 3) ** 2, dx * dx + dy * dy < (2 + 3) ** 2)
25 25 False

Test yourself

What is the area of a sector with radius 4 and central angle 90°?

  1. 16π16\pi
  2. 2π2\pi
  3. 8π8\pi
  4. 4π4\pi

An angle is inscribed in a semicircle. How large is it?

  1. 90°
  2. 45°
  3. 180°
  4. It depends on where the vertex is

Why do games compare dx2+dy2dx^2 + dy^2 with (r1+r2)2(r_1 + r_2)^2 instead of taking a square root?

  1. The square root gives wrong answers
  2. It avoids a slower square root and gives the same yes/no result
  3. Squares work only for integers
  4. It makes circles larger