various methods to parse pages from thingiverse
[skdb.git] / cycloidal.py
blob6cac9606b6df63b09919fb48bed979fe262f38d0
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
7 import cairo
8 from math import *
10 def frange(start, stop, step): #this ought to come with python :(
11 r = start
12 while r < stop:
13 yield r
14 r += step
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
30 z = teeth*2
31 ro = radius/z #rolling circle radius
32 inout = 1
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]:
36 inout *= -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))
42 if m == 0:
43 cr.move_to(x, y)
44 else:
45 cr.line_to(x,y)
46 cr.set_source_rgb(1,1,1)
47 cr.fill_preserve()
48 cr.set_source_rgb(0,0,0)
49 cr.stroke()
51 cycloidal(10)
52 cycloidal(8)
53 surf.write_to_png('cycloidal.png')