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 | centre to any point on the circle |
| diameter | 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
is the ratio of any circle's circumference to its diameter. An arc or sector is just the fraction of the whole circle. In radians these become and , which is one reason radians are preferred later (Angles and Radians).
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 is on the circle with centre and radius exactly when its distance from the centre is . Squaring the distance formula gives:
Expanded, it looks like . To recover centre and radius, complete the square (Completing the Square): the centre is and .
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 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:
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 . Find its radius and circumference.
Answer
cm, cm.
Solution
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 , sector .
Solution
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: .
Exercise 4: Centre and radius
Find the centre and radius of .
Answer
Centre , radius 3.
Solution
Exercise 5: Exactly touching
Circle A has centre and radius 2; circle B has centre and radius 3. What does circles_hit return, and why?
Answer
False: the circles touch at exactly one point but do not overlap.
Solution
Use <= instead if touching should count as a hit.
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°?
An angle is inscribed in a semicircle. How large is it?
- 90°
- 45°
- 180°
- It depends on where the vertex is
Why do games compare with instead of taking a square root?
- The square root gives wrong answers
- It avoids a slower square root and gives the same yes/no result
- Squares work only for integers
- It makes circles larger