@@ -0,0 +1,1781 @@
+import random%0Aimport math%0A%0A%0Adef cutRect(mainRect, subRect):%0A%09mainL, mainT, mainR, mainB = mainRect%0A%09subL, subT, subR, subB = subRect%0A%0A%09if mainB %3C subT or mainL %3E subR or mainR %3C subL or mainT %3E subB:%0A%09%09return (mainRect,)%0A%0A%09subL = mainL if subL %3C mainL else subL%0A%09subT = mainT if subT %3C mainT else subT%0A%09subR = mainR if subR %3E mainR else subR%0A%09subB = mainB if subB %3E mainB else subB%0A%0A%09L = (mainT, mainL, mainB, subL) if mainL %3C subL else None%0A%09T = (mainT, mainL, subT, mainR) if mainT %3C subT else None%0A%09R = (mainT, subR, mainB, mainR) if mainR %3E subR else None%0A%09B = (subB, mainL, mainB, mainR) if mainB %3E subB else None%0A%0A%09return filter(lambda x: x, (L, T, R, B))%0A%0A%0Adef isInRect(mainRect, subRect):%0A%09mainL, mainT, mainR, mainB = mainRect%0A%09subL, subT, subR, subB = subRect%0A%0A%09return mainL %3C= subL and mainT %3C= subT and mainR %3E= subR and mainB %3E= subB%0A%0Adef rank():%0A%09pass%0A%0Adef expand(maxRects, cur):%0A%09pass%0A%0Adef selectBest(maxRectsList, img):%0A%09imgW, imgH = img%0A%09for i, maxRect in enumerate(maxRectsList):%0A%09%09w, h = maxRect%5B2%5D - maxRect%5B0%5D, maxRect%5B3%5D - maxRect%5B1%5D%0A%09%09if w %3E= imgW and h %3E= imgH:%0A%09%09%09return i%0A%09return -1%0A%0Adef maxRectsBinPack(maxRect, imgs):%0A%09maxRectsList = %5BmaxRect%5D%0A%09_imgs = %5B%5D%0A%0A%09for img in imgs:%0A%09%09index = selectBest(maxRectsList, img)%0A%0A%09%09if index == -1:%0A%09%09%09print(%22Can't not fit.%22)%0A%09%09%09return%0A%0A%09%09x, y = maxRectsList%5Bindex%5D%5B:2%5D%0A%09%09w, h = img%0A%09%09imgRect = (x, y, x+w, y+h)%0A%09%09_imgs.append(imgRect)%0A%0A%09%09_maxRectsList = %5B%5D%0A%09%09for maxRect in maxRectsList:%0A%09%09%09_maxRectsList.extend(cutRect(maxRect, imgRect))%0A%0A%09%09maxRectsList = _maxRectsList%0A%0A%09return maxRectsList, _imgs%0A%0A%0A%0Adef genRectData():%0A%09return (random.randint(0,100), random.randint(0, 100))%0A%0A%0Adef main():%0A%09a = (0,0, 100,100)%0A%09b = ((50, 50), (50, 100), (40,40))%0A%09print( maxRectsBinPack(a, b)%5B1%5D)%0A%0Aif __name__ == '__main__':%0A%09main()

add a file