-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
39 lines (32 loc) · 1.19 KB
/
main.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
function gen() {
const dig = document.getElementById("dig").value;
const maxgrid = document.getElementById("maxgrid").value;
const result = generateStepPattern(Number(dig), Number(maxgrid));
document.getElementById("output").textContent = result.join(", ");
document.getElementById("grid").innerHTML = "";
let pos = 0;
let i = 0;
let bef = `<div>${"<div class=\"f\"></div>".repeat(maxgrid)}</div>`
for (const e of result) {
i++;
pos += e;
const width = e;
if (width !== 0) {
bef = `<div>${"<div class=\"f\"></div>".repeat(pos - width)}${"<div class=\"t\"></div>".repeat(width)}${"<div class=\"f\"></div>".repeat(maxgrid - pos)}</div>`
}
document.getElementById("grid").insertAdjacentHTML("beforeend", bef);
}
}
function generateStepPattern(angleDeg, maxgrid) {
const angleRad = angleDeg * (Math.PI / 180);
const slope = Math.tan(angleRad);
const stepPattern = [];
let cumulativeY = 0;
let x = 0;
while (x <= maxgrid) {
cumulativeY += slope;
stepPattern.push(Math.abs(Math.round(cumulativeY) - Math.round(cumulativeY - slope)));
x++;
}
return stepPattern;
}