Saturday 11 June 2016

Packing similar small rectangles into a larger rectangle of given dimension

I was facing a similar issue and finally got the answer to my problem on my own. 
Assuming length is greater than the breadth for both the rectangles 
(smaller and larger ones), following cases will occur while you try to pack 
smaller rectangles on the larger one. Let the length of larger rectangle be L,
 its breadth be B and length and breadth of smaller rectangles be l and b 
respectively. Case 1. Pack the smaller rectangles such that their lengths 
are parallel to the breadth of the larger rectangle until you fall short of space.
 Then try the other way round (Length of larger rectangle parallel to Lengths of 
smaller one) on the available space. Case 2: Pack the smaller rectangles such
 that their lengths are parallel to the length of the larger rectangle until you 
fall short of space. Then try the other way round (Length of larger rectangle 
parallel to breadths of smaller one) on the available space. Take the maximum 
of case 1 and case 2 to get the maximum no of smaller rectangles that can be 
packed on a larger one. Find the python 3 code of the implementation below: 

import math
Length_of_larger_rectangle = 1200
Length_of_smaller_rectangle= 520
Breadth_of_larger_rectangle = 1000
Breadth_of_smaller_rectangle= 343
def pack(Length_of_larger_rectangle,Breadth_of_larger_rectangle,Length_of_smaller_rectangle,Breadth_of_smaller_rectangle ):
    if Length_of_larger_rectangle<Breadth_of_larger_rectangle:
        temp=Breadth_of_larger_rectangle
        Breadth_of_larger_rectangle=Length_of_larger_rectangle
        Length_of_larger_rectangle=temp
    if(Length_of_smaller_rectangle<Breadth_of_smaller_rectangle):
        temp2=Breadth_of_smaller_rectangle
        Breadth_of_smaller_rectangle = Length_of_smaller_rectangle
        Length_of_smaller_rectangle=temp2

    Case1= math.floor(Length_of_larger_rectangle/Length_of_smaller_rectangle )*math.floor(Breadth_of_larger_rectangle/Breadth_of_smaller_rectangle)
    left_length1  =  Length_of_larger_rectangle-Length_of_smaller_rectangle*math.floor(Length_of_larger_rectangle/Length_of_smaller_rectangle )
    if (left_length1>Breadth_of_smaller_rectangle):
        Case1a=math.floor(Breadth_of_smaller_rectangle/left_length1)*math.floor(left_length1/Breadth_of_smaller_rectangle)
        Case1=Case1+Case1a

Case2=math.floor(Breadth_of_larger_rectangle/Breadth_of_smaller_rectangle)*math.floor(Length_of_larger_rectangle/Length_of_smaller_rectangle )

Case3=math.floor(Length_of_larger_rectangle/Breadth_of_smaller_rectangle )*math.floor(Breadth_of_larger_rectangle/Length_of_smaller_rectangle)
    left_breadth2=Breadth_of_larger_rectangle-Length_of_smaller_rectangle*math.floor(Breadth_of_larger_rectangle/Length_of_smaller_rectangle)
    if(left_breadth2>Breadth_of_smaller_rectangle):
        Case3=Case3+math.floor(Length_of_larger_rectangle/Length_of_smaller_rectangle)*math.floor(left_breadth2/Breadth_of_smaller_rectangle)
    Case4=math.floor(Breadth_of_larger_rectangle/ Length_of_smaller_rectangle)*math.floor(Length_of_larger_rectangle/Breadth_of_smaller_rectangle)

    No_of_cfcs=max(Case1,Case2,Case3,Case4)
    return No_of_cfcs

a=pack(Length_of_larger_rectangle,Breadth_of_larger_rectangle,Length_of_smaller_rectangle,Breadth_of_smaller_rectangle )
print(a)

No comments:

Post a Comment