#NEWLINE# def approximate_cubic_bezier( p0 , p1 , p2 , p3 , threshold = 1.0 , limit = 1024 ) : #NEWLINE# #INDENT# '\n    Return an series of points whose segments approximate the given\n    bezier curve\n    ' #NEWLINE# threshold_squared = ( threshold ** 2 ) #NEWLINE# points = { #NEWLINE# #INDENT# 0.0 : p0 , #NEWLINE# 1.0 : p3 , #NEWLINE# #UNINDENT# } #NEWLINE# to_check = deque ( [ ( 0.0 , 1.0 ) ] ) #NEWLINE# while ( ( len ( to_check ) > 0 ) and ( len ( points ) < limit ) ) : #NEWLINE# #INDENT# ( l , r ) = to_check . popleft ( ) #NEWLINE# pl = points [ l ] #NEWLINE# pr = points [ r ] #NEWLINE# m = ( ( l + r ) / 2.0 ) #NEWLINE# pm = cubic_bezier_point ( p0 , p1 , pl , p3 , m ) #NEWLINE# if ( square_distance_to_segment ( pm , pl , pr ) > threshold_squared ) : #NEWLINE# #INDENT# points [ m ] = pm #NEWLINE# to_check . append ( ( l , m ) ) #NEWLINE# to_check . append ( ( m , r ) ) #NEWLINE# #UNINDENT# #UNINDENT# return [ points [ t ] for t in sorted ( points . keys ( ) ) ]