1 #based on a work by genehacker
2 #copyright 2009 ben lipkowitz
3 #distributed under the terms of the GNU GPL version 2 or later
5 #cycloidal.py: draws a cycloidal gear
10 def frange(start
, stop
, step
): #this ought to come with python :(
16 height
, width
= 500, 500
17 surf
= cairo
.SVGSurface('cycloidal.svg', height
, width
)
18 cr
= cairo
.Context(surf
)
19 cr
.translate(0.5, 0.5) #align pixels
20 cr
.scale(width
, height
)
21 cr
.translate(0.5, 0.5) #move gear to center of image
23 tmp
= cr
.user_to_device(1,1)
24 scale
= (tmp
[0] + tmp
[1]) / 2.
25 cr
.set_line_width(1/scale
)
27 def cycloidal(teeth
=17, module
=0.05, resolution
=2):
28 '''draws a cycloidal gear. arguments: number of teeth, circumference per tooth, degrees per step'''
29 radius
= module
*teeth
/2 #pitch radius
31 ro
= radius
/z
#rolling circle radius
33 angle
= 2*pi
/z
#angle of 1 lobe of hypo/epicycloid
34 for m
in range(z
+1): #teeth):
35 #for inout in [1, -1]:
37 for theta
in frange(angle
*m
, angle
*(m
+1), resolution
*pi
/180):
38 x
= ((radius
+ inout
* ro
) * cos(theta
)) - inout
* \
39 (ro
* cos(theta
* (radius
+ inout
* ro
)/ro
))
40 y
= ((radius
+ inout
* ro
) * sin(theta
)) - \
41 (ro
* sin(theta
* (radius
+ inout
* ro
)/ro
))
46 cr
.set_source_rgb(1,1,1)
48 cr
.set_source_rgb(0,0,0)
53 surf
.write_to_png('cycloidal.png')