-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuckyball.js
183 lines (169 loc) · 4.85 KB
/
buckyball.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(function() {
var φ = 1.618033988749895,
ρ = 180 / Math.PI;
var vertices = [
[1,φ,0], [-1,φ,0], [1,-φ,0], [-1,-φ,0],
[0,1,φ], [0,-1,φ], [0,1,-φ], [0,-1,-φ],
[φ,0,1], [-φ,0,1], [φ,0,-1], [-φ,0,-1]
];
var faces = [
[0,1,4], [1,9,4], [4,9,5], [5,9,3], [2,3,7],
[3,2,5], [7,10,2], [0,8,10], [0,4,8], [8,2,10],
[8,4,5], [8,5,2], [1,0,6], [11,1,6], [3,9,11],
[6,10,7], [3,11,7], [11,6,7], [6,0,10], [9,1,11]
].map(function(face) {
return face.map(function(i) {
return vertices[i];
});
});
// orderd cycles of neighbors for each vertex
var pentIdx = [
[ [ 1, 4 ], [ 4, 8 ], [ 8, 10 ], [ 10, 6 ], [ 6, 1 ] ],
[ [ 0, 4 ], [ 4, 9 ], [ 9, 11 ], [ 11, 6 ], [ 6, 0 ] ],
[ [ 3, 7 ], [ 7, 10 ], [ 10, 8 ], [ 8, 5 ], [ 5, 3 ] ],
[ [ 5, 9 ], [ 9, 11 ], [ 11, 7 ], [ 7, 2 ], [ 2, 5 ] ],
[ [ 0, 1 ], [ 1, 9 ], [ 9, 5 ], [ 5, 8 ], [ 8, 0 ] ],
[ [ 4, 9 ], [ 9, 3 ], [ 3, 2 ], [ 2, 8 ], [ 8, 4 ] ],
[ [ 1, 0 ], [ 0, 10 ], [ 10, 7 ], [ 7, 11 ], [ 11, 1 ] ],
[ [ 2, 3 ], [ 3, 11 ], [ 11, 6 ], [ 6, 10 ], [ 10, 2 ] ],
[ [ 0, 10 ], [ 10, 2 ], [ 2, 5 ], [ 5, 4 ], [ 4, 0 ] ],
[ [ 1, 4 ], [ 4, 5 ], [ 5, 3 ], [ 3, 11 ], [ 11, 1 ] ],
[ [ 7, 2 ], [ 2, 8 ], [ 8, 0 ], [ 0, 6 ], [ 6, 7 ] ],
[ [ 1, 6 ], [ 6, 7 ], [ 7, 3 ], [ 3, 9 ], [ 9, 1 ] ]
];
function pentify(neighbors,idx){
console.log(neighbors.length,idx);
var face = neighbors.map(function(edgeIdx){
return interpolate(vertices[idx],vertices[edgeIdx[0]])(1/3)
});
return face;
}
function hexify(face,idx){
console.log(face.length,idx);
var ff=[]
ff.push(interpolate(face[0],face[1])(1/3));
ff.push(interpolate(face[0],face[1])(2/3));
ff.push(interpolate(face[1],face[2])(1/3));
ff.push(interpolate(face[1],face[2])(2/3));
ff.push(interpolate(face[2],face[0])(1/3));
ff.push(interpolate(face[2],face[0])(2/3));
return ff;
}
d3.geodesic = {
hexagons: function() {
return {
type: "MultiPolygon",
coordinates: faces.map(hexify).map(function(face,idx) {
face = face.map(project);
face.push(face[0]);
return [face];
})
};
},
pentagons: function() {
return {
type: "MultiPolygon",
coordinates: pentIdx.map(pentify).map(function(face,idx) {
face = face.map(project);
face.push(face[0]);
return [face];
})
};
},
multipolygon: function(n) {
return {
type: "MultiPolygon",
coordinates: subdivideFaces(~~n).map(function(face) {
face = face.map(project);
face.push(face[0]);
return [face];
})
};
},
polygons: function(n) {
return d3.geodesic.multipolygon(~~n).coordinates.map(function(face) {
return {type: "Polygon", coordinates: face};
});
},
multilinestring: function(n) {
return {
type: "MultiLineString",
coordinates: subdivideEdges(~~n).map(function(edge) {
return edge.map(project);
})
};
}
};
function subdivideFaces(n) {
return d3.merge(faces.map(function(face) {
var i01 = interpolate(face[0], face[1]),
i02 = interpolate(face[0], face[2]),
faces = [];
faces.push([
face[0],
i01(1 / n),
i02(1 / n)
]);
for (var i = 1; i < n; ++i) {
var i1 = interpolate(i01(i / n), i02(i / n)),
i2 = interpolate(i01((i + 1) / n), i02((i + 1) / n));
for (var j = 0; j <= i; ++j) {
faces.push([
i1(j / i),
i2(j / (i + 1)),
i2((j + 1) / (i + 1))
]);
}
for (var j = 0; j < i; ++j) {
faces.push([
i1(j / i),
i1((j + 1) / i),
i2((j + 1) / (i + 1))
]);
}
}
return faces;
}));
}
function subdivideEdges(n) {
var edges = {};
subdivideFaces(n).forEach(function(face) {
add(face[0], face[1]);
add(face[1], face[2]);
add(face[2], face[0]);
});
function add(p0, p1) {
var t;
if (p0[0] < p1[0] || (p0[0] == p1[0] && (p0[1] < p1[1] || (p0[1] == p1[1] && p0[2] < p1[2])))) t = p0, p0 = p1, p1 = t;
edges[p0.map(round) + " " + p1.map(round)] = [p0, p1];
}
function round(d) {
return d3.round(d, 4);
}
return d3.values(edges);
}
function interpolate(p0, p1) {
var x0 = p0[0],
y0 = p0[1],
z0 = p0[2],
x1 = p1[0] - x0,
y1 = p1[1] - y0,
z1 = p1[2] - z0;
return function(t) {
return [
x0 + t * x1,
y0 + t * y1,
z0 + t * z1
];
};
}
function project(p) {
var x = p[0],
y = p[1],
z = p[2];
return [
Math.atan2(y, x) * ρ,
Math.acos(z / Math.sqrt(x * x + y * y + z * z)) * ρ - 90
];
}
})();