-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSquareOctagonTiling.js
59 lines (46 loc) · 1.29 KB
/
SquareOctagonTiling.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
function SquareOctagonTiling(r) {
this.polys = [];
this.buildOctCell = function(x, y) {
var sides = 8;
var p = new Polygon(sides);
var inc = (2 * Math.PI) / sides;
for (var i = 0; i < sides; i++) {
var ang = (i * inc) - inc / 2;
var vX = x + r * Math.cos(ang);
var vY = y + r * Math.sin(ang);
p.addVertex(vX, vY);
}
p.close();
this.polys.push(p);
}
this.buildSquareCell = function(x, y) {
var sides = 4;
var p = new Polygon(sides);
var inc = (2 * Math.PI) / sides;
var rr = 2 * r * sin(Math.PI / 8) * Math.cos(Math.PI / 4);
for (var i = 0; i < sides; i++) {
var ang = (i * inc) - inc / 2;
var vX = x + rr * Math.cos(ang);
var vY = y + rr * Math.sin(ang);
p.addVertex(vX, vY);
}
p.close();
this.polys.push(p);
}
this.buildGrid = function() {
var h = r * 2;
var wo = r * Math.cos(Math.PI / 8);
var ws = 2 * r * sin(Math.PI / 8);
var hInc = h;
var row = 0;
for (var y = h / 2; y < height; y += (wo + ws / 2)) {
var startX = wo;
var col = (row % 2 == 0) ? 0 : 1;
for (var x = startX; x < width; x += (wo + ws / 2)) {
((col % 2) == 0) ? this.buildOctCell(x, y): this.buildSquareCell(x, y);
col++;
}
row++;
}
}
}