Theta* joins cell centers with clear straight segments. It requires uniform costs and a one-cell agent.
dotnet add package RecursiveAI.OnePathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePath; var grid = Grid.FromText( ".....\n.###.\n....."); var path = grid.FindPath( new Cell(0, 0), new Cell(4, 2), new GridOptions { Algorithm = GridAlgorithm.ThetaStar }); Console.WriteLine(path.Status); Console.WriteLine(path.Algorithm);
Prints:
Found ThetaStar
Unzip the example, then dotnet run.
The full program
The full program defines a seven-column map inline: . is open and # is blocked. It prints A*, Theta* and Lazy Theta* waypoints for the same endpoints. Each segment is checked with GridLineOfSight.Test before printing.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Grids; var grid = Grid.FromText(".......\n..###..\n.......\n......."); var start = new Cell(0, 0); var goal = new Cell(6, 3); foreach (var algorithm in new[] { GridAlgorithm.AStar, GridAlgorithm.ThetaStar, GridAlgorithm.LazyThetaStar }) { var path = grid.FindPath(start, goal, new GridOptions { Algorithm = algorithm }); if (!path.Found) throw new InvalidOperationException(path.Status.ToString()); for (int i = 1; i < path.Cells.Length; i++) if (!GridLineOfSight.Test(grid, path.Cells[i - 1], path.Cells[i])) throw new InvalidOperationException("Blocked segment"); Console.WriteLine($"{path.Algorithm}: {path.Status}, {path.Cells.Length} waypoints"); Console.WriteLine(string.Join(" -> ", path.Cells.ToArray() .Select(cell => $"({cell.X},{cell.Y})"))); }
dotnet run prints:
AStar: Found, 8 waypoints (0,0) -> (1,1) -> (1,2) -> (2,3) -> (3,3) -> (4,3) -> (5,3) -> (6,3) ThetaStar: Found, 3 waypoints (0,0) -> (1,2) -> (6,3) LazyThetaStar: Found, 3 waypoints (0,0) -> (1,2) -> (6,3)
An unreachable goal returns NoPath; a blocked or out-of-range endpoint returns StartInvalid or GoalInvalid.
Variations
Use Lazy Theta* instead. This is a complete replacement for Program.cs.
using RecursiveAI.OnePath; var grid = Grid.FromText( ".....\n.###.\n....."); var path = grid.FindPath( new Cell(0, 0), new Cell(4, 2), new GridOptions { Algorithm = GridAlgorithm.LazyThetaStar }); Console.WriteLine(path.Status); Console.WriteLine(path.Algorithm);
Found LazyThetaStar
Related recipes
The short answer
For C# Theta* pathfinding, create a Grid and pass GridOptions with Algorithm = GridAlgorithm.ThetaStar to FindPath. Read Status before moving the agent, and check Algorithm to see which engine actually ran. Cells contains waypoints, not every cell crossed along the route; Points contains their world coordinates.
Theta* is useful when a tile map represents free space but an agent can move along straight lines between tiles. A route following those segments is different from a sequence of diagonal and orthogonal tile moves. The full program shows both representations on one map, without requiring a game engine or an external map file.
How does Theta* any-angle path planning on grids work?
OnePath uses cell centers as nodes, rather than the cell corners used in the original papers. When it considers a neighbor, Theta* tests whether that neighbor can see the current node’s parent. If so, the candidate path goes directly from the parent to the neighbor. Otherwise it uses the ordinary move from the current node.
The returned list therefore skips intermediate cells when a straight segment is clear. Do not interpret consecutive waypoints as adjacent tiles, and do not teleport from one waypoint to the next. Move along the segment, using the grid’s coordinate mapping to place it in your world. Segment cost is Euclidean distance multiplied by the uniform area multiplier; total cost sums those segments.
The pinned grid documentation specifies the search and line-of-sight model. This is a grid-based any-angle search, not a navigation-mesh query and not a guarantee of the globally shortest continuous path around arbitrary geometry.
How do I use a C# Theta* pathfinding example without cutting corners?
Keep the default NoCornerCutting rule. The line-of-sight test treats each wall as a closed square: merely touching a wall’s corner blocks a segment. Both cells beside a diagonal step must be open. Cells outside the grid also count as walls. The full program uses that same test on every returned segment.
Theta* applies only to eight-connected square grids, with agent size one, identical multipliers for all areas, no StepCost or Blocked callbacks, and an Auto, Euclidean or Zero heuristic. If those conditions fail, the query runs as A* and reports AStar in Algorithm. Requesting Theta* is not sufficient to establish that the result is an any-angle route.
For a larger square footprint, use ordinary grid search with AgentSize, or build a map whose free cells already represent safe center positions for your actor. The Theta* query itself does not apply a physical-radius clearance test.
How do I use Lazy Theta* in C#?
The variation selects GridAlgorithm.LazyThetaStar. It postpones visibility checks until a node is popped. If a tentative parent is not visible, the search repairs that parent using a closed grid neighbor.
OnePath’s tier-one option is the re-queue variant: after a repair changes the parent, the node returns to the open set with its corrected cost rather than being expanded immediately. It is not the exact same expansion order as the paper’s unmodified lazy variant. Both options still return waypoint segments under the same corner, cost and one-cell-agent restrictions. The sample’s matching waypoint lists are specific to this small map, not a promise that both searches always select the same route.