Skip to content

Commit f734a59

Browse files
authored
Merge pull request #107 from Ash-Jose/pathfinding_visualizer
Pathfinding visualizer issue #105 completed
2 parents 276761d + d89520c commit f734a59

File tree

30 files changed

+1061
-47
lines changed

30 files changed

+1061
-47
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# ----------------------------
2+
# Build folders
3+
# ----------------------------
4+
build/
5+
build-*/
6+
out/
7+
bin/
8+
obj/
9+
10+
# Generated Qt / CMake files
11+
CMakeFiles/
12+
CMakeCache.txt
13+
cmake_install.cmake
14+
CTestTestfile.cmake
15+
Makefile
16+
*.moc
17+
*.qrc.dep
18+
*.qm
19+
*.qmake.stash
20+
*.qmake.cache
21+
*.qtc_settings
22+
*.qtc_clangd
23+
.qm
24+
*_automoc.cpp
25+
26+
# Qt Creator project files
27+
*.user
28+
*.pro.user
29+
*.qmlproject.user
30+
*.tags
31+
*.swp
32+
*.autosave
33+
*.creator
34+
*.creator.user
35+
36+
# VS Code
37+
.vscode/
38+
.vscode-ipch/
39+
*.code-workspace
40+
41+
# CLion / JetBrains
42+
.idea/
43+
cmake-build-*/
44+
45+
# Windows system files
46+
Thumbs.db
47+
Desktop.ini
48+
$RECYCLE.BIN/
49+
50+
# Logs
51+
*.log
52+
53+
# Temporary files
54+
*.tmp
55+
*.temp
56+
*.bak
57+
*~
58+
*.cache
59+
*.orig
60+
61+
# Compiled object files & artifacts
62+
*.o
63+
*.obj
64+
*.dll
65+
*.exe
66+
*.pdb
67+
*.lib
68+
*.a
69+
*.so
70+
*.dylib
71+
72+
# Python virtual envs (if used in repo)
73+
venv/
74+
.env/
75+
76+
# Ignore test output
77+
test_output/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(pathfinding_visualizer LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
# Enable Qt automatic processing
7+
set(CMAKE_AUTOMOC ON)
8+
set(CMAKE_AUTOUIC ON)
9+
set(CMAKE_AUTORCC ON)
10+
11+
find_package(Qt6 REQUIRED COMPONENTS Widgets Core Gui)
12+
13+
qt_add_executable(pathfinding_visualizer
14+
src/main.cpp
15+
src/MainWindow.cpp
16+
src/Grid.cpp
17+
src/Node.cpp
18+
src/Algorithms/AlgorithmWorker.cpp
19+
20+
# Headers (needed for AUTOMOC)
21+
include/MainWindow.hpp
22+
include/Grid.hpp
23+
include/Node.hpp
24+
include/Algorithms/AlgorithmWorker.hpp
25+
26+
# UI + Resources
27+
ui/MainWindow.ui
28+
)
29+
30+
target_include_directories(pathfinding_visualizer PRIVATE include)
31+
32+
target_link_libraries(pathfinding_visualizer PRIVATE Qt6::Widgets Qt6::Core Qt6::Gui)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Pathfinding Visualizer (Code_Script Module)
2+
3+
A GUI-based C++17 + Qt6 pathfinding animation tool that visually demonstrates
4+
**BFS**, **Dijkstra**, and **A\*** exploring a 2D grid in real time.
5+
This module is intended for educational use and integration into the Code_Script project.
6+
7+
---
8+
9+
## 📄 Small Description
10+
11+
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.
12+
13+
---
14+
15+
## 🎯 Goals
16+
17+
- Visually demonstrate how BFS, Dijkstra, and A\* explore and find shortest paths.
18+
- Provide an interactive 2D grid where users can toggle walls and set start/target nodes.
19+
- Animate algorithm steps in real-time (visited nodes + final path).
20+
- Maintain a thread-safe architecture using a background worker thread for algorithms.
21+
- Offer adjustable animation speed and algorithm selection via a toolbar UI.
22+
- Serve as a self-contained Code_Script module.
23+
24+
---
25+
26+
## 📁 File Structure
27+
28+
```
29+
pathfinding_visualizer/
30+
31+
├── CMakeLists.txt
32+
├── README.md
33+
├── .gitignore
34+
35+
├── include/
36+
│ ├── MainWindow.hpp
37+
│ ├── Grid.hpp
38+
│ ├── Node.hpp
39+
│ └── Algorithms/
40+
│ └── AlgorithmWorker.hpp
41+
42+
├── src/
43+
│ ├── main.cpp
44+
│ ├── MainWindow.cpp
45+
│ ├── Grid.cpp
46+
│ ├── Node.cpp
47+
│ └── Algorithms/
48+
│ └── AlgorithmWorker.cpp
49+
50+
├── ui/
51+
│ └── MainWindow.ui
52+
```
53+
54+
---
55+
56+
## 🛠️ Prerequisites
57+
58+
### Software Requirements
59+
- **Qt 6.9.3** (or later)
60+
- MinGW 64-bit or MSVC 64-bit build tools installed via Qt Online Installer.
61+
- **CMake 3.16+**
62+
- **C++17 compatible compiler**
63+
- MSYS2 MinGW-w64 (preferred for simplicity)
64+
- or Microsoft Visual C++ (MSVC)
65+
- **VS Code** (recommended) with:
66+
- *CMake Tools* extension
67+
- *C/C++* extension
68+
69+
### Optional
70+
- Qt Creator (IDE)
71+
- Git (if contributing back to Code_Script)
72+
73+
---
74+
75+
## ▶️ Usage Instructions
76+
77+
### Running the Application
78+
After building the project, launch:
79+
80+
./build/pathfinding_visualizer.exe
81+
82+
This opens the main window containing the 2D grid and the control toolbar.
83+
84+
---
85+
86+
### Grid Interaction
87+
88+
- **Left Click** on a cell → Toggle Wall (White ↔ Black)
89+
- **Right Click** on a cell → Set Start Node (Green)
90+
- **Shift + Left Click** or **Middle Click** → Set Target Node (Red)
91+
92+
The grid updates visually using `Node` objects in a `QGraphicsScene`.
93+
94+
---
95+
96+
### Toolbar Controls
97+
98+
- **Run**
99+
Starts the selected algorithm on a background thread (`AlgorithmWorker`).
100+
101+
- **Reset**
102+
Clears all walls, visited cells, and path markings. Start/Target nodes return to defaults.
103+
104+
- **Algorithm Selector**
105+
Choose between **BFS**, **Dijkstra**, or **A\***.
106+
107+
- **Speed Slider**
108+
Controls animation delay (ms per step).
109+
Lower value = faster, higher = slower.
110+
111+
---
112+
113+
### Visualization Colors
114+
- **Start Node** → Green
115+
- **Target Node** → Red
116+
- **Walls** → Black
117+
- **Visited Cells** → Light Blue
118+
- **Final Path** → Yellow
119+
120+
Updates are triggered by worker-thread signals:
121+
`visit(row,col)` and `pathNode(row,col)`.
122+
123+
---
124+
125+
## 🔧 Build Instructions (Windows — Qt 6.9.3)
126+
127+
### Requirements
128+
- Qt **6.9.3**
129+
- Either **MinGW 64-bit** or **MSVC 2022 64-bit** Qt build installed
130+
- CMake **3.16+**
131+
- A C++17 compiler
132+
- Optional: VS Code with **CMake Tools** extension
133+
134+
---
135+
136+
### 1️⃣ Configure (MinGW Example)
137+
138+
cmake -B build -S . -G "MinGW Makefiles" ^
139+
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/mingw_64/lib/cmake"
140+
141+
### 2️⃣ Build (MinGW)
142+
cmake --build build
143+
144+
---
145+
146+
### 1️⃣ Configure (MSVC Example)
147+
148+
cmake -B build -S . -G "NMake Makefiles" `
149+
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/msvc2022_64/lib/cmake"
150+
151+
### 2️⃣ Build (MSVC)
152+
cmake --build build --config Release
153+
154+
---
155+
156+
### 3️⃣ Run
157+
158+
./build/pathfinding_visualizer.exe
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
#include <QPoint>
5+
#include <QVector>
6+
7+
class AlgorithmWorker : public QObject {
8+
Q_OBJECT
9+
public:
10+
explicit AlgorithmWorker(QObject *parent = nullptr);
11+
~AlgorithmWorker() override;
12+
13+
public slots:
14+
void runBFS(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);
15+
void runDijkstra(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);
16+
void runAStar(const QVector<QVector<int>> &grid, const QPoint &start, const QPoint &target, int delayMs);
17+
18+
void requestAbort();
19+
20+
signals:
21+
void visit(int row, int col);
22+
void pathNode(int row, int col);
23+
void status(const QString &msg);
24+
void finished();
25+
26+
private:
27+
volatile bool m_abortRequested;
28+
29+
void sleepMs(int ms) const;
30+
31+
static inline int manhattan(int r1, int c1, int r2, int c2) {
32+
return qAbs(r1 - r2) + qAbs(c1 - c2);
33+
}
34+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <QGraphicsScene>
4+
#include <QObject>
5+
#include <QPoint>
6+
#include <QVector>
7+
8+
/**
9+
* Grid manages QGraphicsScene and Node items.
10+
* It also handles mouse interactions by installing an event filter on the scene.
11+
* exportModel() returns a copy (QVector) safe to send across threads.
12+
*/
13+
class Node;
14+
class Grid : public QObject {
15+
Q_OBJECT
16+
public:
17+
struct Model {
18+
QVector<QVector<int>> grid; // 0 = free, 1 = wall
19+
QPoint start;
20+
QPoint target;
21+
};
22+
23+
explicit Grid(int rows, int cols, QObject *parent = nullptr);
24+
~Grid() override;
25+
26+
QGraphicsScene* scene() const { return m_scene; }
27+
28+
Model exportModel() const;
29+
30+
// Called from GUI thread (slots)
31+
void markVisited(int r, int c);
32+
void markPath(int r, int c);
33+
void reset();
34+
35+
protected:
36+
// eventFilter to capture mouse clicks on the scene and translate to grid actions
37+
bool eventFilter(QObject *watched, QEvent *event) override;
38+
39+
private:
40+
void toggleWallAtScenePos(const QPointF &scenePos);
41+
void setStartAtScenePos(const QPointF &scenePos);
42+
void setTargetAtScenePos(const QPointF &scenePos);
43+
44+
int m_rows;
45+
int m_cols;
46+
QGraphicsScene *m_scene;
47+
QVector<QVector<Node*>> m_nodes;
48+
49+
QPoint m_start;
50+
QPoint m_target;
51+
};

0 commit comments

Comments
 (0)