Jump to content


Photo

Math question regarding columns and rows


  • Please log in to reply
6 replies to this topic

#1 Coco

Coco
  • Members
  • 613 posts

Posted 28 July 2012 - 11:36 AM

How do I get the current row or column position if an item in a grid given that I only know the following:
Example:
Total Rows = 3
Total Columns = 3
Item Index = 5
Based on the example, we can picture out that it's a 3x3 grid and that the 5th item is in row 2 and column 2. What is the math formula to determine it's current row and column position?

#2 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 28 July 2012 - 11:44 AM

Much depends on how items are stored in the grid. They could be stored row first or column first. Or in the case of a 3 dimensional grid even by page. And, you'd need to know the type of the item stored in each cell. It's possible that any cell could hold any given type of variable -- string, double, byte, integer, or even an object, or a pointer to another grid etc. so you would have to know the something about the data being stored in order to retrieve any portion of the cells contents successfully.

#3 Coco

Coco
  • Members
  • 613 posts

Posted 28 July 2012 - 11:55 AM

I'm trying to position window thumbnails in a grid, so far I know how to get the the total number of rows and columns base on the number of windows(excluding the minimized ones). And their position is determined by their index.
Let's say there are 6 windows:

cols := Ceil(Sqrt(6))
    rows := Ceil(Sqrt(6))
    if (cols*(rows-1) >= 6)
        rows--

This should give me 3 columns and 2 rows, how would I know the current window's position in the grid.? Window 1 should be in the row 1 and column 1; Window 2 should be in row 1 column 2 ans so on...

#4 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 28 July 2012 - 02:01 PM

Total Rows = 3
Total Columns = 3
Item Index = 5
Based on the example, we can picture out that it's a 3x3 grid and that the 5th item is in row 2 and column 2.
What is the math formula to determine it's current row and column position?



TotalRows    := 3 [color=#FF0000]; Not used[/color]
TotalColumns := 3
ItemIndex    := 5

Row := Ceil( ItemIndex / TotalColumns )
Col := ( C := Mod( ItemIndex, TotalColumns ) ) ? C : TotalColumns
Msgbox % Row "," Col


#5 Coco

Coco
  • Members
  • 613 posts

Posted 28 July 2012 - 02:54 PM

Thanks SKAN, really great..my solution was different and a little bit "messy":

rows := 3
cols := 3
Item_Index := 5

pos_Cols = 
pos_Rows = 1
Loop, % Item_Index
{
    pos_Cols++
    if pos_Cols > % cols
    {
        pos_Cols = 1
        pos_Rows++
    }
}
MsgBox, Column Pos: %pos_Cols%, Row Pos: %pos_Rows%


#6 MasterFocus

MasterFocus
  • Moderators
  • 4132 posts

Posted 28 July 2012 - 04:48 PM

Here is a very related code I created some time ago.
Change the variables var_TotalGrid and var_CellSize to test it.

----------------------------------------------------------------------------------------------
License: GNU AGPL v3.0 ( see the quick guide )
Please check: MasterFocus's Home Topic
This post's latest update: 03/september/2012
Latest code modification: 29/november/2010
----------------------------------------------------------------------------------------------

Script:

Grid Template

Description:

This template tries to find the best way to fit images inside a Gui (like a grid)

Additional info:

< none >

Changelog:

29/november/2010

• Initial version


Code:

; By MasterFocus
; Licensed under GNU AGPL v3
; Please see the following link for more information:
; http://www.autohotkey.com/community/viewtopic.php?p=554782#p554782

; Create a Gui to show the images
Gui, -Caption +Border
Gui, Color, White
Gui, Margin, 0, 0

var_TotalGrid := 8 ; total number of grid cells
var_CellSize := 100 ; side size (each cell is a square)

Loop, %var_TotalGrid% ; Define example pictures (in this case, just text really)
var_Image%A_Index% := "img " A_INDEX " here"

; Define some variables, just to keep track of them
var_ImgPerLine := var_Count := var_TempA := 0
var_TempB := var_TempC := var_TempMod := 0

; Calculate number of images per line
var_TempA := Sqrt(var_TotalGrid)
var_TempB := Floor(var_TempA)
var_TempC := !( var_TempA = var_TempB )
var_ImgPerLine := var_TempC + var_TempB
; the whole thing can be shortened, but I left these
; calculations (and the whole template) this way for readability

; Add pictures to the Gui
Loop, %var_TotalGrid%
{
    Gui, Add, Picture, % "w" var_CellSize " h" var_CellSize
        ;. " gLABEL_WHEN_IMG_IS_CLICKED" ; currently unused
        . " x" var_CellSize*(var_TempMod:=Mod(A_Index-1,var_ImgPerLine))
        . " y" ( var_TempMod ? "p" : var_CellSize*(var_Count++) ) , % var_Image%A_Index%
}

; Show Gui, wait a little bit and exit
Gui, Show, AutoSize
Sleep, 4000
ExitApp


#7 Coco

Coco
  • Members
  • 613 posts

Posted 29 July 2012 - 01:18 AM

Really cool, MasterFocus..your script is really helpful..I'm currently rewriting my "Expose" script, inspired by Holomind's Real Eexpose Clone. I'm using maul.esel's Thumbnail library. I had it working already but I had to rewrite my grid-positioning function as it gets longer and longer the more windows I'd like my script to support. Thanks to you guys, I was able to simplify it..