I want to find a position of an item of an array, that's arranged in a 6x6 grid. I've come up with both formulas for finding the X-axis and Y-axis of an item, however when I want to get the position of an item on the last position of a row in the grid, the calculation results in 0 instead of 6 and the Y axis is off too.
This is what you need to know (Full Lua code at the bottom):
You want to arrange the characters of English alphabet in a 6x6 grid.
You know the position of each letter in the alphabet (for example i will be 9 and w will be 23) - you need this for the calculations.
The characters are written in the grid from left to right, when you reach the last place in a row, you start on the left of the next row.
Don't mind the empty spots, you can fill them with imaginary letters, but they don't matter to us.
Here's how it looks. So the coordinates (x; y) for i will be: 3;2
.
Now the calculations:
Explaining the calculations of the Y axis are a bit too complicated for me, so I'll skip them. Here's the formula:
(charTableItem.index (XAxisSize - itemIndex % XAxisSize))/XAxisSize
And here's the X axis with an explanation: (Sadly you need the Y axis for this)
itemIndex - XAxisSize * (charYPos-1)
Quick explanation:
itemIndex is the index if a letter in the English alphabet.
XAxisSize is the number of rows - for us it's 6.
charYPos is the row which the items is in.
My explanation of the logic behind this: You take the index of the letter in alphabet, and subtract from it the number of rows that are above it multiplied by the number of cells in each row. Why do we subtract 1? Because we want to only subtract the rows above, not the one the item is in.
So this should work perfectly. However, when you reach the last cell in a row, the calculation just leads to 0. Take the letter f. Now the calculation is 6 - 6 * (2.0-1)
that's 6 - 6 * 1
and that's 6 - 6
. Same for l: 12 - 6 * (3.0-1)
-> 12 - 6 * 2
-> 12 - 12
. You sure see the problem now. For every other cell in a row the calculation is always correct.
So how do I solve this problem using only math? I don't want to use the if
funciton since it's supposed to be possible to be done with only mathematics.
The lua code:
local charTable = {{char= "a", index= 1, xpos = 1, ypos = 1},{char= "b", index= 2, xpos = 2, ypos = 1},{char= "c", index= 3, xpos = 3, ypos = 1},{char= "d", index= 4, xpos = 4, ypos = 1},{char= "e", index= 5, xpos = 5, ypos = 1},{char= "f", index= 6, xpos = 6, ypos = 1},{char= "g", index= 7, xpos = 1, ypos = 2},{char= "h", index= 8, xpos = 2, ypos = 2},{char= "i", index= 9, xpos = 3, ypos = 2},{char= "j", index= 10, xpos = 4, ypos = 2},{char= "k", index= 11, xpos = 5, ypos = 2},{char= "l", index= 12, xpos = 6, ypos = 2},{char= "m", index= 13, xpos = 1, ypos = 3},{char= "n", index= 14, xpos = 2, ypos = 3},{char= "o", index= 15, xpos = 3, ypos = 3},{char= "p", index= 16, xpos = 4, ypos = 3},{char= "q", index= 17, xpos = 5, ypos = 3},{char= "r", index= 18, xpos = 6, ypos = 3},{char= "s", index= 19, xpos = 1, ypos = 4},{char= "t", index= 20, xpos = 2, ypos = 4},{char= "u", index= 21, xpos = 3, ypos = 4},{char= "v", index= 22, xpos = 4, ypos = 4},{char= "w", index= 23, xpos = 5, ypos = 4},{char= "x", index= 24, xpos = 6, ypos = 4},{char= "y", index= 25, xpos = 1, ypos = 5},{char= "z", index= 26, xpos = 2, ypos = 5},}
local gridSize = 6
local function mainFunc(charTableItem)
local YAxisSize = (#charTable (gridSize - #charTable % gridSize))/gridSize -- not needed for the calculation of char position
local XAxisSize = gridSize -- just for better understanding of what we're working with
local charYPos = (charTableItem.index (XAxisSize - charTableItem.index % XAxisSize))/XAxisSize
local charXPos = charTableItem.index - XAxisSize * (charYPos-1)
-- this is just a check - not part of the calculation - obviously
local isXSame = false
if charTableItem.xpos == charXPos then
isXSame = true
end
local isYSame = false
if charTableItem.ypos == charYPos then
isYSame = true
end
print("=========================================================================================")
print(charTableItem.char.." Y position: ", charYPos, " |", charTableItem.ypos, "|", isYSame, "("..charTableItem.index.." ("..XAxisSize.." - "..charTableItem.index.." % "..XAxisSize.."))/"..XAxisSize)
print(charTableItem.char.." X position: ", charXPos, " |", charTableItem.xpos, "|", isXSame, charTableItem.index.." - "..XAxisSize.." * ("..charYPos.."-1)")
return {x = charXPos, y = charYPos}
end
for i in pairs(charTable) do
mainFunc(charTable[i])
end
And here's the output:
=========================================================================================
a Y position: 1.0 | 1 | true (1 (6 - 1 % 6))/6
a X position: 1.0 | 1 | true 1 - 6 * (1.0-1)
=========================================================================================
b Y position: 1.0 | 1 | true (2 (6 - 2 % 6))/6
b X position: 2.0 | 2 | true 2 - 6 * (1.0-1)
=========================================================================================
c Y position: 1.0 | 1 | true (3 (6 - 3 % 6))/6
c X position: 3.0 | 3 | true 3 - 6 * (1.0-1)
=========================================================================================
d Y position: 1.0 | 1 | true (4 (6 - 4 % 6))/6
d X position: 4.0 | 4 | true 4 - 6 * (1.0-1)
=========================================================================================
e Y position: 1.0 | 1 | true (5 (6 - 5 % 6))/6
e X position: 5.0 | 5 | true 5 - 6 * (1.0-1)
=========================================================================================
f Y position: 2.0 | 1 | false (6 (6 - 6 % 6))/6
f X position: 0.0 | 6 | false 6 - 6 * (2.0-1)
=========================================================================================
g Y position: 2.0 | 2 | true (7 (6 - 7 % 6))/6
g X position: 1.0 | 1 | true 7 - 6 * (2.0-1)
=========================================================================================
h Y position: 2.0 | 2 | true (8 (6 - 8 % 6))/6
h X position: 2.0 | 2 | true 8 - 6 * (2.0-1)
=========================================================================================
i Y position: 2.0 | 2 | true (9 (6 - 9 % 6))/6
i X position: 3.0 | 3 | true 9 - 6 * (2.0-1)
=========================================================================================
j Y position: 2.0 | 2 | true (10 (6 - 10 % 6))/6
j X position: 4.0 | 4 | true 10 - 6 * (2.0-1)
=========================================================================================
k Y position: 2.0 | 2 | true (11 (6 - 11 % 6))/6
k X position: 5.0 | 5 | true 11 - 6 * (2.0-1)
=========================================================================================
l Y position: 3.0 | 2 | false (12 (6 - 12 % 6))/6
l X position: 0.0 | 6 | false 12 - 6 * (3.0-1)
=========================================================================================
m Y position: 3.0 | 3 | true (13 (6 - 13 % 6))/6
m X position: 1.0 | 1 | true 13 - 6 * (3.0-1)
=========================================================================================
n Y position: 3.0 | 3 | true (14 (6 - 14 % 6))/6
n X position: 2.0 | 2 | true 14 - 6 * (3.0-1)
=========================================================================================
CodePudding user response:
You just made a calculation error with charYPos
, which makes your charXPos
also wrong.
So this should work perfectly. However, when you reach the last cell in a row, the calculation just leads to 0. Take the letter f. Now the calculation is
6 - 6 * (2.0-1)
that's6 - 6 * 1
and that's6 - 6
.
From the grid, letter f is in row 1, not 2. So charYPos
should be 1.
Then 6 - 6 * (1 - 1)
which is 6 - 6 * (0)
resulting in 6
.
To calculate charYPos
correctly, use charYPos = math.ceil(itemIndex / XAxisSize)