-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
109 lines (89 loc) · 2.64 KB
/
common.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
const drawWalls = cell => {
stroke(255)
const s = cell.size
// if (!cell.connections.top) {
// line(cell.x, cell.y, cell.x + s, cell.y)
// }
if (!cell.connections.right) {
line(cell.x + s, cell.y, cell.x + s, cell.y + s)
}
if (!cell.connections.bottom) {
line(cell.x + s, cell.y + s, cell.x, cell.y + s)
}
// if (!cell.connections.left) {
// line(cell.x, cell.y + s, cell.x, cell.y)
// }
}
const colorCell = (rgb, cell) => {
noStroke()
fill(...rgb, 100)
rect(cell.x, cell.y, cell.size, cell.size)
}
const createCells = (cellSize, nColumns, nRows) => {
const cells = []
for (let row = 0; row < nRows; row++) {
for (let column = 0; column < nColumns; column++) {
cells.push({
column,
row,
visited: false,
size: cellSize,
connections: {},
x: column * cellSize,
y: row * cellSize
})
}
}
return cells
}
const connect = (a, b) => {
if (a.column > b.column) {
a.connections.left = true
b.connections.right = true
} else if (a.column < b.column) {
a.connections.right = true
b.connections.left = true
} else if (a.row > b.row) {
a.connections.top = true
b.connections.bottom = true
} else {
a.connections.bottom = true
b.connections.top = true
}
}
const getCell = (grid, column, row) => {
if (column < 0 || row < 0) {
return null
}
if (column >= grid.nColumns || row >= grid.nRows) {
return null
}
return grid.cells[column + row * grid.nColumns]
}
const getNeighbours = (grid, cell) => {
return [
getCell(grid, cell.column, cell.row - 1),
getCell(grid, cell.column + 1, cell.row),
getCell(grid, cell.column, cell.row + 1),
getCell(grid, cell.column - 1, cell.row),
].filter(x => x !== null)
}
const pickRandom = arr => {
if (arr.length === 0) {
return null
}
return arr[floor(random(0, arr.length))]
}
const pickMiddle = grid => {
return getCell(grid, floor(grid.nColumns / 2), floor(grid.nRows / 2))
}
const createGrid = () => {
const url = new URL(window.location.href)
const cellSize = parseInt(url.searchParams.get('cell-size'))
const width = document.documentElement.clientWidth
const height = window.innerHeight
const nColumns = floor(width / cellSize)
const nRows = floor(height / cellSize)
createCanvas(nColumns * cellSize, nRows * cellSize)
return { cells: createCells(cellSize, nColumns, nRows), nRows, nColumns }
}