#NEWLINE# def pack( self ) : #NEWLINE# #INDENT# global min_x_reject , min_y_reject #NEWLINE# rects = [ ( key , self . rectangles [ key ] [ 0 ] , self . rectangles [ key ] [ 1 ] ) for key in self . rectangles ] #NEWLINE# rects . sort ( rectcmp ) #NEWLINE# width = 2 #NEWLINE# height = 2 #NEWLINE# done = False #NEWLINE# while ( not done ) : #NEWLINE# #INDENT# locations = TreeNode ( None , None , ( 0 , 0 , width , height ) , None ) #NEWLINE# self . free_locations = [ locations ] #NEWLINE# rejects = [ ] #NEWLINE# for rect in rects : #NEWLINE# #INDENT# if ( self . insert ( rect ) is None ) : #NEWLINE# #INDENT# rejects . append ( rect ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( len ( rejects ) == 0 ) or ( self . maxwidth and ( width >= self . maxwidth ) and self . maxheight and ( height >= self . maxheight ) ) ) : #NEWLINE# #INDENT# done = True #NEWLINE# #UNINDENT# if ( not done ) : #NEWLINE# #INDENT# minx = min ( ( reject [ 1 ] for reject in rejects ) ) #NEWLINE# miny = min ( ( reject [ 2 ] for reject in rejects ) ) #NEWLINE# totx = sum ( ( reject [ 1 ] for reject in rejects ) ) #NEWLINE# toty = sum ( ( reject [ 2 ] for reject in rejects ) ) #NEWLINE# next_width = width #NEWLINE# while ( ( width + minx ) > next_width ) : #NEWLINE# #INDENT# next_width *= 2 #NEWLINE# #UNINDENT# next_height = height #NEWLINE# while ( ( height + miny ) > next_height ) : #NEWLINE# #INDENT# next_height *= 2 #NEWLINE# #UNINDENT# if ( ( ( ( next_width - width ) < ( next_height - height ) ) or ( self . maxheight and ( height >= self . maxheight ) ) ) and ( ( self . maxwidth is None ) or ( next_width <= self . maxwidth ) ) ) : #NEWLINE# #INDENT# width = next_width #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( ( ( next_height - height ) <= ( next_width - width ) ) or ( self . maxwidth and ( width >= self . maxwidth ) ) ) and ( ( self . maxheight is None ) or ( next_height <= self . maxheight ) ) ) : #NEWLINE# #INDENT# height = next_width #NEWLINE# #UNINDENT# #UNINDENT# if ( width < minx ) : #NEWLINE# #INDENT# width = next_width #NEWLINE# #UNINDENT# if ( height < miny ) : #NEWLINE# #INDENT# height = next_height #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# self . rejects = [ reject [ 0 ] for reject in rejects ] #NEWLINE# self . placements = dict ( locations ) #NEWLINE# self . width = width #NEWLINE# self . height = height #NEWLINE# if ( len ( self . rejects ) > 0 ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return True