geometry
trigonometry
mathematics
angle-calculation
Cartesian-coordinates

How to calculate an angle from three points?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

To calculate the angle from three points A, B, and C, you usually mean the angle at B, formed by the segments BA and BC. The standard way to compute it is to turn those segments into vectors and then use either the dot-product formula or atan2 with cross and dot products.

Build vectors from the middle point

If the angle is ABC, the vertex is B, so the relevant vectors are:

  • 'BA = A - B'
  • 'BC = C - B'

In coordinates:

python
1ax, ay = 1, 3
2bx, by = 4, 5
3cx, cy = 7, 2
4
5ba = (ax - bx, ay - by)
6bc = (cx - bx, cy - by)

The angle depends on these two direction vectors, not on the absolute positions alone.

Use the dot-product formula for the unsigned angle

The cosine formula is:

  • 'cos(theta) = dot(BA, BC) / (|BA| * |BC|)'

In Python:

python
1import math
2
3def angle_from_three_points(a, b, c):
4    bax = a[0] - b[0]
5    bay = a[1] - b[1]
6    bcx = c[0] - b[0]
7    bcy = c[1] - b[1]
8
9    dot = bax * bcx + bay * bcy
10    mag_ba = math.hypot(bax, bay)
11    mag_bc = math.hypot(bcx, bcy)
12
13    cos_theta = dot / (mag_ba * mag_bc)
14    cos_theta = max(-1.0, min(1.0, cos_theta))
15
16    return math.degrees(math.acos(cos_theta))
17
18
19print(angle_from_three_points((1, 3), (4, 5), (7, 2)))

Clamping cos_theta into [-1, 1] is important because floating-point roundoff can otherwise produce a tiny invalid value such as 1.0000000002.

atan2 is often better numerically

For a 2D angle, a very robust approach is to combine:

  • the dot product
  • the scalar 2D cross product

Then use atan2:

python
1import math
2
3def angle_from_three_points_atan2(a, b, c):
4    bax = a[0] - b[0]
5    bay = a[1] - b[1]
6    bcx = c[0] - b[0]
7    bcy = c[1] - b[1]
8
9    dot = bax * bcx + bay * bcy
10    cross = bax * bcy - bay * bcx
11
12    return math.degrees(math.atan2(abs(cross), dot))

This returns the unsigned angle in the range from 0 to 180 degrees and is often more stable than relying only on acos.

Watch out for degenerate cases

If one of the segments has zero length, the angle is undefined because one of the vectors has no direction. That happens when:

  • 'A == B'
  • or C == B

You should check for that explicitly:

python
if mag_ba == 0 or mag_bc == 0:
    raise ValueError("angle is undefined when two points coincide with the vertex")

Ignoring this case leads to division by zero or meaningless output.

Signed angle versus unsigned angle

Sometimes you need direction, not just size. In 2D, the sign of the cross product tells you orientation:

  • positive cross product: one turning direction
  • negative cross product: the other turning direction

That is useful in geometry, robotics, and graphics when clockwise versus counterclockwise matters.

Choose the formula by need

Use:

  • dot product plus acos when you want the ordinary magnitude of the angle
  • 'atan2 with cross and dot when you want better numerical robustness or orientation-related logic'

Both methods are based on the same underlying vectors.

Common Pitfalls

  • Using the wrong point as the vertex when forming the vectors.
  • Forgetting that the angle ABC is measured at B.
  • Ignoring zero-length segments when two points coincide.
  • Failing to clamp the cosine value before calling acos.
  • Confusing signed orientation with the ordinary unsigned interior angle.

Summary

  • To compute angle ABC, build vectors BA and BC.
  • The dot-product formula gives the usual unsigned angle.
  • 'atan2 with cross and dot products is often numerically more robust.'
  • Degenerate cases must be handled when one segment has zero length.
  • Be explicit about whether you need an unsigned angle or a signed orientation.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.