---
title: "Stop diagonal corner cutting in C#"
url: https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/
description: "Keep diagonal grid moves from cutting past walls in C#. Compare OnePath’s three corner rules on one map, with runnable examples of each route."
version: "1.2.0"
---

# Stop diagonal corner cutting in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/prevent-diagonal-corner-cutting.zip)

A diagonal move stays blocked when either cell beside it is a wall. `NoCornerCutting` is OnePath’s default for eight-way movement.

```shell
dotnet add package RecursiveAI.OnePath
```

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

```csharp
using RecursiveAI.OnePath;

// (x, y) = column, row; (0, 0) is top-left.
// # is a wall; . is open.
var map = Grid.FromText(".#.\n#..\n...");
var options = new GridOptions {
    Diagonals = DiagonalRule.NoCornerCutting
};
Cell start = new(2, 0), goal = new(1, 1);
var path = map.FindPath(start, goal, options);
var cells = path.Cells.ToArray()
    .Select(cell => $"({cell.X},{cell.Y})");
Console.WriteLine(string.Join(" -> ", cells));
```

```text
(2,0) -> (2,1) -> (1,1)
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/prevent-diagonal-corner-cutting.zip), then `dotnet run`.

## The full program

The same map gives two cases with goal `(1,1)`: start at `(2,0)` for one wall beside the direct diagonal, or at `(0,0)` for two. The program checks the expected cells and prints each route.

```csharp
using RecursiveAI.OnePath;

var map = Grid.FromText(".#.\n#..\n...");
Cell goal = new(1, 1);
foreach (var rule in Enum.GetValues<DiagonalRule>())
{
    var options = new GridOptions { Diagonals = rule };
    GridPath oneWall = map.FindPath(new(2, 0), goal, options);
    GridPath twoWalls = map.FindPath(new(0, 0), goal, options);
    Cell[] expectedOne = rule == DiagonalRule.NoCornerCutting
        ? [new(2, 0), new(2, 1), goal]
        : [new(2, 0), goal];
    Cell[] expectedTwo = rule == DiagonalRule.AllowAll
        ? [new(0, 0), goal]
        : [];
    var expectedStatus = expectedTwo.Length == 0
        ? PathStatus.NoPath : PathStatus.Found;
    if (oneWall.Status != PathStatus.Found || twoWalls.Status != expectedStatus ||
        !oneWall.Cells.SequenceEqual(expectedOne) ||
        !twoWalls.Cells.SequenceEqual(expectedTwo))
        throw new InvalidOperationException($"Unexpected path for {rule}.");

    Console.WriteLine(rule);
    Console.WriteLine($"  One wall: {Describe(oneWall)}");
    Console.WriteLine($"  Two walls: {Describe(twoWalls)}");
}

static string Describe(GridPath path) => path.Status == PathStatus.Found
    ? string.Join(" -> ", path.Cells.ToArray().Select(cell => $"({cell.X},{cell.Y})"))
    : path.Status.ToString();
```

`dotnet run` prints:

```text
NoCornerCutting
  One wall: (2,0) -> (2,1) -> (1,1)
  Two walls: NoPath
NoSqueezing
  One wall: (2,0) -> (1,1)
  Two walls: NoPath
AllowAll
  One wall: (2,0) -> (1,1)
  Two walls: (0,0) -> (1,1)
```

If the rule blocks every route to the goal, `FindPath` returns `NoPath`. It doesn’t return a route that cuts the corner anyway.

## Variations

**Turn off diagonal movement entirely.** Append this to the snippet at the top. With four-way movement, the diagonal rule has no effect.

```csharp
var straight = map.FindPath(start, goal,
    GridOptions.FourConnected);
Console.WriteLine(straight.Cells.Length - 1);
```

```text
2
```

## Related recipes

- [Find a weighted grid path in C#](https://recursiveai.org/onepath/cookbook/route-around-expensive-grid-terrain/)
- [A* pathfinding in C#](https://recursiveai.org/onepath/cookbook/a-star-pathfinding/)
- [Grid paths for wide units in C#](https://recursiveai.org/onepath/cookbook/pathfind-with-agent-clearance/)

## The short answer

Use `DiagonalRule.NoCornerCutting` for eight-way grid movement. It requires both cells beside a diagonal step to be walkable, in addition to the destination. That is OnePath’s default on square grids, so you only need to set it explicitly if you want the rule visible at the call site or are replacing different options.

## How do you avoid cutting corners with A* pathfinding?

Check a move when you decide whether to add the destination as a neighbor. For a diagonal from `(x,y)` to `(x+1,y+1)`, the two cells that share an edge with both ends are `(x+1,y)` and `(x,y+1)`. The [grid’s diagonal rules](https://github.com/recursive-ai-oss/onepath-csharp/blob/9da11631e6c0d85abbb302e51f0511b74fa728fd/docs/grids.md#L39-L49) apply to those cells. Checking only the destination would let a unit move past an obstacle even though the diagonal passes its corner.

In this map, the top-right unit can move down before going left, so banning the diagonal adds a step but doesn’t strand it. The top-left unit has walls immediately to its right and below, so banning its diagonal really does leave it with no route. Those are different outcomes of the same rule: taking the detour is possible only when a legal detour exists.

## What is the difference between no corner cutting and no squeezing?

| Rule | Neither beside cell is blocked | One is blocked | Both are blocked |
| --- | --- | --- | --- |
| `NoCornerCutting` | Allowed | Blocked | Blocked |
| `NoSqueezing` | Allowed | Allowed | Blocked |
| `AllowAll` | Allowed | Allowed | Allowed |

In all three cases the destination still has to be walkable. Choose `NoSqueezing` if you want to pass one obstacle corner but not a diagonal pinch between two walls. Choose `AllowAll` only if that pinch is a valid move in your map’s rules. If the unit can move only up, down, left or right, use `GridOptions.FourConnected` instead of changing a diagonal rule. For a multi-cell square unit, “walkable” means its whole footprint fits at the cell being tested.
