Skip to content

Instantly share code, notes, and snippets.

@bmander
Created April 10, 2012 02:53
Show Gist options
  • Save bmander/2348059 to your computer and use it in GitHub Desktop.
Save bmander/2348059 to your computer and use it in GitHub Desktop.
gear
function sqrt(a){
return Math.pow(a,0.5);
}
function sq(a){
return Math.pow(a,2);
}
function rotate(theta, func){
return function(x,y){
var xprime = Math.cos(theta)*x-Math.sin(theta)*y;
var yprime = Math.sin(theta)*x+Math.cos(theta)*y;
return func(xprime,yprime);
}
}
function translate(tx, ty, func){
return function(x,y){
var xprime = x-tx;
var yprime = y-ty;
return func(xprime,yprime);
}
}
function reflect_y(func){
return function(x,y){
return func(x,-y);
}
}
function intersect(funcs){
var metaargs=arguments;
return function(x,y) {
var ret=true;
for(var i=0; i<metaargs.length;i++){
ret = ret && metaargs[i](x,y);
}
return ret;
}
}
function union(funcs){
var metaargs=arguments;
return function(x,y) {
var ret=false;
for(var i=0; i<metaargs.length;i++){
ret = ret || metaargs[i](x,y);
}
return ret;
}
}
function pie_slice(top,bottom){
return function(x,y){
return atan2(y, x)<=top && atan2(y, x)>=bottom;
}
}
var atan = Math.atan;
var acos = Math.acos;
var cos = Math.cos;
var sin = Math.sin;
var atan2 = Math.atan2;
var pi = Math.PI;
var border = 0.05 ;
var outer_diameter = 0.65 ;
var base_diameter = outer_diameter * 0.88 ;
var root_diameter = outer_diameter * 0.82 ;
var pitch_diameter = outer_diameter * 0.92 ;
var num_teeth = 24 ;
var outer_radius = outer_diameter/2;
var base_radius = base_diameter/2;
var root_radius = root_diameter/2;
var pitch_radius = pitch_diameter/2;
function maketooth(base_radius){
// Formula for the region on the upper left of an involute
// (thanks to Peter Fedak, HMC 2013)
return function(x,y){
return (base_radius*(atan(y/x) + acos(base_radius/sqrt(x*x + y*y))) - sqrt((x*x + y*y)-base_radius*base_radius)) < 0;
}
}
// Calculate how to rotate the tooth for the given pitch diameter
var pitch_param = sqrt(sq(pitch_radius) / sq(base_radius) - 1.0)
var x_pitch = base_radius * (cos(pitch_param) + pitch_param * sin(pitch_param))
var y_pitch = base_radius * (sin(pitch_param) - pitch_param * cos(pitch_param))
var pitch_angle = atan2(y_pitch, x_pitch)
var rotation_angle = 90. / num_teeth - pitch_angle * 180 / pi
tooth = maketooth(30);
tooth = rotate(rotation_angle, tooth);
tooth = intersect( tooth, reflect_y(tooth) );
return true;
//return true;
//tooth = union(tooth, pie_slice(1,-1));
//return true;
//return translate( 150,150, tooth )(x,y);
//return translate(150,150,pie_slice(rotation_angle,-pi/3))(x,y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment