-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (80 loc) · 2.38 KB
/
index.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
const Bitfield = require('bitfield').default;
const ndarray = require('ndarray');
const rcolor = require('rcolor');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 600;
canvas.height = 600;
document.body.appendChild(canvas);
// minimum width and height should divide evenly into the width (no remainders)
const minimumWidth = canvas.width / 8; // 75
const minimumHeight = canvas.height / 8; // 75
const maximumWidth = minimumWidth * 3;
const maximumHeight = minimumHeight * 2;
// use an even number here
const gap = 8;
let remainingArea = canvas.width * canvas.height;
const filled = ndarray(new Bitfield(canvas.width * canvas.height), [
canvas.width,
canvas.height
]);
function findEmptyPosition() {
for (let x = 0; x < canvas.width; x += 1) {
for (let y = 0; y < canvas.height; y += 1) {
if (!filled.get(x, y)) {
return { x, y };
}
}
}
}
function getRemainingWidth(x, y) {
let width = 0;
while (x < canvas.width && !filled.get(x, y)) {
width += 1;
x += 1;
}
return width;
}
function getRemainingHeight(x, y) {
let height = 0;
while (y < canvas.height && !filled.get(x, y)) {
height += 1;
y += 1;
}
return height;
}
function getWidth(remainingWidth) {
const width = Math.min(
(Math.floor(Math.random() * (remainingWidth / minimumWidth - 1)) + 1) *
minimumWidth,
maximumWidth
);
return width;
}
function getHeight(remainingHeight) {
const height = Math.min(
(Math.floor(Math.random() * (remainingHeight / minimumHeight - 1)) +
1) *
minimumHeight,
maximumHeight
);
return height;
}
function plotRectangle(x, y, width, height) {
for (let i = x; i < x + width; i += 1) {
for (let j = y; j < y + height; j += 1) {
filled.set(i, j, true);
}
}
ctx.fillStyle = rcolor();
ctx.fillRect(x + gap / 2, y + gap / 2, width - gap / 2, height - gap / 2);
}
while (remainingArea > 0) {
const { x, y } = findEmptyPosition();
const remainingWidth = getRemainingWidth(x, y);
const remainingHeight = getRemainingHeight(x, y);
const width = getWidth(remainingWidth);
const height = getHeight(remainingHeight);
remainingArea -= width * height;
plotRectangle(x, y, width, height);
}