plato.scad with syntax coloring

This HTML file was generated with Kalle's syntaxcolor.py


   1// the non-built-in Platonic solids: regular tetra-, octa-, dodeca- and icosahedron
   2// by Kalle (http://qalle.net)
   3
   4module tetrahedron(e) {
   5    // regular tetrahedron (four faces); e = edge length
   6    // http://en.wikipedia.org/wiki/Tetrahedron
   7    // http://en.wikipedia.org/wiki/Equilateral_triangle
   8
   9    // radius of circle circumscribed in faces
  10    ccr = sqrt(3) / 3 * e;
  11
  12    // radius of circle inscribed in faces
  13    icr = sqrt(3) / 6 * e;
  14
  15    // radius of sphere circumscribed in tetrahedron
  16    csr = sqrt(6) / 4 * e;
  17
  18    // radius of sphere inscribed in tetrahedron
  19    isr = sqrt(6) / 12 * e;
  20
  21    polyhedron(
  22        // vertices
  23        [
  24            [     0,    0,  csr],  // 0: top
  25            [     0,  ccr, -isr],  // 1: bottom front
  26            [-e / 2, -icr, -isr],  // 2: bottom rear left
  27            [ e / 2, -icr, -isr]   // 3: bottom rear right
  28        ],
  29
  30        // faces
  31        [
  32            [0, 3, 2],  // rear
  33            [0, 1, 3],  // front right
  34            [0, 2, 1],  // front left
  35            [1, 2, 3]   // bottom
  36        ]
  37    );
  38}
  39
  40module octahedron(e) {
  41    // regular octahedron (eight faces); e = edge length
  42    // http://en.wikipedia.org/wiki/Octahedron
  43
  44    thin = .000001;
  45
  46    hull() {
  47        for(i = [0, 1]) {
  48            rotate([45, 0, i * 90]) {
  49                cube([thin, e, e], center = true);
  50            }
  51        }
  52    }
  53}
  54
  55module octahedron_alt(e) {
  56    // regular octahedron (eight faces), alternative method; e = edge length
  57    // http://en.wikipedia.org/wiki/Octahedron
  58
  59    // radius of circumscribed sphere
  60    csr = sqrt(2) / 2 * e;
  61
  62    polyhedron(
  63        // vertices
  64        [
  65            [   0,    0,  csr],  // 0: top
  66            [   0,  csr,    0],  // 1: front
  67            [ csr,    0,    0],  // 2: right
  68            [   0, -csr,    0],  // 3: rear
  69            [-csr,    0,    0],  // 4: left
  70            [   0,    0, -csr]   // 5: bottom
  71        ],
  72
  73        // faces
  74        [
  75            [0, 1, 2],  // top right front
  76            [0, 2, 3],  // top right rear
  77            [0, 3, 4],  // top left rear
  78            [0, 4, 1],  // top left front
  79            [5, 4, 3],  // bottom left rear
  80            [5, 3, 2],  // bottom right rear
  81            [5, 2, 1],  // bottom right front
  82            [5, 1, 4],  // bottom left front
  83        ]
  84    );
  85}
  86
  87module dodecahedron(e) {
  88    // regular dodecahedron (twelve faces); e = edge length
  89    // the vertices can be thought as three rectangles and a cube:
  90    // http://en.wikipedia.org/wiki/Regular_dodecahedron#Cartesian_coordinates
  91
  92    // phi   = (sqrt(5) + 1) / 2
  93    // phi^2 = (sqrt(5) + 3) / 2
  94    // 1/phi = (sqrt(5) - 1) / 2
  95
  96    long  = (sqrt(5) + 3) / 2 * e;  // 2 * phi / (2/phi) * e
  97    med   = (sqrt(5) + 1) / 2 * e;  // 2 * 1 / (2/phi) * e
  98    short = e;                      // 2 * 1/phi / (2/phi) * e
  99    thin  = .000001;
 100
 101    hull() {
 102        cube([thin,  short,  long], center = true);
 103        cube([long,   thin, short], center = true);
 104        cube([short,  long,  thin], center = true);
 105
 106        cube(med, center = true);
 107    }
 108}
 109
 110module dodecahedron_alt(e) {
 111    // regular dodecahedron (twelve faces), alternative method; e = edge length
 112    // the vertices can be thought as three rectangles and a cube:
 113    // http://en.wikipedia.org/wiki/Regular_dodecahedron#Cartesian_coordinates
 114
 115    // phi   = (sqrt(5) + 1) / 2
 116    // phi^2 = (sqrt(5) + 3) / 2
 117    // 1/phi = (sqrt(5) - 1) / 2
 118
 119    long  = (sqrt(5) + 3) / 4 * e;  // phi / (2/phi) * e
 120    med   = (sqrt(5) + 1) / 4 * e;  // 1 / (2/phi) * e
 121    short = e / 2;                  // 1/phi / (2/phi) * e
 122
 123    polyhedron(
 124        // vertices
 125        [
 126            // rectangle perpendicular to X axis
 127            [0,  short,  long],  //  0: top front
 128            [0, -short,  long],  //  1: top rear
 129            [0,  short, -long],  //  2: bottom front
 130            [0, -short, -long],  //  3: bottom rear
 131
 132            // rectangle perpendicular to Y axis
 133            [ long, 0,  short],  //  4: right top
 134            [ long, 0, -short],  //  5: right bottom
 135            [-long, 0,  short],  //  6: left top
 136            [-long, 0, -short],  //  7: left bottom
 137
 138            // rectangle perpendicular to Z axis
 139            [-short,  long, 0],  //  8: front left
 140            [ short,  long, 0],  //  9: front right
 141            [-short, -long, 0],  // 10: rear left
 142            [ short, -long, 0],  // 11: rear right
 143
 144            // cube
 145            [ med,  med,  med],  // 12: top front right
 146            [-med,  med,  med],  // 13: top front left
 147            [ med, -med,  med],  // 14: top rear right
 148            [-med, -med,  med],  // 15: top rear left
 149            [ med,  med, -med],  // 16: bottom front right
 150            [-med,  med, -med],  // 17: bottom front left
 151            [ med, -med, -med],  // 18: bottom rear right
 152            [-med, -med, -med],  // 19: bottom rear left
 153        ],
 154
 155        // faces
 156        [
 157            [0, 12,  4, 14,  1],  // top right
 158            [0,  1, 15,  6, 13],  // top left
 159            [2,  3, 18,  5, 16],  // bottom right
 160            [3,  2, 17,  7, 19],  // bottom left
 161
 162            [4, 12,  9, 16,  5],  // right front
 163            [4,  5, 18, 11, 14],  // right rear
 164            [6,  7, 17,  8, 13],  // left front
 165            [6, 15, 10, 19,  7],  // left rear
 166
 167            [0, 13,  8,  9, 12],  // front top
 168            [2, 16,  9,  8, 17],  // front bottom
 169            [1, 14, 11, 10, 15],  // rear top
 170            [3, 19, 10, 11, 18]   // rear bottom
 171        ]
 172    );
 173}
 174
 175module icosahedron(e) {
 176    // regular icosahedron (twenty faces); e = edge length
 177    // the vertices can be thought as three golden rectangles:
 178    // http://en.wikipedia.org/wiki/Regular_icosahedron#Cartesian_coordinates
 179
 180    long = (1 + sqrt(5)) / 2 * e;  // phi * e
 181    short = e;
 182    thin = .000001;
 183
 184    hull() {
 185        cube([thin,  short,  long], center = true);
 186        cube([long,   thin, short], center = true);
 187        cube([short,  long,  thin], center = true);
 188    }
 189}
 190
 191module icosahedron_alt(e) {
 192    // regular icosahedron (twenty faces), alternative method; e = edge length
 193    // the vertices can be thought as three golden rectangles:
 194    // http://en.wikipedia.org/wiki/Regular_icosahedron#Cartesian_coordinates
 195
 196    long = (1 + sqrt(5)) / 4 * e;  // phi / 2 * e
 197    short = e / 2;                 // 1 / 2 * e
 198
 199    polyhedron(
 200        // vertices
 201        [
 202            [0,  short,  long],  //  0: top front
 203            [0, -short,  long],  //  1: top rear
 204            [0,  short, -long],  //  2: bottom front
 205            [0, -short, -long],  //  3: bottom rear
 206
 207            [ long, 0,  short],  //  4: right top
 208            [ long, 0, -short],  //  5: right bottom
 209            [-long, 0,  short],  //  6: left top
 210            [-long, 0, -short],  //  7: left bottom
 211
 212            [-short,  long, 0],  //  8: front left
 213            [ short,  long, 0],  //  9: front right
 214            [-short, -long, 0],  // 10: rear left
 215            [ short, -long, 0],  // 11: rear right
 216        ],
 217
 218        // faces
 219        [
 220            [0,  4,  1],  // top right
 221            [0,  1,  6],  // top left
 222            [2,  3,  5],  // bottom right
 223            [2,  7,  3],  // bottom left
 224
 225            [1, 11, 10],  // rear top
 226            [3, 10, 11],  // rear bottom
 227            [0,  8,  9],  // front top
 228            [2,  9,  8],  // front bottom
 229
 230            [6,  7,  8],  // left front
 231            [6, 10,  7],  // left rear
 232            [4,  9,  5],  // right front
 233            [4,  5, 11],  // right rear
 234
 235            [1,  4, 11],  // top right rear
 236            [0,  9,  4],  // top right front
 237            [1, 10,  6],  // top left rear
 238            [0,  6,  8],  // top left front
 239
 240            [3, 11,  5],  // bottom right rear
 241            [2,  5,  9],  // bottom right front
 242            [3,  7, 10],  // bottom left rear
 243            [2,  8,  7]   // bottom left front
 244        ]
 245    );
 246}
 247
 248translate([  -5,    0, 0]) tetrahedron(2.5);
 249translate([-2.5, -1.5, 0]) octahedron(2);
 250translate([-2.5,  1.5, 0]) octahedron_alt(2);
 251translate([   1, -1.5, 0]) dodecahedron(1);
 252translate([   1,  1.5, 0]) dodecahedron_alt(1);
 253translate([   4, -1.5, 0]) icosahedron(1.5);
 254translate([   4,  1.5, 0]) icosahedron_alt(1.5);