How to calculate the angle of a vector from the vertical?
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 of a 2D vector from the vertical axis, you need to be explicit about what "from the vertical" means. The simplest unsigned answer uses the dot product with the vertical unit vector, while the most practical signed answer usually comes from atan2 with the vector components arranged for a vertical reference frame.
Unsigned angle from the positive vertical axis
For a vector with components (x, y), the positive vertical axis is the vector (0, 1). The cosine formula gives:
So:
Python example:
This returns an angle between 0 and 180 degrees.
Signed angle using atan2
If you want to know whether the vector leans left or right of vertical, atan2 is usually better. For a vertical reference, swap the usual arguments conceptually so the angle is measured from the y-axis instead of the x-axis.
This gives a signed angle:
- positive for one side of vertical
- negative for the other side
That is often more useful in geometry, navigation, and game logic.
Why atan2(x, y) looks unusual
Most people learn atan2(y, x) for angles from the positive x-axis. Here the reference axis is vertical, not horizontal, so the component roles change conceptually.
That is why:
is the natural signed-angle form for "angle from vertical".
Example values
For the vector (0, 5):
- unsigned angle from vertical is
0 - signed angle from vertical is also
0
For (5, 0):
- unsigned angle is
90 - signed angle is
90
For (-5, 0):
- unsigned angle is still
90 - signed angle is
-90
That is the key difference between an unsigned geometric angle and a directional signed angle.
The zero vector is a special case
If the vector is (0, 0), it has no direction, so the angle is undefined. Good code should handle that case explicitly instead of quietly returning nonsense.
This matters more than people expect because zero vectors often appear in physics and graphics when two points coincide.
Choosing the right formula
Use the dot-product plus arccos form when you only need the magnitude of the angle from vertical. Use atan2 when you need orientation relative to vertical as well.
That is the real decision point, not just "which trig function looks familiar".
Common Pitfalls
- Measuring from the x-axis and forgetting the question asked for the vertical axis.
- Using
atan(y / x)instead ofatan2, which loses quadrant information. - Forgetting that unsigned and signed angles answer different questions.
- Not handling the zero vector as a special case.
- Mixing radians and degrees in the final result.
Summary
- From vertical, the unsigned angle can be computed with
arccos(y / |v|). - For a signed angle from the vertical axis,
atan2(x, y)is usually the most practical formula. - Decide whether you want only the size of the angle or also its left or right orientation.
- Handle the zero vector explicitly because its direction is undefined.
- Keep your units consistent when converting between radians and degrees.
Related reading
- How to calculate the coordinates of a arrowhead based on the arrow?
- How to calculate the Cosine similarity between two tensors?
- How to calculate the intersection of two sets?
- How to calculate the mirror point along a line?
- How to calculate the number of coprime subsets of the set 1,2,3,..,n
- How to calculate the shortest path between two points in a grid
- How to calculate total volume of multiple overlapping cuboids
- How to change edges' weight by designated rule?

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.