/ src / dishes / cylindrical.scad
cylindrical.scad
 1  module cylindrical_dish(width, height, depth, inverted){
 2    // .5 has problems starting around 3u
 3    $fa=.25;
 4    /* we do some funky math here
 5     * basically you want to have the dish "dig in" to the keycap x millimeters
 6     * in order to do that you have to solve a small (2d) system of equations
 7     * where the chord of the spherical cross section of the dish is
 8     * the width of the keycap.
 9     */
10    // the distance you have to move the dish so it digs in depth millimeters
11    chord_length = (pow(width, 2) - 4 * pow(depth, 2)) / (8 * depth);
12    //the radius of the dish
13    rad = (pow(width, 2) + 4 * pow(depth, 2)) / (8 * depth);
14    direction = inverted ? -1 : 1;
15  
16    translate([0,0, chord_length * direction]){
17      rotate([90, 0, 0]) cylinder(h=height + 20, r=rad, center=true);
18    }
19  }