A* pathfinding algorithm implementation in C++
Project description
Stara C++
A C++ implementation of the A* pathfinding algorithm with Python bindings using pybind11.
Requirements
- Python 3.12 or higher
- CMake 3.15 or higher
- C++17 compatible compiler
- Poetry (for Python dependency management)
- numpy
Installation
- Clone the repository:
git clone https://github.com/valerius21/stara-cpp.git
cd stara-cpp
- Create and activate a Poetry environment:
poetry install
poetry shell
- Build and install the package:
pip install -e .
Usage
Python
import numpy as np
from stara_cpp import load_maze, AStar
# Create a maze (0 = wall, ≥1 = passage)
maze = np.array([
[1, 0, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 0, 1, 0, 1],
[1, 1, 1, 0, 1],
[1, 0, 1, 1, 1],
], dtype=np.int32)
# Load maze and create pathfinder
maze_ptr = load_maze(maze)
pathfinder = AStar(maze_ptr)
# Find path from (0,0) to (4,4)
start = (0, 0)
goal = (4, 4)
try:
path = pathfinder.find_path(start, goal)
if path is not None:
print("Path found:", path)
else:
print("No path found!")
except RuntimeError as e:
print("Error:", str(e))
API Reference
Python API
load_maze(array: np.ndarray) -> Maze
Loads a numpy array into a C++ Maze object.
array: 2D numpy array where 0 represents walls and ≥1 represents passages- Returns: Maze object
class AStar
A* pathfinding implementation.
Methods:
__init__(maze: Maze): Initialize with a mazefind_path(start: Tuple[int, int], goal: Tuple[int, int]) -> Optional[List[Tuple[int, int]]]: Find path from start to goalmanhattan_distance(pos1: Tuple[int, int], pos2: Tuple[int, int]) -> int: Calculate Manhattan distance
C++ API
class Maze
Represents the maze grid.
Methods:
isValid(int x, int y) -> bool: Check if coordinates are within boundsisWalkable(int x, int y) -> bool: Check if position is walkablegetCellNeighbours(int x, int y) -> vector<tuple<int, int, int>>: Get neighboring cells
class AStar
A* pathfinding implementation.
Methods:
findPath(pair<int, int> start, pair<int, int> goal) -> optional<vector<pair<int, int>>>: Find pathmanhattanDistance(pair<int, int> pos1, pair<int, int> pos2) -> int: Calculate heuristic
Development
Building from Source
- Install development dependencies:
poetry install
- Build in development mode:
poetry shell
pip install -e .
Running Tests
Run the example script:
python example.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
stara_cpp-0.1.0.tar.gz
(4.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stara_cpp-0.1.0.tar.gz.
File metadata
- Download URL: stara_cpp-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.8 Darwin/24.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83eec4c497ddb757afd71264fc362d0563f45e2b58f95ad3249eedccebf6cd3b
|
|
| MD5 |
520c9fdb9d795af6b5eb4c1d142916e8
|
|
| BLAKE2b-256 |
4d7b41721f68e3aa2bd2900fc6888bdf9ea9210bc770a80d0f28050411862987
|
File details
Details for the file stara_cpp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: stara_cpp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.8 Darwin/24.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11a6dc78da128c6036e8ecda39f31bd909f344f1d18ef19f031138babdd0eea5
|
|
| MD5 |
02f37c3116fb1b2f4a9f9e056f1cbf8e
|
|
| BLAKE2b-256 |
6650da699963eb642376eb4ef4c2a6643c7ff75aabf436ee8a408ce318d38aec
|