Optimal sizes to display X picture boxes in an given space

110 views Asked by At

I searched before posting but couldn't find anything close to my issue.

What I need to figure out is how to come with the optimal width and height of picture boxes (with a 4:3 ratio), given the required number of boxes to be displayed, and the available space.

Now, it's not as simple as a just dividing the available space by the number of required boxes, because the available space is not a uniform shape, but rather two rectangles of which size may vary (see this picture, it's the a+b space).

If fact, I have tried starting from there with the following code :

    Private Sub LayoutSnapshots()
    Dim lTotalSpace As Single, lSnapsize As Single, sXSize As Single, sYSize As Single
    Dim I As Integer, J As Integer, X As Integer = 0, Y As Integer = 0, oPic As PictureBox

    ' bSnaps is the number of picture boxes to be displayed
    If stSetting.bSnaps = 0 Then Exit Sub

    ' oSnaps is a List(Of PictureBoxe) to groupp the actual picture boxes
    If oSnaps.Count > 0 Then
        For Each oCtrl As PictureBox In oSnaps
            Me.Controls.Remove(oCtrl)
        Next
    End If
    oSnaps.Clear()

    ' Calculating the a+b space shown on the picture
    lTotalSpace = ((Me.ClientSize.Height - MenuStrip1.Height) * Me.ClientSize.Width) - ((picPreview.Width + iMargin) * (picPreview.Height + iMargin))

    If lTotalSpace < 1 Then
        MsgBox("Window is too small. Please adjust one of these settings : Window size, Snapshots count, Live free view size.", MsgBoxStyle.ApplicationModal Or MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
        Exit Sub
    End If

    'calculating a single picture's size by dividing total space by the number of snaps
    lSnapsize = Math.Truncate(lTotalSpace / stSetting.bSnaps)

    'Calculating Height and Width, with 4:3 ratio
    sXSize = Math.Truncate(Math.Sqrt((4 * lSnapsize) / 3))
    sYSize = Math.Truncate(Math.Sqrt((3 * lSnapsize) / 4))

    For I = 1 To stSetting.bSnaps
        If oPic IsNot Nothing Then oPic = Nothing
        oPic = New PictureBox
        oPic.BackColor = Color.White
        oPic.BorderStyle = BorderStyle.FixedSingle
        oPic.Size = New Size(sXSize - 1, sYSize - 1)
        oPic.Location = New Point(X * sXSize, (Y * sYSize) + MenuStrip1.Height)

        oSnaps.Add(oPic)
        ' Layed them successively on screen, need to optimize this
        If ((X + 2) * sXSize) > (Me.ClientSize.Width) Then
            X = 0
            Y += 1
        Else
            X += 1
        End If
    Next

    For Each oCtrl As PictureBox In oSnaps
        Me.Controls.Add(oCtrl)
    Next
End Sub

But obviously with all the possibilities of windows resizing, I couldn't think of any practical way to optimize it.

enter image description here

I am pretty sure this has to do with "operation research", as I recall we did optimization problems like this back then when I was a student, but I'm not sure how to actually model this or even if it is solvable by linear programming.

1

There are 1 answers

0
S. Saad On BEST ANSWER

I have figured this out. The solution is kind of a "brute force" technique, it doesn't always return the optimum BUT the error is merely a few pixels. I used the code below, it works but it might need further optimization in terms of spacing. I couldn't comment on everything since I have a time pressure right now, but still wanted to share the answer, so just take some time to analyze it :

Private Sub LayoutSnapshots()

    Dim sA As Single, sB As Single, sTotal As Single, sSnap As Single, sWidth As Single, sHeight As Single
    Dim iCount As Integer = stSetting.bSnaps, iFit As Integer, iX As Integer, iY As Integer, iYg As Integer, I As Integer
    Dim rA As Rectangle, rB As Rectangle, oPic As PictureBox, lpLoc As New List(Of Point), pLoc As New Point

    Static bWarn As Boolean

    Dim gPic As Graphics

    ' bSnaps is the number of picture boxes to be displayed
    If stSetting.bSnaps = 0 Then Exit Sub

    ' If controls already on form, remove them and start form scratch
    If oSnaps.Count > 0 Then
        For Each oCtrl As PictureBox In oSnaps
            Me.Controls.Remove(oCtrl)
        Next
    End If

    ' oSnaps is a List(Of PictureBox) grooping the picture boxes. Clear it for now
    oSnaps.Clear()

    'sA, sB are the sizes of spaces A and B respectively
    sA = (Me.ClientSize.Width * (Me.ClientSize.Height - (MenuStrip1.Height + picPreview.Height + iMargin)))
    sB = ((Me.ClientSize.Width - (picPreview.Width + iMargin)) * (picPreview.Height + iMargin))

    ' Total free space
    sTotal = sA + sB

    ' This condition is important. It ensures there is at least one solution
    ' before entering the loops bellow. Otherwise we might get stuck in an infinite loop
    If (sTotal < (stSetting.bSnaps * stSetting.bSnaps)) Then

        ' bWarn is a static boolean. Since this Sub is called from Form_Resize event, we 
        ' want to warn the user only once when there is no space. 
        ' Otherwise it becomes annoying.

        If bWarn Then MsgBox("Window is too small. Please adjust one of these settings : Window size, Snapshots count, Live free view size.", MsgBoxStyle.ApplicationModal Or MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
        bWarn = False
        Exit Sub
    End If

    bWarn = True

    Me.UseWaitCursor = True

    Do

        'rA, rB are the bounding rectangles of spaces A and B respectively
        rA = New Rectangle(0, MenuStrip1.Height, Me.ClientSize.Width, Me.ClientSize.Height - (MenuStrip1.Height + picPreview.Height + iMargin))
        rB = New Rectangle(0, picPreview.Top, Me.ClientSize.Width - (picPreview.Width + iMargin), picPreview.Height + iMargin)

        ' A single box's size
        sSnap = Math.Truncate(sTotal / iCount)

        ' Width and Height with 4:3 aspect ratio.
        sWidth = Math.Truncate(Math.Sqrt((4 * sSnap) / 3))
        sHeight = Math.Truncate(Math.Sqrt((3 * sSnap) / 4))

        ' iFit keeps track of how many boxes we could fit in total
        iFit = 0
        iYg = 0
        lpLoc.Clear()

        ' It would be a bit too long to explain the next block of code and I have a deadline to meet
        ' I'll comenting on that later
        iX = 0
        iY = 0
        Do While (rA.Height >= ((sHeight * (iY + 1)) + 1))
            If (((iX + 1) * sWidth) + 1) <= rA.Width Then
                iFit += 1
                lpLoc.Add(New Point(rA.X + ((iX * sWidth) + 1), rA.Y + ((iYg * sHeight) + 1)))
                iX += 1
            Else
                iX = 0
                iY += 1
                iYg += 1
            End If
        Loop

        'Add unused space from A to B.
        rB.Height = rB.Height + (rA.Height - ((iYg * sHeight) + 1))

        iX = 0
        iY = 0
        Do While (rB.Height >= ((sHeight * (iY + 1)) + 1))
            If (((iX + 1) * sWidth) + 1) <= rB.Width Then
                iFit += 1
                lpLoc.Add(New Point(rB.X + ((iX * sWidth) + 1), rA.Y + ((iYg * sHeight) + 1)))
                iX += 1
            Else
                iX = 0
                iY += 1
                iYg += 1
            End If
        Loop

        Application.DoEvents()

        iCount += 1

    Loop While iFit < stSetting.bSnaps

    ' Add controls to form. Lay them one next to each other.
    iX = 0
    iY = 0
    For I = 1 To stSetting.bSnaps
        If oPic IsNot Nothing Then oPic = Nothing
        oPic = New PictureBox

        oPic.BackColor = Color.Cyan
        oPic.BorderStyle = BorderStyle.FixedSingle
        oPic.Size = New Size(sWidth - 1, sHeight - 1)
        oPic.Location = lpLoc(I - 1)

        ' Just for debugging, displays index of each box inside it.
        oPic.Image = New Bitmap(oPic.Width, oPic.Height)
        gPic = Graphics.FromImage(oPic.Image)
        gPic.DrawString(I, New Font("Arial", 10, FontStyle.Regular), Brushes.Red, New Point(0, 0))

        oSnaps.Add(oPic)
        Me.Controls.Add(oSnaps.Last)
    Next

    'Catch Ex As Exception

    'Finally
    Me.UseWaitCursor = False
    'End Try
End Sub

P.S : Anyone please feel free to add more explanation to the code if you want.