Skip to content

Commit 8d91702

Browse files
committed
initial commit
0 parents  commit 8d91702

22 files changed

+101997
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.ts

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Tend
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# A* pathfinding algorithm simulator
2+
Astar algorithm applied to a pathfinding scenario, with the ability to see the algorithm in action, step by step.
3+
4+
## :red_circle: Online version
5+
A live version is available [here](https://drendog.github.io/Logic-Circuit-Simulator/).
6+
![](demo.gif)
7+
8+
## :books: Documentation
9+
[Documentation](https://google.com)
10+
11+
## 🤝 Contributing
12+
13+
Contributions, issues and feature requests are welcome.<br>
14+
I'd especially appreciate every feedback aimed at improving the presentation and any bug report
15+
16+
## :wrench: Made with
17+
[p5.js library](https://github.com/processing/p5.js)<br>
18+
[BOOSTRAP](https://getbootstrap.com/)
19+
20+
## :book: Reference sources
21+
22+
[Wikipedia](https://en.wikipedia.org/wiki/A*_search_algorithm)<br>
23+
[Heuristics](http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html)
24+
25+
## :thought_balloon: Inspired by
26+
[Logic Circuit Simulator](https://github.com/drendog/Logic-Circuit-Simulator)
27+
28+
## 👤 Author
29+
30+
**[Tend](https://github.com/TendTo)**
31+
32+
## :balance_scale: License
33+
34+
[MIT](https://choosealicense.com/licenses/mit/)

doc/DOC.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
## Documentation
2+
3+
### Constants
4+
Vertical and Horizontal cost: 10
5+
Diagonal cost: 14
6+
7+
### Heuristics
8+
To find the h value, there are 4 possible heuristics contemplated:
9+
- **Manhattan distance:** estimate the distance considering only horizontal-vertical movements. Ignores obstacles
10+
- **Diagonal distance:** estimate the distance considering all the 8 possible directions. Ignores obstacles
11+
- **Euclid distance:** estimate the distance as the crow flies. Ignores obstacles
12+
- **Dijkstra (h = 0):** always set h = 0. It behaves exactly like Dijkstra's algorithm
13+
14+
### main.js
15+
```javascript
16+
/**
17+
* Called at the start
18+
*/
19+
function setup()
20+
21+
/**
22+
* Called each frame
23+
*/
24+
function draw()
25+
26+
/**
27+
* Set up the buttons variables and set the value for each button in settings
28+
*/
29+
function setupButtons()
30+
31+
/**
32+
* Initialize the squares matrix so that everyghing is walkable
33+
*/
34+
function initializeMatrix()
35+
36+
/**
37+
* Udate the matrix with the new width and height but trying to keep the elements that don't need deleting
38+
*/
39+
function updateMatrix()
40+
/**
41+
* Draw the square matrix on the canvas, according to the state of each square
42+
*/
43+
function drawMatrix()
44+
45+
/**
46+
* Draw both end and start positions
47+
*/
48+
function drawStartEnd()
49+
50+
/**
51+
* Make the selected palette follow the mouse
52+
*/
53+
function drawCurrentMouse()
54+
55+
/**
56+
* If the position is valid, apply the correct palette on the square
57+
*/
58+
function mousePressed()
59+
60+
/**
61+
* If the mouse is locked, keep drawing the current palette
62+
*/
63+
function mouseDragged()
64+
65+
/**
66+
* Unlock the mouse
67+
*/
68+
function mouseReleased()
69+
70+
/**
71+
* Check if the given vecor is inside the matrix borders
72+
* @param {p5.Vector} v - Vetor to check
73+
* @returns {boolean} Whether or not the vector is valid
74+
*/
75+
76+
function validVector(v)
77+
78+
/**
79+
* Set the start position and checks for any conflicts
80+
* @param {number} x - x coordinate of the start position
81+
* @param {number} y - y coordinate of the start position
82+
*/
83+
function setStart(x, y)
84+
85+
/**
86+
* Set the end position and checks for any conflicts
87+
* @param {number} x - x coordinate of the end position
88+
* @param {number} y - y coordinate of the end position
89+
*/
90+
function setEnd(x, y)
91+
92+
/**
93+
* If the start or end position are on this point, remove them
94+
* @param {number} x - x coordinate of the point
95+
* @param {number} y - y coordinate of the point
96+
*/
97+
function removeStartEnd(x, y)
98+
99+
/**
100+
* Start the astar algorithm animation
101+
*/
102+
function startAlgotithm()
103+
104+
/**
105+
* Clears the board
106+
*/
107+
function clearBoard()
108+
109+
/**
110+
* Set the palette with the given index {walkable, unwalkable, start, end, special_ground}
111+
* @param {string} id - id of the wanted palette
112+
*/
113+
function setPalette(id)
114+
115+
/**
116+
* Apply the changes in the settings
117+
*/
118+
function apply()
119+
120+
/**
121+
* Cancels every change in the settings
122+
*/
123+
function cancel()
124+
125+
/**
126+
Set the currentStatus value based of the chosen status
127+
* @param {string} newStatus - status to use when changing the editor mode value
128+
*/
129+
function setStatus(newStatus)
130+
```
131+
### astar.js
132+
```javascript
133+
/**
134+
* Class that handles the A* algorithm
135+
*/
136+
class Astar
137+
138+
/**
139+
* Create an istance of Astar
140+
*/
141+
constructor()
142+
143+
/**
144+
* Start the algorithm animation
145+
*/
146+
startAlgorithm()
147+
148+
/**
149+
* Clear the point matrix and the openPoints array
150+
*/
151+
clearAll()
152+
153+
/**
154+
* Inizialize the starting point
155+
*/
156+
initializeAlgorithm()
157+
158+
/**
159+
* Follows the Astar algorithm step by step. Called each frame
160+
*/
161+
stepSolution()
162+
163+
/**
164+
* Check all the neighbours of the point and, if they are valid points, calculate their f value
165+
* @param {number} x - x coordinate of the point
166+
* @param {number} y - y coordinate of the point
167+
*/
168+
checkNeighbours(x, y)
169+
170+
/**
171+
* Calculate the new F value for the point, if the g provided is less than the current g
172+
* @param {number} x - x coordinate of the point
173+
* @param {number} y - y coordinate of the point
174+
* @param {number} g - new g value calculated for the point
175+
*/
176+
calculateF(x, y, g, px, py)
177+
178+
/**
179+
* Calculate the heuristic distance from the indicated point to the end point
180+
* @param {number} x - x coordinate of the point
181+
* @param {number} y - y coordinate of the point
182+
* @returns {number} The value h
183+
*/
184+
calculateH(x, y)
185+
186+
/**
187+
* Retrace the path discovered by the Astar algorithm and color it
188+
*/
189+
retracePath()
190+
191+
/**
192+
* Retrace the all the path tried by the Astar algorithm and color it
193+
*/
194+
retraceFailPath()
195+
196+
/**
197+
* Generate a string containing all the point's info
198+
* @param {number} x - x coordinate of the point
199+
* @param {number} y - y coordinate of the point
200+
* @returns {string} - Point's info: h, g and f values
201+
*/
202+
getPointInfo(x, y)
203+
204+
/**
205+
* Draw some text on the chosen square
206+
* @param {number} x - x coordinate of the square
207+
* @param {number} y - y coordinate of the square
208+
* @param {boolean} override - whether or not the intern of the square should be recolored
209+
*/
210+
drawText(x, y, override)
211+
212+
213+
/**
214+
* Draw a stroke around the chosen square
215+
* @param {number} x - x coordinate of the square
216+
* @param {number} y - y coordinate of the square
217+
* @param {p5.color} color - color of the stroke
218+
*/
219+
drawFocus(x, y, color)
220+
221+
/**
222+
* Push the point into the openPoints array, if it is not present yet
223+
* @param {any} point - point to push in the array
224+
*/
225+
pushToOpenNodes(point)
226+
```

graphic/block.svg

Lines changed: 3 additions & 0 deletions
Loading

graphic/end.svg

Lines changed: 3 additions & 0 deletions
Loading

graphic/ground.svg

Lines changed: 3 additions & 0 deletions
Loading

graphic/special_ground.svg

Lines changed: 3 additions & 0 deletions
Loading

graphic/start.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)