Package visgraph :: Package drawing :: Module bezier
[hide private]
[frames] | no frames]

Source Code for Module visgraph.drawing.bezier

 1   
2 -def splitline(pt1, pt2, percent=0.5):
3 ''' 4 Return a point which splits the given line at the 5 given percentage... 6 7 Example: splitline( (0,0), (20, 30), 0.1) 8 ''' 9 10 pt1_x, pt1_y = pt1 11 pt2_x, pt2_y = pt2 12 13 deltax = (pt2_x - pt1_x) * percent 14 deltay = (pt2_y - pt1_y) * percent 15 16 return int(pt1_x + deltax), int(pt1_y + deltay)
17
18 -def calculate_bezier(points, steps = 30):
19 ''' 20 Arbitrary depth and arbitrary precision bezier implementation. Takes 21 a list of (x,y) point tuples and returnes the points to draw for the 22 bezier curve. 23 ''' 24 ret = [] 25 points = [ (float(x),float(y)) for x,y in points ] 26 27 for i in xrange(steps+1): 28 29 pcent = i / float(steps) 30 31 layers = [ points, ] 32 while len(layers[-1]) != 1: 33 l_points = layers[-1] 34 newpoints = [ splitline( l_points[i], l_points[i+1], pcent) for i in xrange(len(l_points)-1) ] 35 layers.append(newpoints) 36 37 ret.append(layers[-1][0]) 38 39 return ret
40 41 if __name__ == '__main__': 42 print calculate_bezier( [ (0,0), (3, 20), (20, 23), (20, 20)] ) 43 44 print calculate_bezier( [ (0,0), (10,10) ], 10) 45