Skip to main content

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

  1. Clone the repository:
git clone https://github.com/valerius21/stara-cpp.git
cd stara-cpp
  1. Create and activate a Poetry environment:
poetry install
poetry shell
  1. 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 maze
  • find_path(start: Tuple[int, int], goal: Tuple[int, int]) -> Optional[List[Tuple[int, int]]]: Find path from start to goal
  • manhattan_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 bounds
  • isWalkable(int x, int y) -> bool: Check if position is walkable
  • getCellNeighbours(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 path
  • manhattanDistance(pair<int, int> pos1, pair<int, int> pos2) -> int: Calculate heuristic

Development

Building from Source

  1. Install development dependencies:
poetry install
  1. Build in development mode:
poetry shell
pip install -e .

Running Tests

Run the example script:

python example.py

Project details


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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stara_cpp-0.1.0-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

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

Hashes for stara_cpp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 83eec4c497ddb757afd71264fc362d0563f45e2b58f95ad3249eedccebf6cd3b
MD5 520c9fdb9d795af6b5eb4c1d142916e8
BLAKE2b-256 4d7b41721f68e3aa2bd2900fc6888bdf9ea9210bc770a80d0f28050411862987

See more details on using hashes here.

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

Hashes for stara_cpp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11a6dc78da128c6036e8ecda39f31bd909f344f1d18ef19f031138babdd0eea5
MD5 02f37c3116fb1b2f4a9f9e056f1cbf8e
BLAKE2b-256 6650da699963eb642376eb4ef4c2a6643c7ff75aabf436ee8a408ce318d38aec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page