Skip to content

Commit ee32f35

Browse files
committed
Added the feature as proposed in issue #105
Signed-off-by: Ash-Jose <ashlinjose986@outlook.com>
1 parent 8fc08ca commit ee32f35

File tree

15 files changed

+1064
-47
lines changed

15 files changed

+1064
-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: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
> Icons are SVG files, recommended from **Lucide Icons** (MIT licensed).
55+
> Place all SVGs in `ui/icons/` exactly matching the filenames above.
56+
57+
---
58+
59+
## 🛠️ Prerequisites
60+
61+
### Software Requirements
62+
- **Qt 6.9.3** (or later)
63+
- MinGW 64-bit or MSVC 64-bit build tools installed via Qt Online Installer.
64+
- **CMake 3.16+**
65+
- **C++17 compatible compiler**
66+
- MSYS2 MinGW-w64 (preferred for simplicity)
67+
- or Microsoft Visual C++ (MSVC)
68+
- **VS Code** (recommended) with:
69+
- *CMake Tools* extension
70+
- *C/C++* extension
71+
72+
### Optional
73+
- Qt Creator (IDE)
74+
- Git (if contributing back to Code_Script)
75+
76+
---
77+
78+
## ▶️ Usage Instructions
79+
80+
### Running the Application
81+
After building the project, launch:
82+
83+
./build/pathfinding_visualizer.exe
84+
85+
This opens the main window containing the 2D grid and the control toolbar.
86+
87+
---
88+
89+
### Grid Interaction
90+
91+
- **Left Click** on a cell → Toggle Wall (White ↔ Black)
92+
- **Right Click** on a cell → Set Start Node (Green)
93+
- **Shift + Left Click** or **Middle Click** → Set Target Node (Red)
94+
95+
The grid updates visually using `Node` objects in a `QGraphicsScene`.
96+
97+
---
98+
99+
### Toolbar Controls
100+
101+
- **Run** (`play.svg`)
102+
Starts the selected algorithm on a background thread (`AlgorithmWorker`).
103+
104+
- **Reset** (`reset.svg`)
105+
Clears all walls, visited cells, and path markings. Start/Target nodes return to defaults.
106+
107+
- **Algorithm Selector**
108+
Choose between **BFS**, **Dijkstra**, or **A\***.
109+
110+
- **Speed Slider**
111+
Controls animation delay (ms per step).
112+
Lower value = faster, higher = slower.
113+
114+
---
115+
116+
### Visualization Colors
117+
- **Start Node** → Green
118+
- **Target Node** → Red
119+
- **Walls** → Black
120+
- **Visited Cells** → Light Blue
121+
- **Final Path** → Yellow
122+
123+
Updates are triggered by worker-thread signals:
124+
`visit(row,col)` and `pathNode(row,col)`.
125+
126+
---
127+
128+
## 🔧 Build Instructions (Windows — Qt 6.9.3)
129+
130+
### Requirements
131+
- Qt **6.9.3**
132+
- Either **MinGW 64-bit** or **MSVC 2022 64-bit** Qt build installed
133+
- CMake **3.16+**
134+
- A C++17 compiler
135+
- Optional: VS Code with **CMake Tools** extension
136+
137+
---
138+
139+
### 1️⃣ Configure (MinGW Example)
140+
141+
cmake -B build -S . -G "MinGW Makefiles" ^
142+
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/mingw_64/lib/cmake"
143+
144+
### 2️⃣ Build (MinGW)
145+
cmake --build build
146+
147+
---
148+
149+
### 1️⃣ Configure (MSVC Example)
150+
151+
cmake -B build -S . -G "NMake Makefiles" `
152+
-DCMAKE_PREFIX_PATH="C:/Qt/6.9.3/msvc2022_64/lib/cmake"
153+
154+
### 2️⃣ Build (MSVC)
155+
cmake --build build --config Release
156+
157+
---
158+
159+
### 3️⃣ Run
160+
161+
./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)