License New BSD license
Lines 82
Keywords
cogs (1) gears (1) graphics (9) Nodebox (14) path filter (1) text (3)
Permissions
Owner: Theodore Test
Group Owner: Nodebox Community
Viewable by Everyone
Editable by All Siafoo Users
Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

Gears Graphical Path Filter Atom Feed 1

In Brief A neat path filter that makes bezier paths look cogs-and-gear styled. Also comes with a helper function for producing properly-spaced coggy text.... more
# 's
 1from nodebox.geo import coordinates,distance,angle
2
3def cog_filter(bezier_path,num_teeth,tooth_fraction,tooth_height,draw_it=True):
4 num_t = int(max(num_teeth,1))
5 p_len = float(bezier_path.length)
6 t_frac = float(min(max(0,tooth_fraction),1))
7 v_frac = 1.0 - t_frac
8
9 tot_t_len = int(p_len)*t_frac
10 tot_v_len = int(p_len)*v_frac
11
12 points_per_t = max(int(tot_t_len / num_t),1)
13 points_per_v = max(int(tot_v_len / num_t),1)
14 points_per_tv_set = max(points_per_t + points_per_v,1)
15 tot_points = num_t*points_per_tv_set
16
17 in_points_list = list(bezier_path.points(tot_points))
18
19
20 pt_ind = 0
21 for tooth_num in xrange(num_t):
22 if pt_ind == 0:
23 start_pt = in_points_list[0]
24 autoclosepath(True)
25 beginpath(start_pt.x,start_pt.y)
26 v_ind = 0
27 for v in xrange(points_per_v):
28 pt_ind = v_ind + tooth_num*points_per_tv_set
29 pt = in_points_list[pt_ind]
30 lineto(pt.x,pt.y)
31 v_ind += 1
32
33 t_ind = 0
34 for t in xrange(points_per_t):
35 pt_ind = t_ind + v_ind + tooth_num*points_per_tv_set
36 pt = in_points_list[pt_ind]
37
38 if pt_ind < len(in_points_list)-1:
39 next_pt = in_points_list[pt_ind+1]
40 else:
41 continue
42
43 ang_to_next_pt = angle(pt.x,pt.y,next_pt.x,next_pt.y)
44 perp_ang = ang_to_next_pt - 90 + (points_per_t/2 - t_ind)
45
46 ### Note to self: Playing with the perpendicular angle is awesome ###
47 ### Add either variable or incremental differences to the this angle... ###
48 perp_point_x,perp_point_y = coordinates(pt.x,pt.y,tooth_height,perp_ang)
49 lineto(perp_point_x,perp_point_y)
50
51 t_ind += 1
52
53 o_p = endpath(draw_it)
54
55def cog_text(text_string,x,y,num_teeth,tooth_fraction,tooth_height,text_width=WIDTH,draw_text=True):
56 my_cogs = []
57 adj = 0
58 for t in text_string:
59 t_path = textpath(t,x+adj,y,text_width)
60 cog_t = cog_filter(t_path,num_teeth,tooth_fraction,tooth_height,draw_it=draw_text)
61 my_cogs.append(cog_t)
62 w= textwidth(t,width=text_width)
63 adj += w + tooth_height
64
65 return my_cogs
66
67
68size(900,900)
69colors = ximport('colors')
70colors.shadow(alpha=0.2,blur=10)
71fill(0.5,1.0)
72stroke(0)
73strokewidth(3)
74o1 = oval(50,80,200,200,draw=False)
75o2 = oval(325,80,200,200,draw=False)
76o3 = oval(600,80,200,200,draw=False)
77r1 = rect(50,400,200,200,draw=False)
78r2 = rect(325,400,200,200,draw=False)
79r3 = rect(600,400,200,200,draw=False)
80
81colormode(HSB)
82background(0.6,0.5,0.9,0.9)
83cog_filter(o1,10,0.5,10)
84cog_filter(o2,20,0.5,10)
85cog_filter(o3,20,0.5,40)
86
87cog_filter(r1,10,0.5,10)
88cog_filter(r2,20,0.75,10)
89cog_filter(r3,50,0.5,10)
90
91fontsize(250)
92font('Courier')
93cog_text("Gears",50,800,20,0.5,5)

A neat path filter that makes bezier paths look cogs-and-gear styled. Also comes with a helper function for producing properly-spaced coggy text.

The filter allows you to control height, number, and fraction of hte path surface occupied by the gear'teeth.'

http://www.siafoo.net/image/95