aboutsummaryrefslogtreecommitdiff
path: root/lib/dice.scad
blob: 92eccbb85be71567994da14d91465b696e31e58b (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use <utils.scad>

// Tetrahedron inscribed in a sphere of radius r
module tetrahedron(r) {
    c = r*2;
    intersection() {
        translate([0, 0, r-r/3]) cube(c, center=true);
        intersection_for ( i = [0:2] ) {
            rotate([0, 0, 120*i]) rotate([acos(1/3)+180, 0, 0])
                translate([0, 0, r-r/3]) cube(c, center=true);
        }
    }
}

module tetrahedron_faces(r, sc_face=0.6) {
    union() {
        rotate([180, 0, 180]) translate([0, 0, r/3]) scale([sc_face, sc_face, 1]) children(0);
        rotate([acos(1/3)+180, 0, 0]) rotate([180, 0, 0]) translate([0, 0, r/3]) scale([sc_face, sc_face, 1]) children(1);
        rotate([0, 0, 120]) rotate([acos(1/3)+180, 0, 0]) rotate([180, 0, 0]) translate([0, 0, r/3]) scale([sc_face, sc_face, 1]) children(2);
        rotate([0, 0, 240]) rotate([acos(1/3)+180, 0, 0]) rotate([180, 0, 0]) translate([0, 0, r/3]) scale([sc_face, sc_face, 1]) children(3);
    }
}

module d4(size=false, side=false) {
    l = side ? side : s2s_d4(size);
    if (l == false) {
        echo("ERROR: d4: one of side or size should be defined");
    }
    r = sqrt(6)/4*l;
    difference() {
        intersection() {
            sphere(r=r*0.79, $fn=64);
            tetrahedron(r);
        }
    }
}

module cube_faces(l) {
    union() {
        translate([0, 0, l/2]) children(0);
        rotate([90, 0, 0]) translate([0, 0, l/2]) children(1);
        rotate([0, 90, 0]) translate([0, 0, l/2]) children(2);
        rotate([-90, 0, 0]) translate([0, 0, l/2]) children(3);
        rotate([0, -90, 0]) translate([0, 0, l/2]) children(4);
        rotate([180, 0, 0]) translate([0, 0, l/2]) children(5);
    }
}

module d6(size=false, side=false) {
    l = side ? side : s2s_d6(size);
    if (l == false) {
        echo("ERROR: d6: one of side or size should be defined");
    }
    difference() {
        intersection() {
            cube(l, center=true);
            sphere(l*0.79);
        }
    }
}

// Octahedron inscribed in a sphere of radius r
module octahedron(r) {
    c = r*2;
    i_r = r / sqrt(3);
    intersection() {
        cube([c, c, i_r*2], center=true);
        intersection_for ( i = [0:2] ) {
            rotate([0, 0, 120*i]) rotate([acos(-1/3), 0, 0]) cube([c, c, i_r*2], center=true);
        }
    }
}

module octahedron_faces(r, sc_face=0.6) {
    c = r*2;
    i_r = r / sqrt(3);
    union() {
        translate([0, 0, i_r]) rotate([0, 0, 180]) scale([sc_face, sc_face, 1]) children(0);
        rotate([acos(-1/3), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(4);
        rotate([0, 0, 120]) rotate([acos(-1/3)+180, 0, 0]) translate([0, 0, i_r]) rotate([0, 0, 120]) scale([sc_face, sc_face, 1]) children(6);
        rotate([0, 0, -120]) rotate([acos(-1/3)+180, 0, 0]) rotate([0, 0, 240]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(2);
        rotate([0, 0, -120]) rotate([acos(-1/3), 0, 0]) rotate([0, 0, 120]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(5);
        rotate([0, 0, 120]) rotate([acos(-1/3), 0, 0]) rotate([0, 0, 240]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(1);
        rotate([acos(-1/3)+180, 0, 0]) translate([0, 0, i_r])scale([sc_face, sc_face, 1]) children(3);
        rotate([180, 0, 0]) translate([0, 0, i_r])scale([sc_face, sc_face, 1]) children(7);
    }
}

module d8(size=false, side=false) {
    l = side ? side : s2s_d8(size);
    if (l == false) {
        echo("ERROR: d8: one of side or size should be defined");
    }
    r = sqrt(2)/2*l;
    intersection() {
        sphere(r*0.86, $fn=64);
        octahedron(r);
    }
}

// Dodecahedron inscribed in a sphere of radius r
module dodecahedron(r) {
    c = r*2;
    l = r * 4 / (sqrt(3) * (1+sqrt(5)));
    i_r = (l/2)*sqrt(5/2 + 11*sqrt(5)/10);
    intersection() {
        cube([c, c, i_r*2], center=true);
        intersection_for ( i = [0:4] ) {
            rotate([0, 0, 72*i]) rotate([acos(-1/sqrt(5)), 0, 0]) cube([c, c, i_r*2], center=true);
        }
    }
}

module dodecahedron_faces(r, sc_face=1.1) {
    c = r*2;
    l = r * 4 / (sqrt(3) * (1+sqrt(5)));
    echo(l);
    i_r = (l/2)*sqrt(5/2 + 11*sqrt(5)/10);
    union() {
        translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(0);
        rotate([acos(-1/sqrt(5))+180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(1);
        rotate([0, 0, 72]) rotate([acos(-1/sqrt(5))+180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(3);
        rotate([0, 0, 144]) rotate([acos(-1/sqrt(5))+180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(5);
        rotate([0, 0, 216]) rotate([acos(-1/sqrt(5))+180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(4);
        rotate([0, 0, 288]) rotate([acos(-1/sqrt(5))+180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(2);

        rotate([acos(-1/sqrt(5)), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(10);
        rotate([0, 0, 72]) rotate([acos(-1/sqrt(5)), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(8);
        rotate([0, 0, 144]) rotate([acos(-1/sqrt(5)), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(6);
        rotate([0, 0, 216]) rotate([acos(-1/sqrt(5)), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(7);
        rotate([0, 0, 288]) rotate([acos(-1/sqrt(5)), 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(9);
        rotate([180, 0, 0]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(11);
    }
}

module d12(size=false, side=false) {
    l = side ? side : s2s_d12(size);
    if (l == false) {
        echo("ERROR: d12: one of side or size should be defined");
    }
    r = l*sqrt(3)*(1+sqrt(5))/4;
    intersection() {
        sphere(r*0.96, $fn=64);
        dodecahedron(r);
    }
}

module icosahedron(r) {
    l = r*4/(sqrt(10+2*sqrt(5)));
    echo(r);
    echo(l);
    c = r*2;
    i_r = (l/12)*sqrt(3)*(3+sqrt(5));
    a_b2 = atan(1/2) + acos(-sqrt(5)/3)/2;
    phi = (1+sqrt(5))/2;
    rotate([0, -acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) 
        intersection() {
            rotate([0, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) cube([c, c, i_r*2], center=true);
            mirror([1, 0, 0]) rotate([0, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) cube([c, c, i_r*2], center=true);
            rotate([0, acos(1/sqrt(3)), -45]) cube([c, c, i_r*2], center=true);
            mirror([1, 0, 0]) rotate([0, acos(1/sqrt(3)), -45]) cube([c, c, i_r*2], center=true);
            mirror([0, 1, 0]) rotate([0, acos(1/sqrt(3)), -45]) cube([c, c, i_r*2], center=true);
            mirror([0, 1, 0]) mirror([1, 0, 0]) rotate([0, acos(1/sqrt(3)), -45]) cube([c, c, i_r*2], center=true);
            rotate([90, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) cube([c, c, i_r*2], center=true);
            mirror([0, 0, 1]) rotate([90, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) cube([c, c, i_r*2], center=true);
            rotate([0, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) cube([c, c, i_r*2], center=true);
            mirror([0, 1, 0]) rotate([0, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) cube([c, c, i_r*2], center=true);
        }
}

module icosahedron_faces(r, sc_face=0.5) {
    l = r*4/(sqrt(10+2*sqrt(5)));
    echo(r);
    echo(l);
    c = r*2;
    i_r = (l/12)*sqrt(3)*(3+sqrt(5));
    a_b2 = atan(1/2) + acos(-sqrt(5)/3)/2;
    phi = (1+sqrt(5))/2;
    rotate([0, -acos((-(1+sqrt(5)))/(2*sqrt(3))), 0])
    union() {
        rotate([0, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) translate([0, 0, i_r]) rotate([0, 0, 90]) scale([sc_face, sc_face, 1]) children(0);
        rotate([180, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) translate([0, 0, i_r]) rotate([0, 0, 270]) scale([sc_face, sc_face, 1]) children(19);

         mirror([1, 0, 0]) rotate([0, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) translate([0, 0, i_r]) mirror([0, 1, 0]) rotate([0, 0, 90]) scale([sc_face, sc_face, 1]) children(12);
         mirror([1, 0, 0]) rotate([180, acos((-(1+sqrt(5)))/(2*sqrt(3))), 0]) translate([0, 0, i_r]) mirror([0, 1, 0]) rotate([0, 0, 90]) scale([sc_face, sc_face, 1]) children(7);

         rotate([0, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(9);
         rotate([180, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) scale([sc_face, sc_face, 1]) children(10);

         mirror([1, 0, 0]) rotate([0, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) mirror([1, 0, 0]) scale([sc_face, sc_face, 1]) children(1);
         mirror([1, 0, 0]) rotate([180, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) mirror([1, 0, 0]) scale([sc_face, sc_face, 1]) children(18);

         mirror([0, 1, 0]) rotate([0, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) mirror([0, 1, 0]) rotate([0, 0, 180]) scale([sc_face, sc_face, 1]) children(15);
         mirror([0, 1, 0]) rotate([180, acos(1/sqrt(3)), -45]) translate([0, 0, i_r]) mirror([0, 1, 0]) rotate([0, 0, 180]) scale([sc_face, sc_face, 1]) children(4);

         mirror([0, 1, 0]) mirror([1, 0, 0]) rotate([0, acos(1/sqrt(3)), -45])  translate([0, 0, i_r]) mirror([0, 1, 0]) mirror([0, 1, 0]) scale([sc_face, sc_face, 1]) children(13);
         mirror([0, 1, 0]) mirror([1, 0, 0]) rotate([180, acos(1/sqrt(3)), -45])  translate([0, 0, i_r]) mirror([0, 1, 0]) mirror([0, 1, 0]) scale([sc_face, sc_face, 1]) children(6);

         rotate([90, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) translate([0, 0, i_r]) rotate([0, 0, 270]) scale([sc_face, sc_face, 1]) children(11);
         rotate([270, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) translate([0, 0, i_r]) rotate([0, 0, 270]) scale([sc_face, sc_face, 1]) children(8);

         mirror([0, 0, 1]) rotate([90, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) translate([0, 0, i_r]) mirror([1, 0, 0]) rotate([0, 0, 90]) scale([sc_face, sc_face, 1]) children(14);
         mirror([0, 0, 1]) rotate([270, 0, 0]) rotate([0, acos((sqrt(5)+1)/(2*sqrt(3))), 90]) translate([0, 0, i_r]) mirror([1, 0, 0]) rotate([0, 0, 90]) scale([sc_face, sc_face, 1]) children(5);

         rotate([0, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) translate([0, 0, i_r]) rotate([0, 0, 180]) scale([sc_face, sc_face, 1]) children(16);
         rotate([180, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) translate([0, 0, i_r]) rotate([0, 0, 180]) scale([sc_face, sc_face, 1]) children(3);

         mirror([0, 1, 0]) rotate([0, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) translate([0, 0, i_r]) mirror([0, 1, 0]) scale([sc_face, sc_face, 1]) children(2);
         mirror([0, 1, 0]) rotate([180, 90, atan((-sqrt(5)+1)/(sqrt(5)+1))]) translate([0, 0, i_r]) mirror([0, 1, 0]) scale([sc_face, sc_face, 1]) children(17);
    }
}

module d20(size=false, side=false) {
    l = side ? side : s2s_d20(size);
    if (l == false) {
        echo("ERROR: d20: one of side or size should be defined");
    }
    r = l*sqrt(10+2*sqrt(5))/4;
    intersection() {
        sphere(r*0.96, $fn=64);
        icosahedron(r);
    }
}