-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_canvas.js
289 lines (265 loc) · 9.12 KB
/
grid_canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//TODO:
// -bfs: geht durch obstruction durch muss behoben werden
// -animation: will nicht wirklich der canvas wird nach abschluss der ganzen berechnung neu gezeichnet
// ->kann mit einer eigenen draw-function behoben werden die mit setInterval zusammenarbeiten
// -dfs: komplett
//
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
let canvas = document.getElementById("grid_canvas")
let ctx = canvas.getContext("2d")
let grid_width = 100
let nav_height = 47
let max_width = window.innerWidth - (window.innerWidth % grid_width)
let max_height = window.innerHeight - nav_height - ((window.innerHeight - nav_height) % grid_width)
let obstruction_color = "green"
let start_point_color = "red"
let end_point_color = "blue"
let start_point_already_set = false
let startpoint = new Array()
let end_point_already_set = false
let field_id_counter = 0
console.log(window.innerHeight)
console.log(window.innerWidth)
console.log(window.innerHeight % grid_width)
console.log(window.innerWidth % grid_width)
let painting = false
canvas.height = max_height
canvas.width = max_width
//finished and tested
function start_drawing_obstruction(){
painting = true
}
//finished and tested
function stop_drawing_obstruction(){
painting = false
}
//finished and tested
function draw_obstruction(e){
if(!painting){
return
}
console.log(e.clientX, e.clientY)
console.log(fields)
posX = (e.clientX - (e.clientX % grid_width)) / grid_width
posY = (e.clientY - nav_height - ((e.clientY - nav_height) % grid_width)) / grid_width
fields[posY][posX].obstruction = true
fields[posY][posX].color = obstruction_color
fields[posY][posX].draw(ctx)
}
//finished and tested
function drawGrid(X, Y, grid_width, color){
ctx.beginPath()
ctx.strokeStyle = color
ctx.fillStyle = color
ctx.fillRect(X, Y, grid_width, grid_width)
ctx.stroke()
ctx.closePath()
}
//finished and testd
function set_nav_height(){
nav = document.querySelector(".navbar")
nav_height = nav.clientHeight
}
//finished and tested
function set_start_and_end_point(e){
if(start_point_already_set && end_point_already_set) {
return
}
if(start_point_already_set && !end_point_already_set){
posX = (e.clientX - (e.clientX % grid_width)) / grid_width
posY = (e.clientY - nav_height - ((e.clientY - nav_height) % grid_width)) / grid_width
fields[posY][posX].end_point = true
fields[posY][posX].color = end_point_color
fields[posY][posX].draw(ctx)
//drawGrid(posX, posY, grid_width, end_point_color)
end_point_already_set = true
}
if(!start_point_already_set && !end_point_already_set){
posX = (e.clientX - (e.clientX % grid_width)) / grid_width
posY = (e.clientY - nav_height - ((e.clientY - nav_height) % grid_width)) / grid_width
console.log("posX")
console.log(posX)
console.log(posY)
fields[posY][posX].start_point = true
fields[posY][posX].color = start_point_color
fields[posY][posX].draw(ctx)
startpoint = [posY,posX]
start_point_already_set = true
}
}
//finished
class Field{
constructor(x_pos, y_pos, color){
this.x_pos = x_pos
this.y_pos = y_pos
this.color = color
this.id = field_id_counter++
this.start_point = false
this.end_point = false
this.obstruction = false
this.visited = false
}
draw(context){
context.fillStyle = this.color
context.strokeStyle ="#55555555"
context.fillRect(this.x_pos, this.y_pos, grid_width, grid_width)
context.strokeRect(this.x_pos, this.y_pos, grid_width, grid_width)
}
}
//------------------------------------------------------------------
//geht irgendwie schief kann aber auch an der neighbour queue kombi liegen die in bfs verwendet wird
function check_if_field_is_obstrruction(fields, list){
for(i = 0; i < list.length; i++){
k = list[i][0]
l = list[i][1]
if(fields[k][l].obstruction){
list.splice(i, 1)
}
}
return list
}
//finished and tested
//top-bottom-left-to-right
function get_neighbours(fields, i, j){
if(i == 0){
if(j == 0){
return [[i + 1, j], [i, j + 1]]
}
else if(j == fields[0].length - 1){
return [[i, j - 1], [i + 1, j]]
}
else{
return [[i, j - 1], [i + 1, j], [i, j + 1]]
}
}
else if(i == fields.length - 1){
if(j == 0){
return [[i - 1, j], [i, j + 1]]
}
else if(j == fields[0].length - 1){
return [[i, j - 1], [i - 1, j]]
}
else{
return [[i, j - 1], [i - 1, j], [i, j + 1]]
}
}
else{
if(j == 0){
return [[i - 1, j], [i, j + 1], [i + 1, j]]
}
else if(j == fields[0].length - 1){
return [[i, j - 1], [i - 1, j], [i + 1, j]]
}
else{
return [[i, j - 1], [i - 1, j], [i, j + 1], [i+1, j]]
}
}
}
var wait = (ms) => {
const start = Date.now();
let now = start;
while (now - start < ms) {
now = Date.now();
}
}
//-----------------------------------------------------------------------------
function bfs(fields, startpoint){
let animation_queue = new Array()
var i = startpoint[0]
var j = startpoint[1]
var queue = [[i,j]]
animation_queue = [[i,j]]
do{
next = queue[0]
i = next[0]
j = next[1]
queue = queue.slice(1)
neighbours = get_neighbours(fields, i, j)
neighbours = check_if_field_is_obstrruction(fields, neighbours)
neigbours = delete_already_visited_fields(fields, neighbours)
queue = [...queue,...neigbours]
animation_queue = [...animation_queue,...neigbours]
fields[i][j].visited = true
fields[i][j].color = 'pink'
//fields[i][j].draw(ctx)
ctx.clearRect(0,0, canvas.width, canvas.height)
wait(200)
for(k = 0; k < fields.length; k++){
for(l = 0; l < fields[0].length; l++){
console.log(k)
console.log(l)
fields[k][l].draw(ctx)
}
}
queue = [...eliminate_duplicates([...queue])]
console.log(queue)
}while(queue.length > 0 && !fields[i][j].end_point)
}
//----------------------------------------------------------------------------------------------
//finished
//-------------------------------------------------------------------------------------------------
function eliminate_duplicates(arr){
arr = arr.map(JSON.stringify).filter((el, i, ar)=> i === ar.indexOf(el)).map(JSON.parse)
return arr
}
//--------------------------------------------------------------------------------------------
function delete_already_visited_fields(fields, list){
var not_visited_fields = new Array()
for(i = 0; i < list.length; i++){
if(!fields[list[i][0]][list[i][1]].visited){
not_visited_fields.push([list[i][0],list[i][1]])
}
}
return not_visited_fields
}
function dfs(fields, startpoint){
var i = startpoint[0]
var j = startpoint[1]
var queue = [[i,j]]
do{
next = queue[0]
i = next[0]
j = next[1]
queue = queue.slice(1)
neighbours = get_neighbours(fields, i, j)
neighbours = check_if_field_is_obstrruction(fields, neighbours)
neigbours = delete_already_visited_fields(fields, neighbours)
queue = [...neigbours,...queue]
fields[i][j].visited = true
fields[i][j].color = 'pink'
fields[i][j].draw(ctx)
queue = [...eliminate_duplicates([...queue])]
console.log(queue)
}while(queue.length > 0 && !fields[i][j].end_point)
}
function draw(){
for(i = 0; i < fields.length; i++){
for(j = 0; j < fields[0].length; j++){
fields[i][j].draw(ctx)
}
}
}
//main:
var fields = new Array((max_height - (max_height % grid_width)) / grid_width )
for(i = 0; i < fields.length; i++){
fields[i] = new Array((max_width - (max_width % grid_width)) / grid_width)
}
console.log(max_height / grid_width)
for(i = 0; i < max_height / grid_width; i++){
for(j = 0; j < max_width / grid_width; j++){
fields[i][j] = new Field(j * grid_width, i * grid_width, "black")
}
}
console.log(fields)
draw()
canvas.addEventListener("mousedown", start_drawing_obstruction)
canvas.addEventListener("mouseup", stop_drawing_obstruction)
canvas.addEventListener("mousemove", draw_obstruction)
window.addEventListener("resize", set_nav_height)
window.addEventListener("DOMContentLoaded", set_nav_height)
window.addEventListener("dblclick", set_start_and_end_point)
const button = document.getElementById('Run')
button.addEventListener("click",(event) => bfs(fields,startpoint))