一種獲取九宮格index的方式

2022-07-30 10:54:09 字數 2518 閱讀 3063

<1>由來:

在做一些需求的時候,我們可能需要把地圖劃分成均勻網格,隨機找乙個格仔a,需要取到這個a格仔九宮格內的所有格仔索引

例如我們把地圖劃分成3x3網格(從左下開始 行向y軸正方向延伸 列向x軸正方形延伸)

用int表示每個格仔索引

7 8 9

4 5 6

1 2 3

用int[2]表示每個格仔索引

3,1 3,2 3,3

2,1 2,2 2,3

1,1 1,2 1,3

例如:  

此時我們需要知道7(或者表示為2,2)的九宮格,均勻網格定義為:行=4 列=5

16 17 18 19 20 

11 12 13 14 15

6 7 8 9 10

1 2 3 4 5

listlst = nineutils.getninegrid(7);或者listlst=  nineutils.getninegrid(2,2);

結果為 13    8    3    2    1    6    11    12

listlst = nineutils.getninegrid(10);或者listlst=  nineutils.getninegrid(2,5);

結果為 5    4    9    14    15

public class nineutils

},},

},},

},},

},},

};//定義行和列

public int rownum = 4;

public int colnum = 5;

public listgetninegrid(int index)

public listgetninegrid(int rw)

public listgetninegrid(int row, int col)

}return indexmap;

}private int getrow(int index)

private int getcol(int index)

//驗證合法性

private bool isvaildindex(int xy)

}

貼乙個之前寫的版本(lua寫的 有點噁心)

--獲取格仔周圍所有格仔

function battleroyaledropmgr:getroundindex(index)

local lst = {}

--top 沒有上 r1r2r3 --bottom沒有下 r6r7r8 --left沒有左r1r4r6 --right沒有右r3r5r8

local istop = index<=maxrow

local isbottom = index > (maxcol-1)*maxrow

local isleft = index%maxrow == 1

local isright = index%maxrow == 0

local filter = {}

local r1 = index - maxrow - 1

local r2 = index - maxrow

local r3 = index - maxrow + 1

local r4 = index - 1

local r5 = index + 1

local r6 = index + maxrow - 1

local r7 = index + maxrow

local r8 = index + maxrow + 1

if istop then

filter[r1] = true

filter[r2] = true

filter[r3] = true

endif isbottom then

filter[r6] = true

filter[r7] = true

filter[r8] = true

endif isleft then

filter[r1] = true

filter[r4] = true

filter[r6] = true

endif isright then

filter[r3] = true

filter[r5] = true

filter[r8] = true

end

local result =

for i =1,#result do

if filter[result[i]] == nil then

lst[result[i]] = true

endend

lst[index] = true

return lst

end

CSS九宮格的4種實現

總結我自己一共總結了4種方法來實現這個效果,前三種方法是大同小異,只有第四種 布局比較特殊。下面我直接給出每一種布局方式相關的樣式和dom結構的原始碼。1.float布局float布局實現這個9宮格沒什麼好講的,關鍵點在於對li子項設定margin left 4px margin top 4px 這...

Android中的九宮格

1 實現基類 1 item數量控制 private void ensureitems int count else if c count for int i 0 i getchildcount i 2 mearsure處理 override protected final void onmeasur...

九宮格一共有多少種解法

今天同事和我討論了九宮格的回溯問題,說實話,我之前玩過一款遊戲叫 九陰真經 裡面有乙個副本第二關開啟就要解乙個九宮格。我一直認為有很多種解法,至少得有百八十種吧。但是用回溯解完之後,發現,我太天真了。不就是1 9的數字放到九個框框裡面麼?我把所有的可能都回溯出來,再看看滿不滿足九宮格的條件不就得了,...