---
title: "Calculate a vector bearing with atan2 in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-bearing-from-vector/
description: "Use deterministic atan2 to turn a flat east/north vector into a compass bearing in degrees. Handle all four quadrants and reject a vector with no direction."
version: "1.0.0"
---

# Calculate a vector bearing with atan2 in C#

**OneMath** is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/calculate-bearing-from-vector/calculate-bearing-from-vector.zip)

A bearing turns clockwise from north. This uses a flat east/north vector.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;
double east = -1, north = 1;
double angle = Math64.Atan2(east, north);
double deg = angle * (180 / Math.PI);
if (deg < 0) deg += 360;
Console.WriteLine(deg == 360 || deg == 0 ? 0 : deg);
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/calculate-bearing-from-vector/calculate-bearing-from-vector.zip), then `dotnet run`.

Prints:

```text
315
```

## The full program

```csharp
using RecursiveAI.OneMath;

foreach (var (east, north) in new[] { (1d, 1d), (1d, -1d), (-1d, -1d), (-1d, 1d), (0d, 0d) })
{
    double? bearing = Bearing(east, north);
    string label = bearing is double degrees ? FormattableString.Invariant($"{degrees:0.###}°") : "undefined";
    Console.WriteLine($"({east}, {north}): {label}");
}

static double? Bearing(double east, double north)
{
    if (!double.IsFinite(east) || !double.IsFinite(north) || (east == 0 && north == 0))
        return null;

    double angle = Math64.Atan2(east, north);
    double degrees = angle * (180 / Math.PI);
    if (degrees < 0) degrees += 360;
    // Roundoff can put a tiny westward offset exactly at 360°.
    return degrees == 360 || degrees == 0 ? 0 : degrees;
}
```

```text
(1, 1): 45°
(1, -1): 135°
(-1, -1): 225°
(-1, 1): 315°
(0, 0): undefined
```

The program takes `(east, north)` pairs and prints each bearing in degrees. It adds 360 only for negative angles, so a tiny positive angle isn’t rounded away by adding and then removing a full turn.

If the vector is zero or either component isn’t finite, the helper returns `null` rather than reporting a direction.

## Variations

**Get a standard XY angle instead.** For an angle counterclockwise from positive X, pass `y` first and `x` second. Keep the result in radians if the next calculation expects radians.

```csharp
using RecursiveAI.OneMath;

double x = -1, y = 1;
Console.WriteLine(Math64.Atan2(y, x));
```

This prints `2.356194490192345` radians, or 135° counterclockwise from positive X.

## Related recipes

- [Calculate deterministic sine and cosine](https://recursiveai.org/onemath/cookbook/deterministic-trigonometry/)
- [Compare distances without a square root](https://recursiveai.org/onemath/cookbook/compare-distance-without-square-root/)


## The short answer

Use `Math64.Atan2(east, north)` for a compass bearing from a flat vector whose components point east and north. Multiply by `180 / Math.PI` to get degrees and wrap the result into `[0, 360)`. With east = −1 and north = 1, the vector points northwest and its bearing is 315°.

## How do you calculate the bearing of a vector?

Use components of a flat vector, not a pair of latitude/longitude coordinates. For a compass bearing, north is the zero axis and east is the direction of increasing angle. That’s why `east` goes in the first `Atan2` argument and `north` in the second. The usual mathematical angle uses positive X as zero and turns toward positive Y instead, as in the variation above.

The four cardinal vectors are a useful check: `(0, 1)` gives 0°, `(1, 0)` gives 90°, `(0, -1)` gives 180°, and `(-1, 0)` gives 270°. If you’re working on a 2D screen where Y increases down, north is the negative of the vertical displacement. If you’re working with two positions on a flat map, subtract the start position from the end position before taking the angle. Reversing the subtraction gives the opposite bearing.

For nonzero vectors, you don’t need to divide by the vector’s length. Scaling both components by the same positive factor doesn’t change their direction. The zero vector is different: it has no bearing. For two identical positions, decide whether to leave the previous bearing unchanged or show that there is no direction.

## How is atan2 different from atan?

`atan(east / north)` receives a ratio, not the two component signs. Northeast `(1, 1)` and southwest `(-1, -1)` have the same ratio but opposite bearings. A due-east or due-west vector also has a zero north component. `Atan2` takes both components directly, so it distinguishes all four quadrants and handles an axis without asking you to divide by zero. The [OneMath function reference](https://recursiveai.org/onemath/docs/functions/) documents its quadrant selection and signed-zero rules.

`Math64.Atan2` returns a correctly rounded `double` in radians. The degree conversion is a separate floating-point operation, not a single correctly rounded bearing in degrees. Use the conditional wrap if tiny angles matter. A negative angle goes to the west side of the compass; a result that rounds to exactly 360° is reported as north, the same direction as 0°. If you only need a direction label, round for display after computing the bearing, not before.
