Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OnePath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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.

C#
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.

C#
var straight = map.FindPath(start, goal,
    GridOptions.FourConnected);
Console.WriteLine(straight.Cells.Length - 1);
Text
2

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 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?

RuleNeither beside cell is blockedOne is blockedBoth are blocked
NoCornerCuttingAllowedBlockedBlocked
NoSqueezingAllowedAllowedBlocked
AllowAllAllowedAllowedAllowed

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.

All onepath recipes