Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions C++/pathfinding_visualizer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ----------------------------
# Build folders
# ----------------------------
build/
build-*/
out/
bin/
obj/

# Generated Qt / CMake files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
CTestTestfile.cmake
Makefile
*.moc
*.qrc.dep
*.qm
*.qmake.stash
*.qmake.cache
*.qtc_settings
*.qtc_clangd
.qm
*_automoc.cpp

# Qt Creator project files
*.user
*.pro.user
*.qmlproject.user
*.tags
*.swp
*.autosave
*.creator
*.creator.user

# VS Code
.vscode/
.vscode-ipch/
*.code-workspace

# CLion / JetBrains
.idea/
cmake-build-*/

# Windows system files
Thumbs.db
Desktop.ini
$RECYCLE.BIN/

# Logs
*.log

# Temporary files
*.tmp
*.temp
*.bak
*~
*.cache
*.orig

# Compiled object files & artifacts
*.o
*.obj
*.dll
*.exe
*.pdb
*.lib
*.a
*.so
*.dylib

# Python virtual envs (if used in repo)
venv/
.env/

# Ignore test output
test_output/
32 changes: 32 additions & 0 deletions C++/pathfinding_visualizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.21)
project(pathfinding_visualizer LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)

# Enable Qt automatic processing
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets Core Gui)

qt_add_executable(pathfinding_visualizer
src/main.cpp
src/MainWindow.cpp
src/Grid.cpp
src/Node.cpp
src/Algorithms/AlgorithmWorker.cpp

# Headers (needed for AUTOMOC)
include/MainWindow.hpp
include/Grid.hpp
include/Node.hpp
include/Algorithms/AlgorithmWorker.hpp

# UI + Resources
ui/MainWindow.ui
)

target_include_directories(pathfinding_visualizer PRIVATE include)

target_link_libraries(pathfinding_visualizer PRIVATE Qt6::Widgets Qt6::Core Qt6::Gui)
158 changes: 158 additions & 0 deletions C++/pathfinding_visualizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Pathfinding Visualizer (Code_Script Module)

A GUI-based C++17 + Qt6 pathfinding animation tool that visually demonstrates
**BFS**, **Dijkstra**, and **A\*** exploring a 2D grid in real time.
This module is intended for educational use and integration into the Code_Script project.

---

## 📄 Small Description

The Pathfinding Visualizer is an interactive Qt6 application that displays how classical pathfinding algorithms traverse a grid to find a path between two points. Users can place walls, change algorithms, adjust animation speed, and observe the search progress and final path.

---

## 🎯 Goals

- Visually demonstrate how BFS, Dijkstra, and A\* explore and find shortest paths.
- Provide an interactive 2D grid where users can toggle walls and set start/target nodes.
- Animate algorithm steps in real-time (visited nodes + final path).
- Maintain a thread-safe architecture using a background worker thread for algorithms.
- Offer adjustable animation speed and algorithm selection via a toolbar UI.
- Serve as a self-contained Code_Script module.

---

## 📁 File Structure

```
pathfinding_visualizer/
├── CMakeLists.txt
├── README.md
├── .gitignore
├── include/
│ ├── MainWindow.hpp
│ ├── Grid.hpp
│ ├── Node.hpp
│ └── Algorithms/
│ └── AlgorithmWorker.hpp
├── src/
│ ├── main.cpp
│ ├── MainWindow.cpp
│ ├── Grid.cpp
│ ├── Node.cpp
│ └── Algorithms/
│ └── AlgorithmWorker.cpp
├── ui/
│ └── MainWindow.ui
```

---

## 🛠️ Prerequisites

### Software Requirements
- **Qt 6.9.3** (or later)
- MinGW 64-bit or MSVC 64-bit build tools installed via Qt Online Installer.
- **CMake 3.16+**
- **C++17 compatible compiler**
- MSYS2 MinGW-w64 (preferred for simplicity)
- or Microsoft Visual C++ (MSVC)
- **VS Code** (recommended) with:
- *CMake Tools* extension
- *C/C++* extension

### Optional
- Qt Creator (IDE)
- Git (if contributing back to Code_Script)

---

## ▶️ Usage Instructions

### Running the Application
After building the project, launch:

./build/pathfinding_visualizer.exe

This opens the main window containing the 2D grid and the control toolbar.

---

### Grid Interaction

- **Left Click** on a cell → Toggle Wall (White ↔ Black)
- **Right Click** on a cell → Set Start Node (Green)
- **Shift + Left Click** or **Middle Click** → Set Target Node (Red)

The grid updates visually using `Node` objects in a `QGraphicsScene`.

---

### Toolbar Controls

- **Run**
Starts the selected algorithm on a background thread (`AlgorithmWorker`).

- **Reset**
Clears all walls, visited cells, and path markings. Start/Target nodes return to defaults.

- **Algorithm Selector**
Choose between **BFS**, **Dijkstra**, or **A\***.

- **Speed Slider**
Controls animation delay (ms per step).
Lower value = faster, higher = slower.

---

### Visualization Colors
- **Start Node** → Green
- **Target Node** → Red
- **Walls** → Black
- **Visited Cells** → Light Blue
- **Final Path** → Yellow

Updates are triggered by worker-thread signals:
`visit(row,col)` and `pathNode(row,col)`.

---

## 🔧 Build Instructions (Windows — Qt 6.9.3)

### Requirements
- Qt **6.9.3**
- Either **MinGW 64-bit** or **MSVC 2022 64-bit** Qt build installed
- CMake **3.16+**
- A C++17 compiler
- Optional: VS Code with **CMake Tools** extension

---

### 1️⃣ Configure (MinGW Example)

cmake -B build -S . -G "MinGW Makefiles" ^
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/mingw_64/lib/cmake"

### 2️⃣ Build (MinGW)
cmake --build build

---

### 1️⃣ Configure (MSVC Example)

cmake -B build -S . -G "NMake Makefiles" `
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/msvc2022_64/lib/cmake"

### 2️⃣ Build (MSVC)
cmake --build build --config Release

---

### 3️⃣ Run

./build/pathfinding_visualizer.exe
34 changes: 34 additions & 0 deletions C++/pathfinding_visualizer/include/Algorithms/AlgorithmWorker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <QObject>
#include <QPoint>
#include <QVector>

class AlgorithmWorker : public QObject {
Q_OBJECT
public:
explicit AlgorithmWorker(QObject *parent = nullptr);
~AlgorithmWorker() override;

public slots:
void runBFS(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);
void runDijkstra(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);
void runAStar(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);

void requestAbort();

signals:
void visit(int row, int col);
void pathNode(int row, int col);
void status(const QString &msg);
void finished();

private:
volatile bool m_abortRequested;

void sleepMs(int ms) const;

static inline int manhattan(int r1, int c1, int r2, int c2) {
return qAbs(r1 - r2) + qAbs(c1 - c2);
}
};
51 changes: 51 additions & 0 deletions C++/pathfinding_visualizer/include/Grid.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <QGraphicsScene>
#include <QObject>
#include <QPoint>
#include <QVector>

/**
* Grid manages QGraphicsScene and Node items.
* It also handles mouse interactions by installing an event filter on the scene.
* exportModel() returns a copy (QVector) safe to send across threads.
*/
class Node;
class Grid : public QObject {
Q_OBJECT
public:
struct Model {
QVector<QVector<int>> grid; // 0 = free, 1 = wall
QPoint start;
QPoint target;
};

explicit Grid(int rows, int cols, QObject *parent = nullptr);
~Grid() override;

QGraphicsScene* scene() const { return m_scene; }

Model exportModel() const;

// Called from GUI thread (slots)
void markVisited(int r, int c);
void markPath(int r, int c);
void reset();

protected:
// eventFilter to capture mouse clicks on the scene and translate to grid actions
bool eventFilter(QObject *watched, QEvent *event) override;

private:
void toggleWallAtScenePos(const QPointF &scenePos);
void setStartAtScenePos(const QPointF &scenePos);
void setTargetAtScenePos(const QPointF &scenePos);

int m_rows;
int m_cols;
QGraphicsScene *m_scene;
QVector<QVector<Node*>> m_nodes;

QPoint m_start;
QPoint m_target;
};
Loading
Loading