translate([-40, 0, 0]) mirror() rotate([0, 0, 180]) screw_gear(8, 4, 2, 2, 8, 18, 32);

difference() {
    union() {
        for (i=[1:10]) rotate([0, 0, 360*i/10]) screw_gear(8, 6, 2, 60, 64, 18, 64);
    }
//    translate([-50, 0, 0]) cube(size=[100,100,100]);
}    
#difference() {
    translate([-15, 0, 0]) mirror() rotate([0, 0, 360/2/10]) {
        union() {
            for (i=[1:10]) rotate([0, 0, 360*i/10]) screw_gear(8, 6, 2, 60, 64, 18, 64);
        }
    }
//    translate([-50, 0, 0]) cube(size=[100,100,100]);
}

module screw_gear(outer_radius, inner_radius, outer_thickness, inner_thickness, thread_length, precision, total_length)
{
    linear_extrude(height=total_length, twist=360*total_length/thread_length, slices=360)
    polygon(
        points = screw_crosssection(outer_radius, inner_radius, outer_thickness, inner_thickness, thread_length, precision),
        paths = [ range((precision+precision)*2) ]
    );
}

function screw_crosssection(outer_radius, inner_radius, outer_thickness, inner_thickness, thread_length, precision) =
    concat(
        [ for (x=[1 : precision]) polar(outer_radius, 360*outer_thickness/thread_length*x/precision - outer_thickness/2/thread_length*360) ],
        [ for (x=[1 : precision])
            polar(
                outer_radius-(outer_radius-inner_radius)*x/precision,
                outer_thickness/2/thread_length*360 + 360*(1-outer_thickness/thread_length-inner_thickness/thread_length)/2*x/precision
            )
        ],
        [ for (x=[1 : precision]) polar(inner_radius, 180 + 360*inner_thickness/thread_length*x/precision - inner_thickness/2/thread_length*360) ],
        [ for (x=[1 : precision])
            polar(
                inner_radius+(outer_radius-inner_radius)*x/precision,
                180 + inner_thickness/2/thread_length*360 + 360*(1-outer_thickness/thread_length-inner_thickness/thread_length)/2*x/precision
            )
        ]
    );

function polar(r,theta) = r*[sin(theta), cos(theta)]; // convert polar to cartesian coordinates

function range(n) = [ for(x = [0 : n-1]) x ];
