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.
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:
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:
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:
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:
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
acoswhen you want the ordinary magnitude of the angle - '
atan2with 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
ABCis measured atB. - 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 vectorsBAandBC. - The dot-product formula gives the usual unsigned angle.
- '
atan2with 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
- How to calculate Ebk of networks with Python?
- How to calculate or approximate the median of a list without storing the list
- how to calculate PDF in tensorflow
- How to calculate rounded corners for a polygon?
- How to calculate smallest multiple formed only of the digit 1?
- How to calculate the angle between a line and the horizontal axis?
- How to calculate the angle of a vector from the vertical?
- How to calculate the coordinates of a arrowhead based on the arrow?

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 courseTrack 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.