A* pathfinding algorithm implementation in C++
Project description
Stara C++
A C++ implementation of the A* pathfinding algorithm with Python bindings using pybind11.
Installation
pip install stara-cpp
Requirements
- Python 3.12 or higher
- CMake 3.15 or higher
- C++17 compatible compiler
- Poetry (for Python dependency management)
- numpy
Development
- 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.2.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.2.tar.gz.
File metadata
- Download URL: stara_cpp-0.1.2.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 |
71745220b410292db0c9eeede15fe079589498ce9590a78418b7aa7a210789bb
|
|
| MD5 |
6d29714f12f6d468ae5e29ed835d1956
|
|
| BLAKE2b-256 |
0b82b097522383a97e4243e63f482058608cc9f34479238aa5ce302d79fe883a
|
File details
Details for the file stara_cpp-0.1.2-py3-none-any.whl.
File metadata
- Download URL: stara_cpp-0.1.2-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 |
db5fcdd10de3a3ecbfb2b7deb2dc2bfee71caa6b6f664884cf876c32007b91f7
|
|
| MD5 |
4eb08045a39b72e8a868f5bca14b6c8a
|
|
| BLAKE2b-256 |
d54678266eb7fe263ff03149d031e0fae19ca6c72625c46e176bc92831f974ff
|