-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivider.js
44 lines (37 loc) · 1.14 KB
/
divider.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
class RectData {
constructor(_x, _y, _w, _h) {
this.x = _x;
this.y = _y;
this.w = _w;
this.h = _h;
}
}
let DIVIDE_CHANCE = 0.9;
let MAX_DEPTH = 6;
let MIN_DEPTH = 3;
function subdivideRect(_x, _y, _w, _h, _depth) {
let isSplit = random() < DIVIDE_CHANCE;
if (_depth >= MAX_DEPTH)
isSplit = false;
if (_depth < MIN_DEPTH)
isSplit = true;
if (isSplit) {
let splitRatio = random(0.2, 0.8);
let isSplitLeftRight = (_w > _h);
// split left right
if (isSplitLeftRight) {
let rectAs = subdivideRect(_x, _y, _w * splitRatio, _h, _depth + 1);
let rectBs = subdivideRect(_x + _w * splitRatio, _y, _w * (1 - splitRatio), _h, _depth + 1);
return rectAs.concat(rectBs);
}
// split top bottom
else {
let rectAs = subdivideRect(_x, _y, _w, _h * splitRatio, _depth + 1);
let rectBs = subdivideRect(_x, _y + _h * splitRatio, _w, _h * (1 - splitRatio), _depth + 1);
return rectAs.concat(rectBs);
}
}
else {
return [new RectData(_x, _y, _w, _h)];
}
}