Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grid Formatting Demo
#1
Based off the topic here: https://qb64phoenix.com/forum/showthread.php?tid=3475

Code: (Select All)
Type Ratio_Type
    x As _Integer64
    y As _Integer64
    description As String
    size As _Integer64
    scale As _Integer64
    total As _Integer64
    empty As _Integer64
End Type

Dim Shared Ratio(8) As Ratio_Type
Dim As Long items
Screen _NewImage(1280, 720, 32)

InitRatios
Do
    Cls
    Input "Enter the number of items =>"; items
    If items <= 0 Then System
    CalculateGrids items
    SortGrids
    DisplayGrids
    Print: Print "Press <ANY KEY> to restart"
    pause$ = Input$(1)
Loop

Sub DisplayGrids
    Print
    Color &HFF00FF00&&
    Print "Description                                                      Ratio  Scaler  Grid  Empty"
    Color &HFFFFFFFF&&
    format$ = "\                                                              \  ##:##    ###    ##x##  ### "
    For i = 1 To UBound(Ratio)
      ? using format$;ratio(i).description,ratio(i).x,ratio(i).y, ratio(i).scale, ratio(i).x*ratio(i).scale,_
              ratio(i).y*ratio(i).scale, ratio(i).empty
    Next
End Sub

Sub SortGrids
    For i = 1 To UBound(Ratio)
        For j = i + 1 To UBound(Ratio)
            If Ratio(i).empty > Ratio(j).empty Then Swap Ratio(i), Ratio(j)
    Next j, i
End Sub

Sub CalculateGrids (items)
    For i = 1 To UBound(Ratio)
        n = 1
        Do Until Ratio(i).size * n ^ 2 >= items
            n = n + 1
        Loop
        Ratio(i).scale = n 'this is the scaler
        Ratio(i).total = Ratio(i).size * n ^ 2 'this is the total number of cells
        Ratio(i).empty = Ratio(i).total - items
    Next
End Sub

Sub InitRatios
    RatioData:
    Data 1,1,Square (profile pictures; social media)
    Data 2,3,Classic 35mm (4x6; 6x9; etc.)
    Data 5,7,5 x 7 photo
    Data 17,22,Standard letter size (8.5x11)
    Data 4,3,Older PC monitor + analog TV (640x480; 1024x768; etc.)
    Data 4,5,Art prints + medium format (8x10; 16x20)
    Data 11,14,legal paper (11x14)
    Data 16,9,Standard HD display (1920x1080; 1280x720; etc.)
    For i = 1 To UBound(Ratio)
        Read Ratio(i).x, Ratio(i).y, Ratio(i).description
        Ratio(i).size = Ratio(i).x * Ratio(i).y
    Next
End Sub

I tried to break this down as simple as possible, so that one can use this to generate a series of grids in a set ratio and choose which layout would work best for their number of items.  I *think* this basically follows the same spirit of what @madscijr was trying to do with his code. 

One difference here though -- I removed the inverse values as they're always going to be the same result, just turned sideways!

a 4 x 6 grid holds 24 items.
a 6 x 4 grid holds the same 24 items.

Seems like a waste to list them both.  If one really wants that, then just swap your X and Y numbers.  It won't change how many items the grid would hold.  4 x 6 is the same as 6 x 4.  Wink

Try it out.  See if this does what you were trying to do, and see if it's a little bit simpler and easier to understand.  Big Grin
Reply


Messages In This Thread
Grid Formatting Demo - by SMcNeill - 02-21-2025, 05:59 PM
RE: Grid Formatting Demo - by Pete - 02-21-2025, 06:52 PM
RE: Grid Formatting Demo - by SMcNeill - 02-21-2025, 06:54 PM
RE: Grid Formatting Demo - by madscijr - 02-21-2025, 07:09 PM
RE: Grid Formatting Demo - by SMcNeill - 02-21-2025, 07:29 PM
RE: Grid Formatting Demo - by madscijr - 02-21-2025, 07:45 PM
RE: Grid Formatting Demo - by Pete - 02-21-2025, 07:13 PM
RE: Grid Formatting Demo - by SMcNeill - 02-21-2025, 07:36 PM
RE: Grid Formatting Demo - by SMcNeill - 02-21-2025, 08:07 PM
RE: Grid Formatting Demo - by Pete - 02-21-2025, 08:15 PM
RE: Grid Formatting Demo - by madscijr - 02-21-2025, 09:26 PM
RE: Grid Formatting Demo - by madscijr - 02-21-2025, 09:58 PM
RE: Grid Formatting Demo - by SMcNeill - Yesterday, 12:16 AM
RE: Grid Formatting Demo - by madscijr - Yesterday, 03:49 AM



Users browsing this thread: 2 Guest(s)