vector angle
vector mathematics
angle calculation
trigonometry
physics basics

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.

Practice algorithms

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:

text
cos(theta) = y / sqrt(x^2 + y^2)

So:

text
theta = arccos(y / |v|)

Python example:

python
1import math
2
3def angle_from_vertical_unsigned(x, y):
4    magnitude = math.hypot(x, y)
5    if magnitude == 0:
6        raise ValueError("zero vector has no direction")
7    return math.degrees(math.acos(y / magnitude))
8
9print(angle_from_vertical_unsigned(3, 4))  # about 36.87 degrees

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.

python
1import math
2
3def angle_from_vertical_signed(x, y):
4    return math.degrees(math.atan2(x, y))
5
6print(angle_from_vertical_signed(3, 4))   # right of vertical
7print(angle_from_vertical_signed(-3, 4))  # left of vertical

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:

python
math.atan2(x, y)

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.

python
if x == 0 and y == 0:
    raise ValueError("zero vector has no angle")

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 of atan2, 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
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.