-
Notifications
You must be signed in to change notification settings - Fork 7
/
grid.scad
41 lines (40 loc) · 934 Bytes
/
grid.scad
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
/*
* Arrange many different items in a grid.
*
* EXAMPLES:
*
* grid([15, 6]) {
* cube([4,5,1]);
* cylinder(r=3, h=10);
* cube([6,6,5]);
* cylinder(r=3, h=5);
* }
*
* use <connector.scad>;
* grid([30,30]) {
* connector(1);
* connector(2);
* connector(3);
* }
*
* PARAMETERS:
*
* space - a single number or an array specifying distance between children
* along x and y axes.
* center - boolean. Whether to position center of grid over origin.
*
*/
module grid(space, center=false) {
n = ceil(sqrt($children));
c = center ? 1 : 0;
spacex = (len(space) == undef) ? space : space[0];
spacey = (len(space) == undef) ? space : (
(len(space) == 1) ? space[0] : space[1] );
for (i=[0:1:n-1], j=[0:1:n-1]) {
idx = n*i+j;
if (idx < $children) {
translate([j*spacex - c*spacex*(n-1)/2, i*spacey - c*spacey*(n-1)/2, 0])
children(idx);
}
}
}