-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (110 loc) · 3.79 KB
/
Copy pathscript.js
File metadata and controls
129 lines (110 loc) · 3.79 KB
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
function createSpiralMatrix(rows, cols) {
let matrix = Array.from({ length: rows }, () => Array(cols).fill(' '));
let top = 0;
let bottom = rows - 1;
let left = 0;
let right = cols - 1;
let num = 1;
while (top <= bottom && left <= right) {
// Traverse from left to right along the top row
for (let i = left; i <= right; i++) {
matrix[top][i] = num++;
}
top++;
// Traverse from top to bottom along the right column
for (let i = top; i <= bottom; i++) {
matrix[i][right] = num++;
}
right--;
// Traverse from right to left along the bottom row
if (top <= bottom) {
for (let i = right; i >= left; i--) {
matrix[bottom][i] = num++;
}
bottom--;
}
// Traverse from bottom to top along the left column
if (left <= right) {
for (let i = bottom; i >= top; i--) {
matrix[i][left] = num++;
}
left++;
}
}
return matrix;
}
function displayMatrix(matrix) {
const table = document.getElementById('spiralMatrix');
table.innerHTML = '';
for (let row of matrix) {
const tr = document.createElement('tr');
for (let cell of row) {
const td = document.createElement('td');
td.textContent = cell;
tr.appendChild(td);
}
table.appendChild(tr);
}
}
function animateMatrix(matrix, speed) {
const table = document.getElementById('spiralMatrix');
const cells = table.getElementsByTagName('td');
const totalCells = matrix.length * matrix[0].length;
let order = [];
let top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
// Traverse from left to right along the top row
for (let i = left; i <= right; i++) {
order.push([top, i]);
}
top++;
// Traverse from top to bottom along the right column
for (let i = top; i <= bottom; i++) {
order.push([i, right]);
}
right--;
// Traverse from right to left along the bottom row
if (top <= bottom) {
for (let i = right; i >= left; i--) {
order.push([bottom, i]);
}
bottom--;
}
// Traverse from bottom to top along the left column
if (left <= right) {
for (let i = bottom; i >= top; i--) {
order.push([i, left]);
}
left++;
}
}
let index = 0;
function showNext() {
if (index < totalCells) {
const [row, col] = order[index];
cells[row * matrix[0].length + col].classList.add('appear');
index++;
setTimeout(showNext, speed); // Use speed input here
} else {
setTimeout(() => {
for (let cell of cells) {
cell.classList.add('appear');
}
table.classList.add('blink');
}, 500);
}
}
showNext();
}
function generateSpiral() {
const rows = parseInt(document.getElementById('rows').value);
const cols = parseInt(document.getElementById('cols').value);
const speed = parseInt(document.getElementById('speed').value);
if (isNaN(rows) || isNaN(cols) || rows <= 0 || cols <= 0 || isNaN(speed) || speed <= 0) {
alert('Please enter valid positive numbers for rows, columns, and speed.');
return;
}
const spiralMatrix = createSpiralMatrix(rows, cols);
displayMatrix(spiralMatrix);
animateMatrix(spiralMatrix, speed);
}