我想找到以 6x6 網格排列的陣列項的位置。我想出了兩個公式來查找專案的 X 軸和 Y 軸,但是當我想在網格中一行的最后一個位置獲取專案的位置時,計算結果為 0為 6,Y 軸也關閉。
這是你需要知道的(完整的 Lua 代碼在底部):
你想把英文字母的字符排列在一個 6x6 的網格中。您知道字母表中每個字母的位置(例如i將是 9,w將是 23) - 您需要這個來進行計算。字符從左到右寫在網格中,當您到達一行中的最后一個位置時,您從下一行的左側開始。

Don't mind the empty spots, you can fill them with imaginary letters, but they don't matter to us.
這是它的外觀。所以i的坐標 (x; y)將是:3;2。現在計算:解釋 Y 軸的計算對我來說有點太復雜了,所以我會跳過它們。這是公式:
(charTableItem.index (XAxisSize - itemIndex % XAxisSize))/XAxisSize
這是帶有解釋的 X 軸:(遺憾的是,您需要 Y 軸)
itemIndex - XAxisSize * (charYPos-1)
快速解釋:itemIndex 是英文字母表中字母的索引。XAxisSize 是行數——對我們來說是 6。charYPos 是專案所在的行。
我對此背后的邏輯的解釋:您獲取字母表中字母的索引,然后從中減去其上方的行數乘以每行中的單元格數。為什么要減 1?因為我們只想減去上面的行,而不是專案所在的行。
所以這應該可以完美地作業。但是,當您到達一行中的最后一個單元格時,計算只會導致 0。取字母f。現在的計算是6 - 6 * (2.0-1)that's6 - 6 * 1和 that's 6 - 6。l相同:12 - 6 * (3.0-1)-> 12 - 6 * 2-> 12 - 12。你現在肯定看到了問題。對于連續的每個其他單元格,計算總是正確的。
那么如何僅使用數學來解決這個問題呢?我不想使用這個功能,if因為它應該可以只用數學來完成。
lua代碼:
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
這是輸出:
=========================================================================================
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)
=========================================================================================
uj5u.com熱心網友回復:
您剛剛使用 計算錯誤charYPos,這也使您charXPos錯了。
所以這應該可以完美地作業。但是,當您到達一行中的最后一個單元格時,計算只會導致 0。以字母 f 為例。現在的計算是
6 - 6 * (2.0-1)that's6 - 6 * 1和 that's6 - 6。
從網格中,字母 f 在第 1 行,而不是 2。所以charYPos應該是 1。然后6 - 6 * (1 - 1)導致6 - 6 * (0).6
要charYPos正確計算,請使用charYPos = math.ceil(itemIndex / XAxisSize)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/486804.html
上一篇:淘汰同配置產品
