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
#2
Input 400 and you will see a % sign in front of the 1096 'empty' cell. %1096

Pete
Reply
#3
That's cause I just didn't leave enough digits for such big values.  Change that format$ to add a few ## symbols.
Reply
#4
(Yesterday, 06:54 PM)SMcNeill Wrote: That's cause I just didn't leave enough digits for such big values.  Change that format$ to add a few ## symbols.
Impressive how you boiled it down to be so simple.
However I'm not sure one of them is working... 
Entering 555 for the # of items, here's a side-by-side comparison for the 2 programs...


LayoutSteve.ColsMad.ColsSteve.RowsMad.RowsSteve.EmptyMad.Empty
HD display32331817216
Letter Size342044209415
Square242424242121

Also I'm not really sure what the scale is for. Did I mention I'm terrible at math?  Tongue

Thoughts?
Reply
#5
Ah yes, pre-stoneage PRINT USING. Big Grin 

I'm so used to displaying everything in a string routine, I totally forgot how PU acts. Anyway, added a couple more # marks and works perfectly.

+1

Pete
Reply
#6
(Yesterday, 07:09 PM)madscijr Wrote:
(Yesterday, 06:54 PM)SMcNeill Wrote: That's cause I just didn't leave enough digits for such big values.  Change that format$ to add a few ## symbols.
Impressive how you boiled it down to be so simple.
However I'm not sure one of them is working... 
Entering 555 for the # of items, here's a side-by-side comparison for the 2 programs...


LayoutSteve.ColsMad.ColsSteve.RowsMad.RowsSteve.EmptyMad.Empty
HD display32331817216
Letter Size342044209415
Square242424242121

Thoughts?

33:17 isn't the same ratio as 16:9, so yours is failing to match the grid somewhere.

16:9 is a 1.77777 ratio.
33:17 is a 1.9411 ratio.

Those aren't the same at all.  

As for the letter size, that's because I swapped it to an integer ratio and not anything fractional.  (17 x 22 instead of 8.5 x 11)  I figure it's hard to break your items into half-grids like that, so I wanted to go with an integer ratio.  

And again, the ratio for yours isn't holding true to that 17x22 ratio.  In fact, I'm not even certain how yours can call that a solution.

20 rows, 20 columns = 400 items for your grid.   How are you going to put your 555 items into the grid, and still have 5 empty spaces left over?  

Something is wrong with the math on yours, somewhere along the way.  Sad
Reply
#7
As for the scaler, it's by how much we increase our grid.

For a 1:1 ratio, you have:

1x1 == one square box  (original scale)
2x2 == four square box  (scale x 2)
3x3 == nine square boxes (scale x 3)

1:1 says that you want the results to go in a square.  If each item is the same size, the scaler tells you how big a square you need to put those items in.

Same way with a 2x3 grid
2x3 = 6 item grid (original scale)
4x6 = 24 item grid (scale x 2)
6x9 = 54 item grid (scale x 2)

Anything besides these values are going to skew your ratio, so they wouldn't be valid numbers and be able to maintain the ratio you set at the same time.  

It's like with your letterbox results above.
20 rows and 20 columns are going to create a square -- not a sheet of paper.  The values don't match the ratio you've established for yourself.
Reply
#8
(Yesterday, 07:29 PM)SMcNeill Wrote: 33:17 isn't the same ratio as 16:9, so yours is failing to match the grid somewhere.

16:9 is a 1.77777 ratio.
33:17 is a 1.9411 ratio.

Those aren't the same at all.  

As for the letter size, that's because I swapped it to an integer ratio and not anything fractional.  (17 x 22 instead of 8.5 x 11)  I figure it's hard to break your items into half-grids like that, so I wanted to go with an integer ratio.  

And again, the ratio for yours isn't holding true to that 17x22 ratio.  In fact, I'm not even certain how yours can call that a solution.

20 rows, 20 columns = 400 items for your grid.   How are you going to put your 555 items into the grid, and still have 5 empty spaces left over?  

Something is wrong with the math on yours, somewhere along the way.  Sad

If I enter 20 for the number of items, I get 
LayoutSteve.ColsMad.ColsSteve.RowsMad.RowsSteve.CellsMad.CellsSteve.EmptyMad.Empty
Square5555252555
Letter177223374213541
Standard HD16793144211241

Maybe I'm misunderstanding the numbers, I'll double check, but for 20 items, you shouldn't have so many rows/columns, or the large empty counts:
* 4:3 = 8x6, 28 empty
* 4:5 = 8x10, 60 empty
* 16:9 = 16x9, 124 empty
* 11:14 = 11x14, 134 empty
* 17:22 = 17x22, 354 empty
Reply
#9
Again, you're not maintaining your set ratios.  

Yes, a 7x3 grid is going to hold 21 items, but it's not going to be able to match your 16:9 ratio.
7x3 is going to scale to a 21:9 ratio, which doesn't match what you started with originally at all.

If that ratio is something that doesn't have to be maintained, then why's it even there to start with?  What's the point for it?

1x1 is square.
1x2 is... not square.
2x1 is... not square.

If I have 2 items, I can put them in a 1x2 grid, or a 2x1 grid, but those grids aren't going to be SQUARE -- which was the whole initial requirement for things.

It's only at:
1x1 that we have a square.
2x2 that we have a square.
3x3 that we have a square.
4x4 that we have a square.

Your description sets the ratio that you want to find a solution for, but then the solutions you are finding doesn't match that ratio.

Let's say you want to sort out how large a SQUARE grid you need to hold 12 items.
3x3 IS square, but it can only hold 9 items.
4x3 isn't square, but it can hold 12 items.
4x4 IS square, but it holds 16 items.  You have 4 empty spaces left over on your grid.

Is the 4x3 a better answer?  Sure, it might be -- but it's NOT a SQUARE grid like the ratio demanded it to be.



If you don't need to maintain the ratio, all you need to do is simple math and look for values that multiply to the closest number for you.  The best answer would always just be a simple 1xnumber grid.  It'd never have any spaces left over.
Reply
#10
4:3 = 8x6 so 48 cells available, only 20 used would be 28 empty.

The math is certainly correct, so the question would be will it work in practice.

Maybe the problem is you are wanting more of a collage effect for uneven distribution like fitting jigsaw pieces together. This cell approach, using mixed photo sizes, is a grid, meaning smaller photos get placed in the center of the grid sized to the largest photo.

Pete
Reply




Users browsing this thread: 7 Guest(s)