Results 1 to 8 of 8

Thread: Rectangle "best fit" optimisation

Threaded View

  1. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Rectangle "best fit" optimisation

    Here's my approach.
    I've called W and H the width and height of the rectangular area and a the widht and b the height of a small rectangle ("brick") that's to be packed inside.

    Let:

    NP = number of rows with the bricks in portrait orientation
    NL = number of rows with the bricks in landscape orientation

    (see figures)

    nP = INT(W/a) is the number of bricks per row (portrait)
    nL = INT(W/b) is the number of bricks per row (landscape)

    Then the total number of bricks is, at least:

    Total = NP*nP + NL*nL

    where these restrictions apply:

    NP <= INT(H/a)
    NL <= INT(H/b)
    b*NP + a*NL <= H

    Now, in some cases, after packing the last rows in landscape mode there could be some space left at one side (left or right) to place a few moore bricks in portrait orientation (see figure). This is the case if both these 2 conditions are met:

    W - b*nL >= a
    H - b*NP >= b

    If you call:
    k1 = W - b * nL
    k2 = H - b * NP

    then k1*k2 is the number of extra bricks.

    VB Code:
    1. 'Rectangle width and height
    2. Const W As Single = 1200
    3. Const H As Single = 800
    4. 'Brick width (a) and height (b)
    5. Const a As Single = 37
    6. Const b As Single = 83
    7. Private Sub main()
    8.     Dim nRowsPort As Integer, nRowsLand As Integer
    9.     Dim nBricksPerRowPort As Integer, nBricksPerRowLand As Integer
    10.     Dim nRowsPortMax As Integer, nRowsLandMax As Integer
    11.     'Height of all the rows (portrait plus landscape)
    12.     Dim TotalH As Integer
    13.     'Total number of bricks
    14.     Dim TotalN As Integer
    15.     'Total bricks with possible extras
    16.     Dim GrandTotalN As Integer
    17.     Dim tmp As Integer, i As Integer, j As Integer
    18.     Dim dummy1 As Single, dummy2 As Single
    19.    
    20.     nBricksPerRowPort = Int(W / a)
    21.     nBricksPerRowLand = Int(W / b)
    22.    
    23.     'Maximum values for nRowsPort and nRowsLand:
    24.     nRowsPortMax = Int(H / b)
    25.     nRowsLandMax = Int(H / a)
    26.     'Try all integer combinations of nRowsPort and nRowsLand
    27.     'with the restrictions:
    28.     'nRowsPort <= nRowsPortMax
    29.     'nRowsLand <= nRowsLandMax
    30.     'b*nRowsPort + a*nRowsLand <= H
    31.     TotalN = 0
    32.     For i = 1 To nRowsPortMax
    33.         For j = 1 To nRowsLandMax
    34.             TotalH = b * i + a * j
    35.             If TotalH <= H Then
    36.                 tmp = i * nBricksPerRowPort + j * nBricksPerRowLand
    37.                 If tmp > TotalN Then
    38.                     TotalN = tmp
    39.                     nRowsPort = i
    40.                     nRowsLand = j
    41.                 End If
    42.             End If
    43.         Next
    44.     Next
    45.     'Check if some space is left to fit in a few more
    46.     dummy1 = W - b * nBricksPerRowLand
    47.     dummy2 = H - b * nRowsPort
    48.     If dummy1 >= a And dummy2 >= b Then
    49.         'Number of extra rows of bricks
    50.         i = Int(dummy2 / b)
    51.         'Number of extra bricks per row
    52.         j = Int(dummy1 / a)
    53.         'Number of extra bricks
    54.         i = i * j
    55.     Else
    56.         i = 0
    57.     End If
    58.    
    59.     'Grand total
    60.     GrandTotalN = TotalN + i
    61.    
    62.     Debug.Print "Total bricks: " & CStr(TotalN)
    63.     Debug.Print "Extra bricks: " & CStr(i)
    64.     Debug.Print "Grand total bricks: " & CStr(GrandTotalN)
    65.     Debug.Print "Rows (portrait): " & CStr(nRowsPort)
    66.     Debug.Print "Rows (landscape): " & CStr(nRowsLand)
    67. End Sub
    Attached Images Attached Images   
    Last edited by krtxmrtz; May 13th, 2009 at 10:35 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width